学生成绩双链表


  1. #include<iostream>
  2. using namespace std;
  3. template < class student>
  4. class Node
  5. {
  6. public:
  7. student data;
  8. Node<student> *prior;
  9. Node<student> *next;
  10. };
  11. template < class student>
  12. class DLL
  13. {
  14. public:
  15. DLL(); //无参的构造函数
  16. DLL(student score[], int n); //带参的构造函数
  17. ~DLL(); //析构函数
  18. int Length(); //返回单链表长度
  19. void insert(int i, student x); //插入操作,在位置i插入元素
  20. student get(int i); //按位查找
  21. int locate(student x); //按值查找
  22. student Delete(int i); //删除操作
  23. void print(); //遍历操作
  24. private:
  25. Node<student> *first; //双链表的头指针
  26. int length; //链的长度计数
  27. };
  28. template < class student>
  29. DLL<student>::DLL(student score[], int n)
  30. {
  31. length= 0;
  32. first= new Node<student>;
  33. first->next= NULL;
  34. first->prior= NULL;
  35. for ( int i= 0;i<n;i++)
  36. {
  37. Node<student> *s= new Node<student>;
  38. s->data=score[i];
  39. s->next=first->next;
  40. first->next = s;
  41. }
  42. }
  43. template < class student>
  44. DLL<student>::~DLL()
  45. {
  46. while (first->next!=first->prior)
  47. {
  48. //临时指针,存储即将释放的节点的指针
  49. Node<student> *temp=first;
  50. //脱链
  51. first->prior->next=first->next;
  52. first->next ->prior=first->prior;
  53. //头指针后移
  54. first=first->next;
  55. //释放内存
  56. delete temp;
  57. }
  58. delete first;
  59. }
  60. template< class student>
  61. int DLL<student>::Length()
  62. {
  63. Node<student> *p;
  64. int count;
  65. p=first->next;
  66. count= 0;
  67. while(p!= NULL)
  68. {
  69. p=p->next;
  70. count++;
  71. }
  72. return length;
  73. }
  74. template < class student>
  75. void DLL<student>::insert( int i,student x)
  76. {
  77. Node<student>*p,*s;
  78. int count;
  79. p=first;
  80. count= 0;
  81. while(p!= NULL&&count<i -1)
  82. {
  83. p=p->next;
  84. count++;
  85. }
  86. if(p== NULL) throw "位置";
  87. else
  88. {
  89. s= new Node<student>;
  90. s->data=x;
  91. s->next=p->next;
  92. p->next=s;
  93. }
  94. }
  95. template < class student>
  96. student DLL<student>::get( int i)
  97. {
  98. Node<student> *p; int count;
  99. count= 1;
  100. p=first->next;
  101. while (p!= NULL&&count<i)
  102. {
  103. p = p->next;
  104. count++;
  105. }
  106. if (p == NULL) throw "位置非法";
  107. else return p->data;
  108. }
  109. template < class student>
  110. int DLL<student>::locate(student x)
  111. {
  112. Node<student> *p;
  113. int count;
  114. p=first->next;
  115. count= 1;
  116. while(p!= NULL)
  117. {
  118. if(p->data==x) return count;
  119. p=p->next;
  120. count++;
  121. }
  122. return 0;
  123. }
  124. template < class student>
  125. student DLL<student>::Delete( int i)
  126. {
  127. Node<student> *p,*q;
  128. p=first->next;
  129. int count, x;
  130. count= 1;
  131. while (p!= NULL&&count<i -1)
  132. {
  133. p=p->next;
  134. count++;
  135. }
  136. if (p== NULL||p->next== NULL) throw "位置非法";
  137. else
  138. {
  139. q=p->next;
  140. x=q->data;
  141. if (p->next!= NULL)
  142. {
  143. if(q->next!= NULL)
  144. q->next->prior=p;
  145. else
  146. {
  147. p->next= NULL;
  148. p->next=q->next;
  149. delete q;
  150. q= NULL;
  151. return x;
  152. }
  153. }
  154. p->next=q->next;
  155. delete q;
  156. q= NULL;
  157. return x;
  158. }
  159. }
  160. template < class student>
  161. void DLL<student>::print()
  162. {
  163. Node<student> *p;
  164. p=first->next;
  165. while(p->next!= NULL)
  166. {
  167. cout<<p->data<< " ";
  168. p=p->next;
  169. }
  170. cout<<p->data<< endl;
  171. }
  172. int main()
  173. {
  174. float score[ 8] = { 72, 88, 57, 83.5, 32.5, 68, 96, 86.5};
  175. DLL< float>Student(score, 8);
  176. cout<< "初始成绩如下:"<< endl;
  177. Student.print();
  178. cout<< endl<< "在学生3插入成绩86,插入后结果如下:"<< endl;
  179. Student.insert( 3, 86);
  180. Student.print();
  181. cout<< endl<< "在学生5删除成绩为:"<<Student.Delete( 5)<< " , "<< "删除后结果如下:"<< endl;
  182. Student.print();
  183. cout<< endl<< "位置6的成绩为:"<<Student.get( 6)<< endl;
  184. cout<< endl<< "成绩72所在位置为:"<<Student.locate( 72)<< endl<< endl;
  185. cout<< "最终数据如下:"<< endl;
  186. Student.print();
  187. cout<< endl;
  188. return 0;
  189. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值