久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: C++語言 線性表的插入和刪除 [打印本頁]

作者: daming    時間: 2014-12-30 01:28
標題: C++語言 線性表的插入和刪除
本帖最后由 daming 于 2014-12-30 02:14 編輯


  1. #include<iostream.h>

  2. struct node
  3. {
  4. int data;
  5. node *next;
  6. };

  7. //建立一條升序鏈表 (尾插法:最后建立的結點是表頭,故應降序輸入)
  8. node *creat_sort()
  9. {
  10. node *p1,*head=NULL;
  11. int a;
  12. cout<<"建立一條有序鏈表,請輸入數據,以-1結束:\n";
  13. cin>>a;
  14. while(a!=-1){
  15.   p1=new node;
  16.   p1->data=a;
  17.   p1->next=head;
  18.   head=p1;
  19.   cin>>a;
  20. }
  21. return head;
  22. }

  23. //輸出鏈表上各個結點的值
  24. void print(const node *head)
  25. {
  26. const node *p;
  27. p=head;
  28. cout<<"鏈表上各個結點的數據為:\n";
  29. while(p!=NULL){
  30.   cout<<p->data<<'\t';
  31.   p=p->next;
  32. }
  33. cout<<endl;
  34. }

  35. //刪除鏈表上具有指定值的一個結點
  36. node *delete_one_node(node *head,int num)
  37. {
  38. node *p1,*p2;
  39. if(head==NULL){
  40.   cout<<"鏈表為空,無結點可刪!\n";
  41.   return NULL;
  42. }
  43. if(head->data==num){
  44.   p1=head;
  45.   head=head->next;
  46.   delete p1;
  47.   cout<<"刪除了一個結點!\n";
  48. }
  49. else{
  50.   p2=p1=head;
  51.   while(p2->data!=num&&p2->next!=NULL){
  52.    p1=p2;
  53.    p2=p2->next;
  54.   }
  55.   if(p2->data==num){
  56.    p1->next=p2->next;
  57.    delete p2;
  58.    cout<<"刪除了一個結點!\n";
  59.   }
  60.   else
  61.    cout<<num<<"鏈表上沒有找到要刪除的結點!\n";
  62. }
  63. return head;
  64. }

  65. //釋放鏈表的結點空間
  66. void deletechain(node *h)
  67. {
  68. node *p1;
  69. while(h){
  70.   p1=h;
  71.   h=h->next;
  72.   delete p1;
  73. }
  74. cout<<"已釋放鏈表的結點空間!\n";
  75. }

  76. //求鏈表的結點數
  77. int count(node *head)
  78. {
  79. int n;
  80. node *p;
  81. p=head;
  82. n=0;
  83. while(p!=NULL){
  84.   n=n+1;
  85.   p=p->next;
  86. }
  87. return n;
  88. }
  89. ////查找第k個結點
  90. node *find(node *head,int k)
  91. {
  92. int i;
  93. node *p;
  94. i=1;
  95. p=head;
  96. while(i<k){
  97.   i++;
  98.   p=p->next;
  99. }
  100. return p;
  101. }
  102. //刪除鏈表上第K個結點
  103. node *delete_k_node(node *head,int k)
  104. {
  105. int j=1;
  106. node *p,*p1;
  107. if(head==NULL){
  108.   cout<<"鏈表為空,無結點可刪!\n";
  109.   return NULL;
  110. }
  111. p=head;
  112. if(k==1){
  113.   p=head;
  114.   head=head->next;
  115.   delete p;
  116.   cout<<"刪除了第一個結點!\n";
  117. }
  118. else{
  119.   p=find(head,k-1); //查找第k-1個結點,并由p指向該結點
  120.   if(p->next!=NULL){
  121.    p1=p->next;
  122.    p->next=p1->next;
  123.    delete p1;
  124.    cout<<"刪除了第"<<k<<"個結點!\n";
  125.   }
  126. }
  127. return head;
  128. }

  129. //插入一個結點,不改變鏈表上的升序關系
  130. node *insert(node *head,int num)
  131. {
  132. node *p1,*p2,*p3;
  133. p1=head;
  134. p2=head->next;
  135. while(!(((p1->data)<=num)&&((p2->data)>=num))){
  136.   p1=p1->next;
  137.   p2=p2->next;
  138. }
  139. p3=new node;
  140. p3->data=num;
  141. p1->next=p3;
  142. p3->next=p2;
  143. return head;
  144. }

  145. //測試函數
  146. void main()
  147. {
  148. node *head;
  149. int num;
  150. head=creat_sort();
  151. print(head);
  152. cout<<"結點數:"<<count(head)<<endl;
  153. cout<<"輸入要刪除結點上的序號!\n";
  154. cin>>num;
  155. head=delete_k_node(head,num);
  156. print(head);
  157. cout<<"輸入要刪除結點上的整數!\n";
  158. cin>>num;
  159. head=delete_one_node(head,num);
  160. print(head);
  161. cout<<"輸入要插入的整數!\n";
  162. cin>>num;
  163. head=insert(head,num);
  164. print(head);
  165. deletechain(head);
  166. }

  167. /**********************************************

  168. 建立一條有序鏈表,請輸入數據,以-1結束:
  169. 9 8 6 5 4 3 2 1 -1
  170. 鏈表上各個結點的數據為:
  171. 1       2       3       4       5       6       8       9
  172. 結點數:8
  173. 輸入要刪除結點上的序號!
  174. 1
  175. 刪除了第一個結點!
  176. 鏈表上各個結點的數據為:
  177. 2       3       4       5       6       8       9
  178. 輸入要刪除結點上的整數!
  179. 2
  180. 刪除了一個結點!
  181. 鏈表上各個結點的數據為:
  182. 3       4       5       6       8       9
  183. 輸入要插入的整數!
  184. 7
  185. 鏈表上各個結點的數據為:
  186. 3       4       5       6       7       8       9
  187. 已釋放鏈表的結點空間!
  188. Press any key to continue
  189. **************************************/
復制代碼







歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 丁香六月综合 | 日韩精品视频免费在线观看 | 久草福利资源 | 欧美日韩国 | 欧洲av网站 | www.久久久久 | 一级片免费在线观看 | 欧洲精品一区二区 | 欧美福利一区二区 | 国产精品一区在线播放 | 午夜影院在线免费观看 | 国产成人精品免费 | 亚洲综合免费 | 午夜久久精品 | 国产一级片视频 | 国产高清91 | 日韩精品一二区 | 欧美不卡在线 | 日韩在线免费观看视频 | 国产精品亚洲精品 | 国产欧美日韩在线 | 国产区一区二区 | 一区二区三区高清 | www4h| 特级黄色大片 | 日韩在线播放视频 | 欧美91| 久久久久成人网 | 国产欧美一区二区精品性色超碰 | 在线小视频 | 秋霞啪啪片 | 亚洲亚洲人成综合网络 | 中文字幕色哟哟 | 99热精品在线观看 | 欧美成人小视频 | 天天干天天干天天干 | 93久久精品日日躁夜夜躁欧美 | 亚洲精品中文字幕乱码三区91 | av手机在线免费观看 | 蜜桃综合网| 九色精品 |