找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4147|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

C++語言 線性表的插入和刪除

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:71259 發(fā)表于 2014-12-30 01:28 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
本帖最后由 daming 于 2014-12-30 02:14 編輯


  1. #include<iostream.h>

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

  7. //建立一條升序鏈表 (尾插法:最后建立的結(jié)點(diǎn)是表頭,故應(yīng)降序輸入)
  8. node *creat_sort()
  9. {
  10. node *p1,*head=NULL;
  11. int a;
  12. cout<<"建立一條有序鏈表,請輸入數(shù)據(jù),以-1結(jié)束:\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. //輸出鏈表上各個結(jié)點(diǎn)的值
  24. void print(const node *head)
  25. {
  26. const node *p;
  27. p=head;
  28. cout<<"鏈表上各個結(jié)點(diǎn)的數(shù)據(jù)為:\n";
  29. while(p!=NULL){
  30.   cout<<p->data<<'\t';
  31.   p=p->next;
  32. }
  33. cout<<endl;
  34. }

  35. //刪除鏈表上具有指定值的一個結(jié)點(diǎn)
  36. node *delete_one_node(node *head,int num)
  37. {
  38. node *p1,*p2;
  39. if(head==NULL){
  40.   cout<<"鏈表為空,無結(jié)點(diǎn)可刪!\n";
  41.   return NULL;
  42. }
  43. if(head->data==num){
  44.   p1=head;
  45.   head=head->next;
  46.   delete p1;
  47.   cout<<"刪除了一個結(jié)點(diǎn)!\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<<"刪除了一個結(jié)點(diǎn)!\n";
  59.   }
  60.   else
  61.    cout<<num<<"鏈表上沒有找到要刪除的結(jié)點(diǎn)!\n";
  62. }
  63. return head;
  64. }

  65. //釋放鏈表的結(jié)點(diǎn)空間
  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<<"已釋放鏈表的結(jié)點(diǎn)空間!\n";
  75. }

  76. //求鏈表的結(jié)點(diǎn)數(shù)
  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個結(jié)點(diǎn)
  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個結(jié)點(diǎn)
  103. node *delete_k_node(node *head,int k)
  104. {
  105. int j=1;
  106. node *p,*p1;
  107. if(head==NULL){
  108.   cout<<"鏈表為空,無結(jié)點(diǎn)可刪!\n";
  109.   return NULL;
  110. }
  111. p=head;
  112. if(k==1){
  113.   p=head;
  114.   head=head->next;
  115.   delete p;
  116.   cout<<"刪除了第一個結(jié)點(diǎn)!\n";
  117. }
  118. else{
  119.   p=find(head,k-1); //查找第k-1個結(jié)點(diǎn),并由p指向該結(jié)點(diǎn)
  120.   if(p->next!=NULL){
  121.    p1=p->next;
  122.    p->next=p1->next;
  123.    delete p1;
  124.    cout<<"刪除了第"<<k<<"個結(jié)點(diǎn)!\n";
  125.   }
  126. }
  127. return head;
  128. }

  129. //插入一個結(jié)點(diǎn),不改變鏈表上的升序關(guān)系
  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. //測試函數(shù)
  146. void main()
  147. {
  148. node *head;
  149. int num;
  150. head=creat_sort();
  151. print(head);
  152. cout<<"結(jié)點(diǎn)數(shù):"<<count(head)<<endl;
  153. cout<<"輸入要刪除結(jié)點(diǎn)上的序號!\n";
  154. cin>>num;
  155. head=delete_k_node(head,num);
  156. print(head);
  157. cout<<"輸入要刪除結(jié)點(diǎn)上的整數(shù)!\n";
  158. cin>>num;
  159. head=delete_one_node(head,num);
  160. print(head);
  161. cout<<"輸入要插入的整數(shù)!\n";
  162. cin>>num;
  163. head=insert(head,num);
  164. print(head);
  165. deletechain(head);
  166. }

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

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


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表