1 #include <stdio.h>
2 #include <malloc.h>
3 #define LEN sizeof(struct student)
4
5 /*----------------數據定義----------------------*/
6
7 //定義一個學生信息的結構體,包括學號,姓名和結構體類型的指針
8 struct student
9 {
10 long num; //學號
11 char name[128]; //姓名
12 struct student *next; //結構體指針
13 };
14
15 typedef struct student * stuNode;
16
17 int n=0; //全局變量,記錄鏈表的長度
18
19 /*---------------函數聲明---------------------*/
20
21 stuNode Create(); //創建一個新的鏈表
22
23 void Print(stuNode head); //通過傳入的鏈表頭指針打印整個鏈表
24
25 stuNode Delete(stuNode head,int num); //通過傳入的鏈表頭指針和學生學號刪除節點
26
27 stuNode Insert(stuNode head,stuNode newStu); //依照學生學號的順序向鏈表中插入新元素
28
29
30 /*---------------函數定義----------------------*/
31
32 struct student *Create()
33 {
34 struct student *head,*p1,*p2;
35
36 //開闢一個LEN大小的空間,並讓p1,p2指針指向它
37 p2=p1=(struct student *)malloc(LEN);
38 //將頭指針置爲NULL
39 head=NULL;
40
41 //創建鏈表節點並給節點的元素賦值
42 printf("請輸入學生的學號和姓名:");
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("請輸入學生的學號和姓名:");
58 scanf("%ld %s",&p1->num,p1->name);
59 }
60 //將尾節點的指針置爲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 //循環打印鏈表中的元素
80 printf("%d 個記錄分別爲:\n",n);
81 while(p!=NULL)
82 {
83 printf("%ld %s\n",p->num,p->name);
84 //指針指向下一個節點
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 //遍歷節點,判斷當前節點是不是需要刪除的節點及是否爲尾節點
103 //如果找到相應節點,或者已經遍歷到尾節點就跳出循環
104 while(p1->num!=num&&p1->next!=NULL)
105 {
106 p2=p1;
107 p1=p1->next;
108 }
109 //判斷是否找到相應節點
110 if(p1->num==num)
111 {
112 //要刪除的節點是不是鏈表的第一個節點
113 //如果是,就將頭指針指向該節點的後一個節點
114 //如果不是,就將該節點的前一個節點的指針指向該節點的後一個節點
115 if(head==p1)
116 {
117 head=p1->next;
118 }
119 else
120 {
121 p2->next=p1->next;
122 }
123 n=n-1;
124 printf("%ld 節點已刪除.\n",num);
125 }
126 else
127 {
128 printf("鏈表中沒有要刪除的元素.\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 //判斷鏈表是否爲空,如果是空鏈表,就將新節點作爲第一個節點
142 if(NULL==head)
143 {
144 head=p0;
145 p0->next=NULL;
146 }
147 else
148 {
149 //遍歷每一個節點中的學號,與新學號比較大小
150 //如果找到一個學號比新學號大,就將新學號的節點插入它之前
151 //如果尾節點的學號仍比新學號小,就將新節點插入到鏈表尾部
152 while((p0->num > p1->num)&&(p1->next!=NULL))
153 {
154 p2=p1;
155 p1=p1->next;
156 }
157 //找到一個比新學號大的節點
158 if(p0->num <= p1->num)
159 {
160 //判斷該節點是否爲頭節點,如果是,則將新節點設置爲頭節點
161 if(p1==head)
162 {
163 head=p0;
164 }
165 else
166 {
167 p2->next=p0;
168 }
169 p0->next=p1;
170 }
171 else
172 {
173 p1->next=p0;
174 p0->next=NULL;
175 }
176 }
177 //鏈表長度加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("請輸入要刪除的學號:");
191 scanf("%ld",&num);
192 while(num!=0)
193 {
194 head=Delete(head,num);
195 Print(head);
196 printf("請輸入要刪除的學號:");
197 scanf("%ld",&num);
198 }
199 printf("請輸入要插入的節點:");
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("請輸入要插入的節點:");
206 stu=(struct student *)malloc(LEN);
207 scanf("%ld %s",&stu->num,stu->name);
208 }
209 Print(head);
210 }
mingw5編譯通過,鏈表結構是數據結構中的基礎,掌握鏈表的邏輯,存儲結構和基本操作,並能自己用代碼實現,將有助於對後續複雜數據結構和算法的學習!