1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #define LEN sizeof(struct Student) 5 struct Student 6 { 7 long num; 8 float score; 9 struct Student*next; 10 }; 11 int n; 12 int main() 13 { 14 /*-----------------------------程序描述-------------------------------------------- 15 题目:写出一个主函数,分别调用建立链表的函数create(),输出链表的函数print(), 16 删除链表结点的函数del(),插入结点的函数insert(),一共5个函数。 17 Author:KillerLegend 18 Date: 2013.12.6 19 ----------------------------------------------------------------------------------*/ 20 //函数声明 21 struct Student* create();//创建动态链表的函数声明,函数类型为student结构体类型,返回头指针 22 struct Student* del(struct Student* ,long);//删除指定位置结点的函数声明,参数:链表头结点+删除结点位置+返回头指针 23 struct Student* insert(struct Student*,struct Student*);//插入一个Student类型数据的函数声明 24 void print(struct Student*);//输出链表中数据的函数声明,参数为链表的头指针 25 //定义变量 26 struct Student *head,*stu;//定义动态链表的头指针与新的结点 27 long del_num; 28 //建立链表操作 29 printf("Input records: "); 30 head = create();//建立链表并返回头指针 31 print(head);//输出全部结点 32 33 //删除结点操作 34 printf(" Input the deleted number:"); 35 scanf("%ld",&del_num); 36 while(del_num!=0)//当输入学号为0时结束循环 37 { 38 head = del(head,del_num);//删除结点后返回链表的头地址 39 print(head);//输出全部结点 40 printf("Input the deleted number:"); 41 scanf("%ld",&del_num); 42 } 43 //插入结点操作 44 printf(" Input the inserted number:"); 45 stu=(struct Student*)malloc(LEN);//每插入一个结点需要开辟一个新的结点 46 scanf("%ld %f",&stu->num,&stu->score); 47 while(stu->num!=0)//当输入的学号为0时结束循环 48 { 49 head = insert(head,stu);//返回链表的头地址 50 print(head); 51 printf(" Input the inserted number:"); 52 stu = (struct Student*)malloc(LEN); 53 scanf("%ld %f",&stu->num,&stu->score); 54 } 55 return 0; 56 } 57 //建立链表的函数 58 struct Student* create() 59 { 60 struct Student *head; 61 struct Student *p1,*p2; 62 n=0; 63 p1=p2=(struct Student *)malloc(LEN); 64 scanf("%ld %f",&p1->num,&p1->score); 65 head=NULL; 66 while(p1->num!=0) 67 { 68 n++; 69 if(n==1)head=p1; 70 else p2->next=p1;//第一次执行时,这一步是将头指针指向自身,当n从2起,这一步用于使p2指向下一个元素 71 p2=p1;//使p2和p1指向同一个存储区 72 p1=(struct Student*)malloc(LEN);//开辟动态存储区,强制返回struct Student类型的指针 73 scanf("%ld %f",&p1->num,&p1->score); 74 } 75 p2->next=NULL; 76 return (head); 77 } 78 79 //删除结点的函数 80 struct Student* del(struct Student* head,long num) 81 { 82 struct Student *p1,*p2; 83 if(head==NULL) 84 { 85 printf("List null! "); 86 return (head); 87 } 88 p1=head; 89 while(num!=p1->num && p1->next!=NULL) 90 { 91 p2=p1; 92 p1=p1->next; 93 } 94 if(num==p1->num) 95 { 96 if(p1==head) 97 { 98 head=p1->next; 99 } 100 else 101 { 102 p2->next=p1->next; 103 } 104 printf("Delete:%ld ",num); 105 n=n-1; 106 } 107 else 108 { 109 printf("%ld not been found!",num); 110 } 111 return (head); 112 } 113 114 //插入结点的函数 115 struct Student* insert(struct Student* head,struct Student * stud) 116 { 117 struct Student *p0,*p1,*p2; 118 p1=head; 119 p0=stud; 120 if(head==NULL)//原来的链表是空表 121 { 122 head=p0;p0->next=NULL;//空表时使插入的结点作为头结点 123 } 124 else//如果不是空表,则遍历寻找合适的插入位置 125 { 126 while((p0->num>p1->num)&&(p1->next!=NULL))//按学号顺序插入,如果插入的学号数字比较大,则应该向后推移 127 { 128 p2=p1; 129 p1=p1->next;//后移 130 } 131 } 132 if(p0->num<=p1->num)//找到插入的位置,插入的位置是p1所指向的位置之前,也就是p2指向的位置 133 { 134 if(head==p1)head=p0;//如果插入的位置是头位置之前,则使head指向p0 135 else p2->next=p0;//如果不是头位置之前,则使p2的next指针指向插入的数据地址即p0 136 p0->next=p1;//使p0的next指针指向p1,完成了数据的加入 137 } 138 else//插入的学号位置在最后一个 139 { 140 p1->next=p0; 141 p0->next=NULL; 142 } 143 n=n+1;//记录数加一 144 return(head); 145 } 146 //输出链表的函数 147 void print(struct Student * head) 148 { 149 struct Student * p; 150 printf("Now,These %d records are: ",n); 151 p=head; 152 if(head!=NULL) 153 do 154 { 155 printf("%ld %5.1f ",p->num,p->score); 156 p=p->next; 157 }while(p!=NULL); 158 }
注:CodeBlocks上调试通过。