双向循环列表使用范例

  1. 转载注明出处:http://blog.sina.com.cn/s/blog_8795b0970101jj27.html
  2. #include
  3. #include
  4. #include
  5. //#include
  6. #define N 100
  7. //#define CHIN
  8. typedef struct student
  9. {
  10. int studentID;
  11. char name[N];
  12. int age;
  13. int sex;
  14. }STUDENT;
  15. typedef struct score
  16. {
  17. int chinese;
  18. int math;
  19. int english;
  20. int average;
  21. int total;
  22. }SCORE;
  23. typedef struct node
  24. {
  25. STUDENT stu;
  26. SCORE sc;
  27. struct node *next;
  28. struct node *pione;
  29. }NODE;
  30. extern NODE * mycreate();
  31. extern NODE * myinsert(NODE *,STUDENT *,SCORE *);
  32. extern void mycopy(NODE *,STUDENT *,SCORE *);
  33. extern void myprint(NODE *);
  34. extern NODE * myremove(NODE **,int);
  35. extern int Doublelinklistlen(NODE *);
  36. //extern NODE * mysearch(NODE *);
  37. //extern NODE * mysort(NODE *);
  38. extern void display(NODE *);
  39. extern void addstudent(NODE *);
  40. extern void lookover(NODE *);
  41. extern void deletestudent(NODE *);
  42. extern NODE * mysearch(NODE *,int);
  43. extern void searchinfo(NODE *);
  44. //extern NODE *mysort(NODE *);
  45. //extern void sortinfo(NODE *);
  46. extern void printnode(NODE *);


StudentManage.c:

[csharp]  view plain copy print ?
  1. #include "StudentManage.h"
  2. void mycopy(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));
  17. head->stu.studentID = 0;
  18. head->next = head;
  19. head->pione = head;
  20. return head;
  21. }
  22. NODE * myinsert(NODE *head,STUDENT *student,SCORE *score)
  23. {
  24. if(head->stu.studentID == 0)
  25. {
  26. mycopy(head,student,score);
  27. }
  28. else
  29. {
  30. NODE *last = head;
  31. while(last->next != head)
  32. {
  33. last = last->next;
  34. }
  35. NODE *p = (NODE *)malloc(sizeof(NODE));
  36. memset(p,0,sizeof(p));
  37. mycopy(p,student,score);
  38. head->pione = p;
  39. last->next = p;
  40. p->next = head;
  41. p->pione = last;
  42. }
  43. return head;
  44. }
  45. void myprint(NODE *head)
  46. {
  47. NODE *q = (NODE *)malloc(sizeof(NODE));
  48. q = head;
  49. int i = 1;
  50. while(q->next != head)
  51. //while(i < Doublelinklistlen(head))
  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. void printnode(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. }
  75. int Doublelinklistlen(NODE *head)
  76. {
  77. int len = 0;
  78. NODE *q = head;
  79. while(q->next != head)
  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));
  90. q = *head;
  91. int j = 0;
  92. while(q->stu.studentID != studentID)
  93. {
  94. j++;
  95. q = q->next;
  96. if(j >= Doublelinklistlen(*head))
  97. {
  98. #ifdef __DDD__
  99. printf("No Such Student has The studentID!\n");
  100. printf("Delete Student Failure!\n");
  101. #endif
  102. return *head;
  103. }
  104. }
  105. NODE *pre = q->pione;
  106. NODE *ne = q->next;
  107. //pre->next = ne;
  108. //ne->pione = pre;
  109. if(q == *head)
  110. {
  111. (*head) = (*head)->next;
  112. pre->next = (*head);
  113. (*head)->pione = pre;
  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 Student Success!\n");
  126. #endif
  127. return (*head);
  128. }
  129. NODE * mysearch(NODE *head,int info)
  130. {
  131. NODE *q = head;
  132. int j = 0;
  133. #if 0
  134. int count = 0;
  135. for(i = 0; i < Doublelinklistlen(head); i++)
  136. {
  137. if((num == 1) && (q->stu.studentID == (int)info))
  138. {
  139. printnode(q);
  140. count++;
  141. continue;
  142. }
  143. else if((num == 2) && (strcpy(q->stu.name,(char *)info) == 0))
  144. {
  145. printnode(q);
  146. count++;
  147. continue;
  148. }
  149. else if((num == 3) && (q->stu.age == (int)info))
  150. {
  151. printnode(q);
  152. count++;
  153. continue;
  154. }
  155. else if((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. {
  169. printf("No infomation about you input!\n");
  170. }
  171. #endif
  172. #if 1
  173. while(q->stu.studentID != info)
  174. {
  175. j++;
  176. q = q->next;
  177. if(j > Doublelinklistlen(head))
  178. {
  179. printf("No This Student!\n");
  180. return NULL;
  181. }
  182. }
  183. //printnode(q);
  184. return q;
  185. #endif
  186. }
  187. #if 0
  188. NODE * mysort(NODE *head)
  189. {
  190. NODE *newhead = mycreate();
  191. NODE *newlast = newhead;
  192. NODE *q = head;
  193. NODE *max = head;
  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__);
  208. if(max == head)
  209. {
  210. printf("%d\n",__LINE__);
  211. NODE * ne = head->next;
  212. NODE * pre = head->pione;
  213. head = head->next;
  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. }
  227. if(newhead->stu.studentID == 0)
  228. {
  229. printf("%d\n",__LINE__);
  230. newhead = max;
  231. newlast = max;
  232. printf("%d\n",__LINE__);
  233. }
  234. else
  235. {
  236. newlast->next = max;
  237. max->pione = newlast;
  238. max->next = newhead;
  239. newhead->pione = max;
  240. newlast = max;
  241. }
  242. q = q->next;
  243. }
  244. return newhead;
  245. }
  246. #endif

Display.c:

[csharp]  view plain copy print ?
  1. #include "StudentManage.h"
  2. void addstudent(NODE *head)
  3. {
  4. STUDENT stu;
  5. SCORE sc;
  6. printf("Please Input Student's StudentID(12XXX):");
  7. scanf("%d",&stu.studentID);
  8. printf("Please Input Student's Name:");
  9. scanf("%s",stu.name);
  10. printf("Please Input Student's Age:");
  11. scanf("%d",&stu.age);
  12. printf("Please Input Student's Sex(1:男 0:女):");
  13. scanf("%d",&stu.sex);
  14. printf("Please Input Student's Chinese Score:");
  15. scanf("%d",&sc.chinese);
  16. printf("Please Input Student's Math Score:");
  17. scanf("%d",&sc.math);
  18. printf("Please Input Student's English Score:");
  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 New Student:\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 To Add %d:%s ??(Y / N)",stu.studentID,stu.name);
  27. char ch;
  28. scanf("\n%c",&ch);
  29. if(ch == 'Y')
  30. {
  31. head = myinsert(head,&stu,&sc);
  32. printf("Add Student Success!\n");
  33. }
  34. printf("Press Enter To Continue...\n");
  35. getchar();
  36. getchar();
  37. }
  38. void lookover(NODE *head)
  39. {
  40. printf("The Infomation Of Student :\n");
  41. myprint(head);
  42. printf("Press Enter To Continue...");
  43. getchar();
  44. getchar();
  45. }
  46. void deletestudent(NODE *head)
  47. {
  48. int id;
  49. #define __DDD__
  50. printf("Please Input The StudentID Of Student You Want To Delete(12XXX):");
  51. scanf("%d",&id);
  52. NODE *p = mysearch(head,id);
  53. printf("The Infomation New Student:\n");
  54. printnode(p);
  55. printf("Are You Sure To Add %d:%s ??(Y / N)",p->stu.studentID,p->stu.name);
  56. char ch;
  57. scanf("\n%c",&ch);
  58. if(ch == 'Y')
  59. {
  60. //head = myinsert(head,&stu,&sc);
  61. //printf("Add Student Success!\n");
  62. //char ch;
  63. head = myremove(&head,id);
  64. }
  65. printf("Press Enter To Continue...");
  66. getchar();
  67. getchar();
  68. }
  69. void searchinfo(NODE *head)
  70. {
  71. int n;
  72. //#define __DDD__
  73. printf("Please Input StudentID You Want To Search(12XXX):");
  74. scanf("%d",&n);
  75. NODE *p = mysearch(head,n);
  76. printnode(p);
  77. #if 0
  78. int n;
  79. int info;
  80. char name[N];
  81. printf("\n");
  82. printf("\n");
  83. printf("\n");
  84. printf("\n");
  85. printf("\n");
  86. printf("\n");
  87. printf("Please input what you want to search:");
  88. scanf("%d",&n);
  89. while((n > 4) || (n < 1))
  90. {
  91. printf("Your input must be 1-4!\n");
  92. printf("Please input again:");
  93. scanf("%d",&n);
  94. }
  95. if(n == 1)
  96. {
  97. printf("Please input StudeentID you want to search:");
  98. scanf("%d",&info);
  99. mysearch(head,1,info);
  100. }
  101. if(n == 2)
  102. {
  103. printf("Please input StudeentName you want to search:");
  104. scanf("%s",name);
  105. mysearch(head,2,name);
  106. }
  107. if(n == 3)
  108. {
  109. printf("Please input StudeentAge you want to search:");
  110. scanf("%d",&info);
  111. mysearch(head,3,info);
  112. }
  113. if(n == 4)
  114. {
  115. printf("Please input StudeentSex you want to search:");
  116. scanf("%d",&info);
  117. mysearch(head,4,info);
  118. }
  119. #endif
  120. printf("Press Enter To Continue...");
  121. getchar();
  122. getchar();
  123. }
  124. void display(NODE *head)
  125. {
  126. int choose;
  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");
  138. printf("Please Input Your Choose:");
  139. scanf("%d",&choose);
  140. while((choose > 5) || (choose < 1))
  141. {
  142. printf("Your Choose Must Be 1 To 5!\n");
  143. printf("Please Input Your Choose Again:");
  144. scanf("%d",&choose);
  145. }
  146. switch(choose)
  147. {
  148. case 1:
  149. {
  150. system("clear");
  151. lookover(head);
  152. break;
  153. }
  154. case 2:
  155. {
  156. system("clear");
  157. addstudent(head);
  158. break;
  159. }
  160. case 3:
  161. {
  162. system("clear");
  163. deletestudent(head);
  164. break;
  165. }
  166. case 4:
  167. {
  168. system("clear");
  169. searchinfo(head);
  170. break;
  171. }
  172. case 5:
  173. {
  174. system("clear");
  175. exit(-1);
  176. }
  177. default:
  178. {
  179. break;
  180. }
  181. }
  182. }
  183. }


main.c:

[csharp]  view plain copy print ?
  1. #include "StudentManage.h"
  2. int main()
  3. {
  4. NODE *head = mycreate();
  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;
  43. head = myinsert(head,&stu1,&sc1);
  44. head = myinsert(head,&stu2,&sc2);
  45. head = myinsert(head,&stu3,&sc3);
  46. head = myinsert(head,&stu4,&sc4);
  47. display(head);
  48. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值