fork download
  1. #include <iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. struct student//定义结构体变量;
  5. {
  6. long int num;
  7. double score;
  8. student *next;
  9. };
  10. student *creatlink()//创建链表节点
  11. {
  12. student *p1,*p2,*p3,*head=NULL;//初始化头指针;
  13. p1=new student;//动态申请内存,
  14. cin>>p1->num>>p1->score;//输入结构体中的内容(为了好以0 0结束创建节点)
  15. if(p1->num!=0&&p1->score!=0)
  16. {
  17. head=p1;
  18. while(p1->num!=0&&p1->score!=0)//判断是不是0 0;
  19. {
  20. p2=new student;//再次申请内存。
  21. p1->next=p2;
  22. p3=p1;
  23. cin>>p2->num>>p2->score;//输入数据
  24. p1=p2;
  25. }
  26. p3->next=NULL;//尾指针置空。
  27. }
  28. else
  29. head=NULL;
  30. return head;
  31. }
  32. student *dellink(student *head,long m)
  33. {
  34. student *p=head,*p1;
  35. while(p!=NULL)
  36. {
  37. if((p->num==m)&&(p==head))//如果要删除的节点在头上。
  38. {
  39. head=p->next;
  40. break;
  41. }
  42. else if((p->num==m)&&(p->next==NULL))//删除的节点在结尾
  43. {
  44. p=NULL;
  45. }
  46. else
  47. {
  48. if(p->num!=m)//要删的节点在中间时判断是否是要删的节点
  49. {
  50. p1=p;
  51. p=p->next;
  52. }
  53. else
  54. {
  55. p1->next=p->next;
  56. break;
  57. }
  58. }
  59. }
  60. return head;
  61. }
  62. student *insertlink(student *head,student *stu)
  63. {
  64. student *p,*p1,*p3,*p2;
  65. p=head;
  66. p2=new student;//申请动态内存,为了将新的节点地址变化(如果不变第二次插入式会出错)
  67. //cout<<p2<<endl;
  68. p2->num=stu->num;//赋值,(也可以用结构体直接赋值)
  69. p2->score=stu->score;
  70. p2->next=stu->next;
  71. p1=p2;
  72. if(head==NULL)//判断链表是否为空
  73. {
  74. head=p1;
  75. p1->next=NULL;
  76. }
  77. else
  78. {
  79. while(p->next->next!=NULL)
  80. {
  81. if((p->num<=p1->num)&&(p->next->num>=p1->num))//判断是否是要插入的位置(此处要求输入是按升序输入的,插入时按升序插入)
  82. {
  83. p3=p->next;
  84. p->next=p1;
  85. p1->next=p3;
  86. break;
  87. }
  88. p=p->next;
  89. }
  90. //cout<<p->next->next<<endl;
  91. if((p->num<=p1->num)&&(p->next->num>=p1->num))//由于判断结束的标志是p->next->next,所以还有两组数据还没比较。
  92. {
  93. p3=p->next;
  94. p->next=p1;
  95. p1->next=p3;
  96. }
  97. else if(p->next->num<=p1->num)
  98. {
  99. p->next->next=p1;
  100. p1->next=NULL;
  101. }
  102. }
  103. return head;
  104. }
  105. void printlink(student *head)
  106. {
  107. student *p=head;
  108. while(p->next!=NULL)
  109. {
  110. cout<<p->num<<" "<<p->score<<endl;
  111. p=p->next;
  112. }
  113. cout<<p->num<<" "<<p->score<<endl;
  114. }
  115. void freelink(student *head)
  116. {
  117. student *p=head,*p1;
  118. while(p!=NULL)
  119. {
  120. p1=p->next;//存储下一个指针的内容
  121. delete(p);//释放p的内存
  122. p=p1;
  123. }
  124. delete(p);
  125. }
  126. int main()
  127. {
  128. student *creatlink(void);
  129. student *dellink(student *,long);
  130. student *insertlink(student *,student *);
  131. void printlink(student *);
  132. void freelink(student *);
  133. student *head,stu;
  134. long del_num;
  135. head=creatlink();
  136. cin>>del_num;
  137. head=dellink(head,del_num);
  138. cin>>stu.num>>stu.score;
  139. head=insertlink(head,&stu);
  140. cin>>stu.num>>stu.score;
  141. head=insertlink(head,&stu);
  142. cout<<setiosflags(ios::fixed);
  143. cout<<setprecision(2);
  144. printlink(head);
  145. freelink(head);
  146. return 0;
  147. }
Success #stdin #stdout 0s 15240KB
stdin
1 2
3 4
5 6
1
stdout
1 2.00
3 4.00
5 6.00
142 0.00
142 0.00