c语言链表应用程序

很多学软件的朋友需要做C语言链表应用的课程设计,下面是我写的代码,希望对需要的人有些帮助。。。希望和大家交流一下,更希望高手指点。c语言链表应用程序。已经经过(turboC 2.0编译通过)。

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student
{ int number;
int score;
struct student*next;
};


struct student*create()
{ struct student*head,*p1,*p2;
int n;
n=0;
head=NULL;
p2=NULL;
p1=NULL;
p1=(struct student*)malloc(LEN);
scanf("%d %d",&p1->number,&p1->score);
while(p1->number!=0)
{n++;
if(n==1) {head=p1;p2=p1;}
else {p2->next=p1;p2=p1;p2->next=NULL;}
p1=(struct student*)malloc(LEN);
scanf("%d %d",&p1->number,&p1->score);
p1->next=NULL;

}
free(p1);
return(head);
}


void print(struct student*head)
{ struct student *p1;
p1=head;
while(p1!=NULL)
{printf("%d %d\n",p1->number,p1->score);
p1=p1->next;
}
}


int count(struct student*head)
{ int n=0;
struct student*p1;
p1=head;
while(p1!=NULL)
{
p1=p1->next;
n++;
}
return(n);

}



struct student* reverse(struct student*head)
{ struct student*p1,*p2,*pa;
p1=head;
p2=p1->next;
p1->next=NULL;
while(p2!=NULL)
{ pa=p2->next;
p2->next=p1;
p1=p2;
p2=pa;

}

head=p1;
return(head);
}


struct student*delete1(struct student*head,int number)
{ struct student*p1,*p2;
p1=head;
p2=p1->next;
if(p2==NULL) head=NULL;

while(p2!=NULL)
{ if(p1->number==number)
{head=p2;
free(p1);break;
}
else if(p2->number==number)
{p1->next=p2->next;
free(p2);break;
}
p1=p1->next;
p2=p2->next;
}


return(head);
}



struct student*delete2(struct student*head,int I,int K)
{ struct student*p1,*pa,*pb,*pc;
int n;
p1=head;
if(I==1)
{for(n=0;n<K;n++)
{pa=p1->next;
free(p1);
p1=pa;
}
head=p1;
}
else
{for(n=1;n<I;n++)
{pb=p1;
p1=p1->next;
}
for(n=0;n<(K-I+1);n++)
{pc=p1->next;
free(p1);
p1=pc;
}
pb->next=p1;
}
return(head);
}



main()
{struct student*list;

int a,I=0,K=0,c;
printf("please input the information of students(number and score):\n");
list=create();
printf("\nthis is the list which you have inputed:\n");


print(list);
do
{
printf("\n\n*****************************\n\n");
printf(" 1.to reverse\n");
printf(" 2.delete a node\n");
printf(" 3.delete nodes\n");
printf(" 0.exit\n");
printf("\n******************************\n");
a=0;
scanf("%d",&a);
switch(a)
{ case 1: list=reverse(list);
printf("the result is:\n") ;
print(list);break;

case 2: printf("please input the number of the node which you want to delete:");scanf("%d",&I);
list=delete1(list,I);
printf("\n\nthe result is:\n");
print(list);break;

case 3:printf("\n\nplease input I and K separate with space(delete the Ith node to the Kth ):");
scanf("%d %d",&I,&K);
if(I>count(list)||K>count(list)||I>K) {printf("\nerror!!!\n"); break;}

list=delete2(list,I,K);
printf("\n\nthe result is :\n");
print(list);break;


case 0:exit(0);

}
printf("\ndo you still have anything to do?:1.yes. 2.no.\n");
scanf("%d",&c);

} while(c!=2);


}
原文地址:https://www.cnblogs.com/wonderKK/p/2240395.html