嵌入式 双向循环列表使用范例

472人阅读 评论(0)

1. #include
2. #include
3. #include
4. //#include
5. #define N 100
6. //#define CHIN
7. typedef structstudent
8. {
9. intstudentID;
10. charname[N];
11. intage;
12. intsex;
13. }STUDENT;
14. typedef structscore
15. {
16. intchinese;
17. intmath;
18. intenglish;
19. intaverage;
20. inttotal;
21. }SCORE;
22. typedef structnode
23. {
24. STUDENT stu;
25. SCORE sc;
26. struct node*next;
27. structnode *pione;
28. }NODE;
29. extern NODE *mycreate();
30. externNODE * myinsert(NODE *,STUDENT *,SCORE *);
31. extern void mycopy(NODE *,STUDENT *,SCORE*);
32. externvoid myprint(NODE*);
33. extern NODE *myremove(NODE **,int);
35. //extern NODE * mysearch(NODE*);
36. //extern NODE *mysort(NODE *);
37. extern void display(NODE *);
39. extern void lookover(NODE *);
40. externvoid deletestudent(NODE*);
41. extern NODE *mysearch(NODE *,int);
42. externvoid searchinfo(NODE*);
43. //extern NODE *mysort(NODE*);
44. //extern voidsortinfo(NODE *);
45. extern void printnode(NODE *);
#include
#include
#include
//#include

#define N 100
//#define CHIN

typedef struct student
{
int studentID;
char name[N];
int age;
int sex;
}STUDENT;

typedef struct score
{
int chinese;
int math;
int english;
int average;
int total;
}SCORE;

typedef struct node
{
STUDENT stu;
SCORE sc;
struct node *next;
struct node *pione;
}NODE;

extern NODE * mycreate();
extern NODE * myinsert(NODE *,STUDENT *,SCORE *);
extern void mycopy(NODE *,STUDENT *,SCORE *);
extern void myprint(NODE *);
extern NODE * myremove(NODE **,int);
//extern NODE * mysearch(NODE *);
//extern NODE * mysort(NODE *);
extern void display(NODE *);
extern void lookover(NODE *);
extern void deletestudent(NODE *);
extern NODE * mysearch(NODE *,int);
extern void searchinfo(NODE *);
//extern NODE *mysort(NODE *);
//extern void sortinfo(NODE *);
extern void printnode(NODE *);


StudentManage.c:

1. #include"StudentManage.h"
2. voidmycopy(NODE *node,STUDENT *student,SCORE*score)
3. {
4. node->stu.studentID =student->studentID;
5. strcpy(node->stu.name,student->name);
6. node->stu.age =student->age;
7. node->stu.sex = student->sex;
8. node->sc.chinese =score->chinese;
9. node->sc.math = score->math;
10. node->sc.english =score->english;
11. node->sc.average = score->average;
12. node->sc.total =score->total;
13. }
14. NODE * mycreate()
15. {
16. NODE *head = (NODE *)malloc(sizeof(NODE));
21. }
22. NODE * myinsert(NODE *head,STUDENT*student,SCORE *score)
23. {
25. {
27. }
28. else
29. {
32. {
33. last = last->next;
34. }
35. NODE *p = (NODE *)malloc(sizeof(NODE));
36. memset(p,0,sizeof(p));
37. mycopy(p,student,score);
39. last->next = p;
41. p->pione = last;
42. }
44. }
46. {
47. NODE *q = (NODE *)malloc(sizeof(NODE));
49. int i =1;
52. {
53. printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex);
54. printf("Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total);
55. i++;
56. q = q->next;
57. printf("\n");
58. }
59. printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex);
60. printf("Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total);
61. }
62. voidprintnode(NODE *node)
63. {
64. if(node !=NULL)
65. {
66. printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",node->stu.studentID,node->stu.name,node->stu.age,node->stu.sex);
67. printf("Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",node->sc.chinese,node->sc.math,node->sc.english,node->sc.average,node->sc.total);
68. }
69. else
70. {
71. printf("");
72. }
73. printf("\n");
74. }
76. {
77. int len =0;
80. {
81. len++;
82. q = q->next;
83. }
84. return (len +1);
85. }
86. NODE * myremove(NODE **head,int studentID)
87. {
88. //#define__DDD__
89. NODE *q = (NODE *)malloc(sizeof(NODE));
91. int j =0;
92. while(q->stu.studentID !=studentID)
93. {
94. j++;
95. q = q->next;
97. {
98. #ifdef__DDD__
99. printf("No Such Student has ThestudentID!\n");
100. printf("Delete StudentFailure!\n");
101. #endif
103. }
104. }
105. NODE *pre = q->pione;
106. NODE *ne = q->next;
107. //pre->next = ne;
108. //ne->pione =pre;
110. {
114. free(q);
115. q = NULL;
116. }
117. else
118. {
119. pre->next = ne;
120. ne->pione = pre;
121. free(q);
122. q = NULL;
123. }
124. #ifdef__DDD__
125. printf("Delete StudentSuccess!\n");
126. #endif
128. }
129. NODE * mysearch(NODE *head,int info)
130. {
132. int j =0;
133. #if 0
134. int count =0;
136. {
137. if((num ==1) && (q->stu.studentID == (int)info))
138. {
139. printnode(q);
140. count++;
141. continue;
142. }
143. elseif((num == 2) &&(strcpy(q->stu.name,(char*)info) == 0))
144. {
145. printnode(q);
146. count++;
147. continue;
148. }
149. elseif((num == 3) &&(q->stu.age == (int)info))
150. {
151. printnode(q);
152. count++;
153. continue;
154. }
155. elseif((num == 4) &&(q->stu.sex == (int)info))
156. {
157. printnode(q);
158. count++;
159. continue;
160. }
161. else
162. {
163. ;
164. }
165. q = q->next;
166. }
167. if(count== 0)
168. {
170. }
171. #endif
172. #if 1
173. while(q->stu.studentID !=info)
174. {
175. j++;
176. q = q->next;
178. {
179. printf("No ThisStudent!\n");
180. returnNULL;
181. }
182. }
183. //printnode(q);
184. returnq;
185. #endif
186. }
187. #if 0
189. {
194. while(q !=NULL)
195. {
196. printf("%d\n",__LINE__);
197. for(;q->next != head; q = q->next)
198. {
199. printf("%d\n",__LINE__);
200. //sleep(2);
201. if(q->next->sc.total >max->sc.total)
202. {
203. printf("%d\n",__LINE__);
204. max = q->next;
205. }
206. }
207. printf("%d\n",__LINE__);
209. {
210. printf("%d\n",__LINE__);
211. NODE * ne = head->next;
212. NODE * pre = head->pione;
214. ne->pione = pre;
215. pre->next = ne;
216. printf("%d\n",__LINE__);
217. }
218. else
219. {
220. printf("%d\n",__LINE__);
221. NODE *ne = q->next;
222. NODE *pre = q->pione;
223. ne->pione = pre;
224. pre->next = ne;
225. printf("%d\n",__LINE__);
226. }
228. {
229. printf("%d\n",__LINE__);
231. newlast = max;
232. printf("%d\n",__LINE__);
233. }
234. else
235. {
236. newlast->next = max;
237. max->pione = newlast;
240. newlast = max;
241. }
242. q = q->next;
243. }
245. }
246. #endif
#include "StudentManage.h"

void mycopy(NODE *node,STUDENT *student,SCORE *score)
{
node->stu.studentID = student->studentID;
strcpy(node->stu.name,student->name);
node->stu.age = student->age;
node->stu.sex = student->sex;
node->sc.chinese = score->chinese;
node->sc.math = score->math;
node->sc.english = score->english;
node->sc.average = score->average;
node->sc.total = score->total;
}

NODE * mycreate()
{
}

NODE * myinsert(NODE *head,STUDENT *student,SCORE *score)
{
{
}
else
{
{
last = last->next;
}
NODE *p = (NODE *)malloc(sizeof(NODE));
memset(p,0,sizeof(p));
mycopy(p,student,score);
last->next = p;
p->pione = last;
}
}

{
NODE *q = (NODE *)malloc(sizeof(NODE));
int i = 1;
{
printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex);
printf("   Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total);
i++;
q = q->next;
printf("\n");
}
printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex);
printf("   Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total);
}

void printnode(NODE *node)
{
if(node != NULL)
{
printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",node->stu.studentID,node->stu.name,node->stu.age,node->stu.sex);
printf("Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",node->sc.chinese,node->sc.math,node->sc.english,node->sc.average,node->sc.total);
}
else
{
printf(" ");
}
printf("\n");
}

{
int len = 0;
{
len++;
q = q->next;
}
return (len + 1);
}

{
//#define __DDD__
NODE *q = (NODE *)malloc(sizeof(NODE));
int j = 0;
while(q->stu.studentID != studentID)
{
j++;
q = q->next;
{
#ifdef __DDD__
printf("No Such Student has The studentID!\n");
printf("Delete Student Failure!\n");
#endif
}
}
NODE *pre = q->pione;
NODE *ne = q->next;
//pre->next = ne;
//ne->pione = pre;
{
free(q);
q = NULL;
}
else
{
pre->next = ne;
ne->pione = pre;
free(q);
q = NULL;
}
#ifdef __DDD__
printf("Delete Student Success!\n");
#endif
}

{
int j = 0;
#if 0
int count = 0;
{
if((num == 1) && (q->stu.studentID == (int)info))
{
printnode(q);
count++;
continue;
}
else if((num == 2) && (strcpy(q->stu.name,(char *)info) == 0))
{
printnode(q);
count++;
continue;
}
else if((num == 3) && (q->stu.age == (int)info))
{
printnode(q);
count++;
continue;
}
else if((num == 4) && (q->stu.sex == (int)info))
{
printnode(q);
count++;
continue;
}
else
{
;
}
q = q->next;
}
if(count == 0)
{
}
#endif
#if 1
while(q->stu.studentID != info)
{
j++;
q = q->next;
{
printf("No This Student!\n");
return NULL;
}
}
//printnode(q);
return q;
#endif
}
#if 0
{
while(q != NULL)
{
printf("%d\n",__LINE__);
for(; q->next != head; q = q->next)
{
printf("%d\n",__LINE__);
//sleep(2);
if(q->next->sc.total > max->sc.total)
{
printf("%d\n",__LINE__);
max = q->next;
}
}
printf("%d\n",__LINE__);
{
printf("%d\n",__LINE__);
ne->pione = pre;
pre->next = ne;
printf("%d\n",__LINE__);
}
else
{
printf("%d\n",__LINE__);
NODE *ne = q->next;
NODE *pre = q->pione;
ne->pione = pre;
pre->next = ne;
printf("%d\n",__LINE__);
}
{
printf("%d\n",__LINE__);
newlast = max;
printf("%d\n",__LINE__);
}
else
{
newlast->next = max;
max->pione = newlast;
newlast = max;
}
q = q->next;
}
}
#endif


Display.c:

1. #include"StudentManage.h"
3. {
4. STUDENT stu;
5. SCORE sc;
7. scanf("%d",&stu.studentID);
9. scanf("%s",stu.name);
11. scanf("%d",&stu.age);
13. scanf("%d",&stu.sex);
15. scanf("%d",&sc.chinese);
17. scanf("%d",&sc.math);
19. scanf("%d",&sc.english);
20. sc.average = (sc.chinese + sc.math +sc.english) / 3;
21. sc.total = sc.chinese + sc.math + sc.english;
22. system("clear");
23. printf("The Infomation NewStudent:\n");
24. printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",stu.studentID,stu.name,stu.age,stu.sex);
25. printf("Chinese:m\tMath:m\tEnglish:m\n",sc.chinese,sc.math,sc.english);
26. printf("Are You Sure ToAdd %d:%s ？？(Y /N)",stu.studentID,stu.name);
27. charch;
28. scanf("\n%c",&ch);
29. if(ch =='Y')
30. {
33. }
34. printf("Press Enter ToContinue...\n");
35. getchar();
36. getchar();
37. }
39. {
40. printf("The Infomation Of Student:\n");
42. printf("Press Enter ToContinue...");
43. getchar();
44. getchar();
45. }
47. {
48. intid;
49. #define __DDD__
50. printf("Please Input TheStudentID Of Student You Want ToDelete(12XXX):");
51. scanf("%d",&id);
53. printf("The Infomation NewStudent:\n");
54. printnode(p);
55. printf("Are You Sure To Add %d:%s？？(Y /N)",p->stu.studentID,p->stu.name);
56. charch;
57. scanf("\n%c",&ch);
58. if(ch== 'Y')
59. {
62. //char ch;
64. }
65. printf("Press Enter ToContinue...");
66. getchar();
67. getchar();
68. }
70. {
71. intn;
72. //#define __DDD__
73. printf("Please InputStudentID You Want ToSearch(12XXX):");
74. scanf("%d",&n);
76. printnode(p);
77. #if 0
78. intn;
79. intinfo;
80. charname[N];
81. printf("\n");
82. printf("\n");
83. printf("\n");
84. printf("\n");
85. printf("\n");
86. printf("\n");
87. printf("Please inputwhat you want to search:");
88. scanf("%d",&n);
89. while((n> 4) || (n < 1))
90. {
93. scanf("%d",&n);
94. }
95. if(n ==1)
96. {
97. printf("Please inputStudeentID you want to search:");
98. scanf("%d",&info);
100. }
101. if(n ==2)
102. {
103. printf("Please inputStudeentName you want to search:");
104. scanf("%s",name);
106. }
107. if(n ==3)
108. {
109. printf("Please inputStudeentAge you want to search:");
110. scanf("%d",&info);
112. }
113. if(n ==4)
114. {
115. printf("Please inputStudeentSex you want to search:");
116. scanf("%d",&info);
118. }
119. #endif
120. printf("Press Enter ToContinue...");
121. getchar();
122. getchar();
123. }
125. {
126. intchoose;
127. while(1)
128. {
129. system("clear");
130. printf("\n");
131. printf("\n");
132. printf("\n");
133. printf("\n");
134. //printf("\n");
135. printf("\n");
136. printf("\n");
137. printf("\n");
139. scanf("%d",&choose);
140. while((choose > 5)|| (choose < 1))
141. {
142. printf("Your Choose Must Be 1 To5!\n");
144. scanf("%d",&choose);
145. }
146. switch(choose)
147. {
148. case1:
149. {
150. system("clear");
152. break;
153. }
154. case2:
155. {
156. system("clear");
158. break;
159. }
160. case3:
161. {
162. system("clear");
164. break;
165. }
166. case4:
167. {
168. system("clear");
170. break;
171. }
172. case5:
173. {
174. system("clear");
175. exit(-1);
176. }
177. default:
178. {
179. break;
180. }
181. }
182. }
183. }
#include "StudentManage.h"

{
STUDENT stu;
SCORE sc;
scanf("%d",&stu.studentID);
scanf("%s",stu.name);
scanf("%d",&stu.age);
scanf("%d",&stu.sex);
scanf("%d",&sc.chinese);
scanf("%d",&sc.math);
scanf("%d",&sc.english);
sc.average = (sc.chinese + sc.math + sc.english) / 3;
sc.total = sc.chinese + sc.math + sc.english;
system("clear");
printf("The Infomation New Student:\n");
printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",stu.studentID,stu.name,stu.age,stu.sex);
printf("Chinese:m\tMath:m\tEnglish:m\n",sc.chinese,sc.math,sc.english);
printf("Are You Sure To Add %d:%s ？？(Y / N)",stu.studentID,stu.name);
char ch;
scanf("\n%c",&ch);
if(ch == 'Y')
{
}
printf("Press Enter To Continue...\n");
getchar();
getchar();
}

{
printf("The Infomation Of Student :\n");
printf("Press Enter To Continue...");
getchar();
getchar();
}

{
int id;
#define __DDD__
printf("Please Input The StudentID Of Student You Want To Delete(12XXX):");
scanf("%d",&id);
printf("The Infomation New Student:\n");
printnode(p);
printf("Are You Sure To Add %d:%s ？？(Y / N)",p->stu.studentID,p->stu.name);
char ch;
scanf("\n%c",&ch);
if(ch == 'Y')
{

//char ch;
}
printf("Press Enter To Continue...");
getchar();
getchar();
}

{

int n;
//#define __DDD__
printf("Please Input StudentID You Want To Search(12XXX):");
scanf("%d",&n);
printnode(p);
#if 0
int n;
int info;
char name[N];
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("Please input what you want to search:");
scanf("%d",&n);
while((n > 4) || (n < 1))
{
scanf("%d",&n);
}
if(n == 1)
{
printf("Please input StudeentID you want to search:");
scanf("%d",&info);
}
if(n == 2)
{
printf("Please input StudeentName you want to search:");
scanf("%s",name);
}
if(n == 3)
{
printf("Please input StudeentAge you want to search:");
scanf("%d",&info);
}
if(n == 4)
{
printf("Please input StudeentSex you want to search:");
scanf("%d",&info);
}
#endif
printf("Press Enter To Continue...");
getchar();
getchar();
}

{
int choose;
while(1)
{
system("clear");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
//printf("\n");
printf("\n");
printf("\n");
printf("\n");
scanf("%d",&choose);
while((choose > 5) || (choose < 1))
{
printf("Your Choose Must Be 1 To 5!\n");
scanf("%d",&choose);
}
switch(choose)
{
case 1:
{
system("clear");
break;
}
case 2:
{
system("clear");
break;
}
case 3:
{
system("clear");
break;
}
case 4:
{
system("clear");
break;
}
case 5:
{
system("clear");
exit(-1);
}
default:
{
break;
}
}

}
}


main.c:

1. #include"StudentManage.h"
2. intmain()
3. {
5. STUDENT stu1,stu2,stu3,stu4;
6. SCORE sc1,sc2,sc3,sc4;
7. stu1.studentID = 12001;
8. strcpy(stu1.name,"王明");
9. stu1.age = 18;
10. stu1.sex = 1;
11. sc1.chinese = 90;
12. sc1.math = 91;
13. sc1.english = 92;
14. sc1.average = 91;
15. sc1.total = 273;
16. stu2.studentID = 12002;
17. strcpy(stu2.name,"张芳");
18. stu2.age = 19;
19. stu2.sex = 0;
20. sc2.chinese = 80;
21. sc2.math = 81;
22. sc2.english = 82;
23. sc2.average = 81;
24. sc2.total = 243;
25. stu3.studentID = 12003;
26. strcpy(stu3.name,"李强");
27. stu3.age = 19;
28. stu3.sex = 1;
29. sc3.chinese = 70;
30. sc3.math = 71;
31. sc3.english = 72;
32. sc3.average = 71;
33. sc3.total = 213;
34. stu4.studentID = 12004;
35. strcpy(stu4.name,"张小雨");
36. stu4.age = 20;
37. stu4.sex = 0;
38. sc4.chinese = 60;
39. sc4.math = 61;
40. sc4.english = 62;
41. sc4.average = 61;
42. sc4.total = 183;
48. }
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1051570次
• 积分：18321
• 等级：
• 排名：第521名
• 原创：741篇
• 转载：523篇
• 译文：0篇
• 评论：105条
阅读排行
最新评论