链表,类似学校信息管理系统

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{//学生信息的数据类型
char name[20];
char sex[15];
int score;
struct node *pNext;
}Node,*pNode;
void printList(pNode pHead);

void print_Info()
{
printf(" ________________________________\n");
printf("|姓名 |性别 |分数 |\n");
printf("|__________|__________|__________|\n");
}

void arr_Change(char a[],char b[])
{
char c[20];
strcpy(c,a);
strcpy(a,b);
strcpy(b,c);
return;
}

void num_Change(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return;
}

void input_Node(pNode student)//链表元素输入函数
{
//scanf("%s\n%s\n%d",&student->name,&student->sex,&student->score);//链表元素输出函数
printf("姓名:");
scanf("%s",&student->name);
printf("性别:");
scanf("%s",&student->sex);
printf("分数:");
scanf("%d",&student->score);
return ;
}

void output_Node(pNode student)//链表元素输出函数
{
printf("|%-10s|%-10s|%-10d|\n",&student->name,&student->sex,student->score);
printf("|__________|__________|__________|\n");
return ;
}

pNode head_Create_List(int num) //创建链表采用的是头插法 ,打印的顺序相反 p->next=head->next;head->next=p;
{
int i=0;
pNode head;
pNode p;
head->pNext=NULL;
printf("请输入%d名学生信息:\n",num);
while(i<num)
{
p=(pNode)malloc(sizeof(Node));
input_Node(p);
p->pNext=head->pNext;
head->pNext=p;
i++;
}
return head;
}

void printList(pNode pHead)//打印链表
{
int i;
//printf("\n");
pNode p=pHead->pNext;
while(p!=NULL)
{
output_Node(p);
p=p->pNext;
}
printf("\n");
return;
}

pNode tail_Create_List(int num)//尾插法end->next=node;end=node
{
int i=0;
pNode head=NULL;//头结点
pNode tail=NULL;//尾节点
pNode node=NULL;//中间节点
head=(pNode)malloc(sizeof(Node));
tail=head;
printf("请输入%d名学生的信息\n",num);
while(i<num)
{
node=(pNode)malloc(sizeof(Node));
input_Node(node);
tail->pNext=node;
tail=node;
i++;
}
tail->pNext=NULL;
return head;
}

void insertList(pNode pHead,int loc)//插入链表
{
int i;
pNode p;
p=pHead;
for(i=0;i<loc-1;i++)
{
p=p->pNext;
}
pNode s;
printf("请输入您想要插入的学生信息:\n") ;
s=(pNode)malloc(sizeof(Node));
input_Node(s);//插入信息的输入
s->pNext=p->pNext;
p->pNext=s;
return;
}

void deleteList(pNode pHead,int loc)//删除链表
{
int i;
pNode p,q;//p是前驱节点,q是删除节点
p=pHead;
for(i=0;i<loc-1;i++)
{
p=p->pNext;
}
q=p->pNext;
p->pNext=q->pNext;
free(q);
return;
}

int get_Single_Listlength(pNode pHead)
{
int length=0;
pNode p;
p=pHead;
while(p!=NULL)
{
p=p->pNext;
length++;
}
return length-1;
}

pNode bubble_Single_List(pNode pHead)
{
pNode pFirst,pEnd;
pFirst=NULL;pEnd=NULL;
pFirst=pHead;
while(pFirst!=pEnd)
{
while(pFirst->pNext!=pEnd)
{
if(pFirst->score<pFirst->pNext->score)
{
arr_Change(pFirst->name,pFirst->pNext->name);
arr_Change(pFirst->sex,pFirst->pNext->sex);
num_Change(&pFirst->score,&pFirst->pNext->score);
}
pFirst=pFirst->pNext;
}
pEnd=pFirst;
pFirst=pHead;
}
return pHead;
}


void destoryList(pNode pHead)//释放内存函数
{
pNode p;
p=pHead;
while(p!=NULL)
{
free(p);
p=p->pNext;
}
return ;
}


int main(void)
{
int num=0;
pNode head,bubble;
int loc=0;
int length;
int deleteLoc;
int changeFlags;
printf("请用单链表尾插法输入学生信息的个数:\n");
scanf("%d",&num);
printf("你想要插入的学生的个数为:%d\n",num);
head=tail_Create_List(num);
printf("\n当前学生的信息如下\n");
print_Info();
printList(head);

printf("依据提示进行操作 :\n");
printf("请输入操作符号,1:插入,2:删除,3:退出,\n");
scanf("%d",&changeFlags);
while(changeFlags!=3)
{
int loc,deleteloc;

if(changeFlags==1)
{
printf("请输入您想要插入的位置:\n");
scanf("%d",&loc);
insertList(head,loc);
}
if(changeFlags==2)
{
printf("请输入您想要删除的位置:\n");
scanf("%d",&deleteloc);
deleteList(head,deleteloc);
}

printf("请继续输入操作符号,1:插入,2:删除,3:退出,\n");
changeFlags=0;
scanf("%d",&changeFlags);
if(changeFlags==3)
{
break;
}
}

//length=get_Single_Listlength(head);
bubble=bubble_Single_List(head);
print_Info();
printList(bubble);

destoryList(head);//销毁链表
return 0;
}

//运行环境DEVC++

猜你喜欢

转载自www.cnblogs.com/--dafeqi--dafenqi--dafenqi/p/11607490.html
今日推荐