1 #include <stdio.h> 2 #include <malloc.h> 3 #define LEN sizeof(struct student) 4 5 /*----------------數據定義----------------------*/ 6 7 //定義一個(gè)學(xué)生信息的結構體,包括學(xué)號,姓名和結構體類(lèi)型的指針 8 struct student 9 { 10 long num; //學(xué)號 11 char name[128]; //姓名 12 struct student *next; //結構體指針 13 }; 14 15 typedef struct student * stuNode; 16 17 int n=0; //全局變量,記錄鏈表的長(cháng)度 18 19 /*---------------函數聲明---------------------*/ 20 21 stuNode Create(); //創(chuàng )建一個(gè)新的鏈表 22 23 void Print(stuNode head); //通過(guò)傳入的鏈表頭指針打印整個(gè)鏈表 24 25 stuNode Delete(stuNode head,int num); //通過(guò)傳入的鏈表頭指針和學(xué)生學(xué)號刪除節點(diǎn) 26 27 stuNode Insert(stuNode head,stuNode newStu); //依照學(xué)生學(xué)號的順序向鏈表中插入新元素 28 29 30 /*---------------函數定義----------------------*/ 31 32 struct student *Create() 33 { 34 struct student *head,*p1,*p2; 35 36 //開(kāi)辟一個(gè)LEN大小的空間,并讓p1,p2指針指向它 37 p2=p1=(struct student *)malloc(LEN); 38 //將頭指針置為NULL 39 head=NULL; 40 41 //創(chuàng )建鏈表節點(diǎn)并給節點(diǎn)的元素賦值 42 printf("請輸入學(xué)生的學(xué)號和姓名:"); 43 scanf("%ld %s",&p1->num,p1->name); 44 while(p1->num!=0) 45 { 46 n=n+1; 47 if(NULL==head) 48 { 49 head=p1; 50 } 51 else 52 { 53 p2->next=p1; 54 } 55 p2=p1; 56 p1=(struct student *)malloc(LEN); 57 printf("請輸入學(xué)生的學(xué)號和姓名:"); 58 scanf("%ld %s",&p1->num,p1->name); 59 } 60 //將尾節點(diǎn)的指針置為NULL 61 p2->next=NULL; 62 return head; 63 } 64 65 66 void Print(struct student *head) 67 { 68 struct student * p; 69 p=head; 70 71 //判斷鏈表是否為空 72 if(NULL==head) 73 { 74 printf("鏈表為空!\n"); 75 return head; 76 } 77 else 78 { 79 //循環(huán)打印鏈表中的元素 80 printf("%d 個(gè)記錄分別為:\n",n); 81 while(p!=NULL) 82 { 83 printf("%ld %s\n",p->num,p->name); 84 //指針指向下一個(gè)節點(diǎn) 85 p=p->next; 86 } 87 } 88 } 89 90 91 struct student *Delete(struct student * head,int num) 92 { 93 struct student *p1; 94 struct student *p2; 95 p1=head; 96 //判斷鏈表是否為空 97 if(NULL==head) 98 { 99 printf("鏈表為空!\n");100 return head;101 }102 //遍歷節點(diǎn),判斷當前節點(diǎn)是不是需要刪除的節點(diǎn)及是否為尾節點(diǎn)103 //如果找到相應節點(diǎn),或者已經(jīng)遍歷到尾節點(diǎn)就跳出循環(huán) 104 while(p1->num!=num&&p1->next!=NULL)105 {106 p2=p1;107 p1=p1->next;108 }109 //判斷是否找到相應節點(diǎn) 110 if(p1->num==num)111 {112 //要刪除的節點(diǎn)是不是鏈表的第一個(gè)節點(diǎn)113 //如果是,就將頭指針指向該節點(diǎn)的后一個(gè)節點(diǎn)114 //如果不是,就將該節點(diǎn)的前一個(gè)節點(diǎn)的指針指向該節點(diǎn)的后一個(gè)節點(diǎn) 115 if(head==p1)116 {117 head=p1->next;118 }119 else120 {121 p2->next=p1->next;122 }123 n=n-1;124 printf("%ld 節點(diǎn)已刪除.\n",num);125 }126 else127 {128 printf("鏈表中沒(méi)有要刪除的元素.\n");129 }130 return head;131 }132 133 134 struct student *Insert(struct student * head,struct student * newStu)135 {136 struct student *p0;137 struct student *p1;138 struct student *p2;139 p0=newStu;140 p1=head;141 //判斷鏈表是否為空,如果是空鏈表,就將新節點(diǎn)作為第一個(gè)節點(diǎn) 142 if(NULL==head)143 {144 head=p0;145 p0->next=NULL;146 }147 else148 {149 //遍歷每一個(gè)節點(diǎn)中的學(xué)號,與新學(xué)號比較大小150 //如果找到一個(gè)學(xué)號比新學(xué)號大,就將新學(xué)號的節點(diǎn)插入它之前 151 //如果尾節點(diǎn)的學(xué)號仍比新學(xué)號小,就將新節點(diǎn)插入到鏈表尾部 152 while((p0->num > p1->num)&&(p1->next!=NULL))153 {154 p2=p1;155 p1=p1->next;156 }157 //找到一個(gè)比新學(xué)號大的節點(diǎn) 158 if(p0->num <= p1->num)159 {160 //判斷該節點(diǎn)是否為頭節點(diǎn),如果是,則將新節點(diǎn)設置為頭節點(diǎn) 161 if(p1==head)162 {163 head=p0;164 }165 else166 {167 p2->next=p0;168 }169 p0->next=p1;170 }171 else172 {173 p1->next=p0;174 p0->next=NULL;175 }176 }177 //鏈表長(cháng)度加1 178 n=n+1;179 printf("%ld 插入成功!\n",newStu->num);180 return head;181 }182 183 void main()184 {185 struct student *head;186 struct student *stu;187 int num;188 head=Create();189 Print(head);190 printf("請輸入要刪除的學(xué)號:");191 scanf("%ld",&num);192 while(num!=0)193 {194 head=Delete(head,num);195 Print(head);196 printf("請輸入要刪除的學(xué)號:");197 scanf("%ld",&num);198 }199 printf("請輸入要插入的節點(diǎn):");200 stu=(struct student *)malloc(LEN);201 scanf("%ld %s",&stu->num,stu->name);202 while(stu->num!=0)203 {204 head=Insert(head,stu);205 printf("請輸入要插入的節點(diǎn):");206 stu=(struct student *)malloc(LEN);207 scanf("%ld %s",&stu->num,stu->name);208 }209 Print(head);210 }
聯(lián)系客服