數據結構實驗2:C++實現單鏈表類


       太簡單了,直接貼題目然后上代碼。

       題目:

實驗2

2.1 實驗目的

熟練掌握線性表的鏈式存儲結構。

熟練掌握單鏈表的有關算法設計。

根據具體問題的需要,設計出合理的表示數據的鏈式存儲結構,並設計相關算法。

2.2 實驗要求

本次實驗中的鏈表結構指帶頭結點的單鏈表;

單鏈表結構和運算定義,算法的實現以庫文件方式實現,不得在測試主程序中直接實現;

比如存儲、算法實現放入文件:linkedList.h

實驗程序有較好可讀性,各運算和變量的命名直觀易懂,符合軟件工程要求;

程序有適當的注釋。

2.3 實驗任務

編寫算法實現下列問題的求解。

<1>尾插法創建單鏈表,打印創建結果。

<2>頭插法創建單鏈表,打印創建結果。

<3>銷毀單鏈表。

<4>求鏈表長度。

<5>求單鏈表中第i個元素(函數),若不存在,報錯。

實驗測試數據基本要求:

第一組數據:單鏈表長度n≥10,i分別為5,n,0,n+1,n+2

第二組數據:單鏈表長度n=0,i分別為0,2

<6>在第i個結點前插入值為x的結點。

實驗測試數據基本要求:

第一組數據:單鏈表長度n≥10,x=100,  i分別為5,n,n+1,0,1,n+2

第二組數據:單鏈表長度n=0,x=100,i=5

<7>鏈表中查找元素值為x的結點,成功返回結點指針,失敗報錯。

實驗測試數據基本要求:

單鏈表元素為(1,3,6,10,15,16,17,18,19,20)

x=1,17,20,88

<8>刪除單鏈表中第i個元素結點。

實驗測試數據基本要求:

第一組數據:單鏈表長度n≥10,i分別為5,n,1,n+1,0

第二組數據:單鏈表長度n=0, i=5

<9>在一個遞增有序的單鏈表L中插入一個值為x的元素,並保持其遞增有序特性。

實驗測試數據基本要求:

單鏈表元素為(10,20,30,40,50,60,70,80,90,100),

x分別為25,85,110和8

<10>將單鏈表L中的奇數項和偶數項結點分解開(元素值為奇數、偶數),分別放入新的單鏈表中,然后原表和新表元素同時輸出到屏幕上,以便對照求解結果。

實驗測試數據基本要求:

第一組數據:單鏈表元素為(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)

第二組數據:單鏈表元素為(10,20,30,40,50,60,70,80,90,100)

       代碼:

 1 // stdafx.h : include file for standard system include files,  2 // or project specific include files that are used frequently, but  3 // are changed infrequently  4 //  5 
 6 #if !defined(AFX_STDAFX_H__195DB73A_A23D_4A80_A4F5_2F4FC5141CBC__INCLUDED_)
 7 #define AFX_STDAFX_H__195DB73A_A23D_4A80_A4F5_2F4FC5141CBC__INCLUDED_
 8 
 9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 #include <stdc++.h>
14 
15 using namespace std; 16 
17 typedef int elementType; 18 typedef struct node 19 { 20  elementType data; 21     node* next; 22 }LList, *PList; 23 
24 // TODO: reference additional headers your program requires here 25 
26 //{{AFX_INSERT_LOCATION}} 27 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
28 
29 #endif // !defined(AFX_STDAFX_H__195DB73A_A23D_4A80_A4F5_2F4FC5141CBC__INCLUDED_)

 

 

 1 // linkedList1.h: interface for the linkedList class.  2 //  3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_LINKEDLIST1_H__4C3F34C9_D36C_43D6_97CF_A8E55FD6BD7D__INCLUDED_)
 6 #define AFX_LINKEDLIST1_H__4C3F34C9_D36C_43D6_97CF_A8E55FD6BD7D__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include "StdAfx.h"
13 
14 using namespace std; 15 
16 class linkedList 17 { 18 public: 19     linkedList();//構造函數
20     virtual ~linkedList();//析構函數,銷毀單鏈表
21     bool createLinkedListRail( int length );//尾插法構建單鏈表
22     bool createLinkedListFront( int length );//頭插法構建單鏈表
23     void addLinkedListNodeLast( int value );//警告:必須初始化才能使用! 24     //我嘗試判斷調用對象的鏈表是否初始化來作為是否調用該函數的依據,結果失敗:無論如何判斷,總是不能在零節點時插入
25     bool initiateLinkedList();//初始化單鏈表
26     bool isEmpty();//判斷單鏈表是否為空
27     bool getElementByPosition( int pos, int& value );//求單鏈表中第pos個元素(函數),若不存在,報錯
28     bool insertListByPosition( int pos, int value );//在第pos個結點前插入值為value的結點
29     bool getElementByValue( int& pos, int value );//鏈表中查找元素值為x的結點,成功返回結點指針,失敗報錯。
30     bool removeListNodeByPosition( int pos, int& value );//刪除單鏈表中第pos個元素結點
31     bool insertListSort( int value );//在一個遞增有序的單鏈表L中插入一個值為value的元素,並保持其遞增有序特性
32     bool oddEvenSort( linkedList& LA,linkedList& LB );//將調用單鏈表中的元素按奇偶性分配給被調用的單鏈表LA與LB
33     void printLinkedList();//打印單鏈表
34     int linkedListLength();//返回單鏈表長度
35 private: 36     LList *head; 37     int len; 38 }; 39 
40 #endif // !defined(AFX_LINKEDLIST1_H__4C3F34C9_D36C_43D6_97CF_A8E55FD6BD7D__INCLUDED_)

 

 1 // linkedList1.cpp: implementation of the linkedList class.  2 //  3 //////////////////////////////////////////////////////////////////////
 4 
 5 #include "stdafx.h"
 6 #include "linkedList1.h"
 7 
 8 
 9 //////////////////////////////////////////////////////////////////////
 10 // Construction/Destruction
 11 //////////////////////////////////////////////////////////////////////
 12 
 13 linkedList::linkedList()  14 {  15     head = NULL;  16     len = 0;  17 }  18 
 19 linkedList::~linkedList()  20 {  21     LList* tmp = head;  22     //for( int i = 0; i < len; i ++ )
 23     while( tmp->next )  24  {  25         LList *q = tmp;  26         tmp = tmp->next;  27         delete q;  28  }  29 }  30 
 31 bool linkedList::initiateLinkedList()  32 {  33     std::ios::sync_with_stdio(false);  34     head = new LList;  35     if( !head )  36  {  37         cout << "初始化失敗!" << endl;  38         return false;  39  }  40     head->next = NULL;  41     return true;  42 }  43 
 44 bool linkedList::createLinkedListRail( int length )  45 {  46     std::ios::sync_with_stdio(false);  47  initiateLinkedList();  48     LList* rail = head;  49     for( int i = 1; i <= length; i ++ )  50  {  51         LList* tmp = new LList;  52         int num;  53         cin >> num;  54         //num = i + 1;
 55         tmp->data = num;  56         tmp->next = rail->next;  57         rail->next = tmp;  58         rail = tmp;  59         len ++;  60  }  61     return true;  62 }  63 
 64 bool linkedList::createLinkedListFront( int length )  65 {  66     std::ios::sync_with_stdio(false);  67  initiateLinkedList();  68     for( int i = 0; i < length; i ++ )  69  {  70         int num;  71         cin >> num;  72         //num = i + 1;
 73         LList* tmp = new LList;  74         tmp->data = num;  75         tmp->next = head->next;  76         head->next = tmp;  77         len ++;  78  }  79     return true;  80 }  81 
 82 void linkedList::addLinkedListNodeLast( int value )  83 {  84     //ios::sync_with_stdio(false);
 85     
 86     LList* tmp = head;  87     LList* last = NULL;  88     while(tmp)  89  {  90         last = tmp;  91         tmp = tmp->next;  92  }  93     LList* PNew = new LList;  94     PNew->data = value;  95     PNew->next = NULL;  96     last->next = PNew;  97     len ++;  98 }  99 
100 bool linkedList::isEmpty() 101 { 102     return head->next == NULL; 103 } 104 
105 void linkedList::printLinkedList() 106 { 107     std::ios::sync_with_stdio(false); 108     if( isEmpty() ) 109  { 110         cout << "空鏈表,無法打印!" << endl; 111         return; 112  } 113     LList* tmp = head->next; 114     int column = 0; 115     while(tmp) 116  { 117         cout << setiosflags(ios::left) << setw(3) << tmp->data << " "; 118         column ++; 119         if( column % 10 == 0 ) 120             cout << endl; 121         tmp = tmp->next; 122  } 123     cout << endl; 124 } 125 
126 int linkedList::linkedListLength() 127 { 128     if( isEmpty() ) 129  { 130         cout << "空鏈表!" << endl; 131         return -1; 132  } 133     int l = 0; 134     LList* tmp = head->next; 135     while(tmp) 136  { 137         tmp = tmp->next; 138         l ++; 139  } 140     return l; 141     //return len;
142 } 143 
144 bool linkedList::getElementByPosition( int pos, int& value ) 145 { 146     ios::sync_with_stdio(false); 147     if( isEmpty() ) 148  { 149         cout << "鏈表為空!獲取元素失敗!" << endl; 150         return false; 151  } 152     if( pos > len ) 153  { 154         cout << "位置大於表長!獲取元素失敗!" << endl; 155         return false; 156  } 157     if( pos <= 0 ) 158  { 159         cout << "位置必須大於0!獲取元素失敗!" << endl; 160         return false; 161  } 162     int index = 0; 163     LList* tmp = head; 164     while(tmp) 165  { 166         if( index == pos ) 167  { 168             //cout << tmp->data;
169             value = tmp->data; 170             return true; 171  } 172         tmp = tmp->next; 173         index ++; 174  } 175     return true; 176 } 177 
178 bool linkedList::insertListByPosition( int pos, int value ) 179 { 180     ios::sync_with_stdio(false); 181     if( isEmpty() ) 182  { 183         cout << "鏈表為空!插入元素失敗!" << endl; 184         return false; 185  } 186     else if( pos > len ) 187  { 188         cout << "位置大於表長且差值大於1!刪除元素失敗!" << endl; 189         return false; 190  } 191     else if( pos == len ) 192  { 193         cout << "將會直接把新節點接在鏈表尾部!" << endl; 194  addLinkedListNodeLast( value ); 195         return true; 196  } 197     else if( pos <= 0 ) 198  { 199         cout << "位置必須大於0!插入元素失敗!" << endl; 200         return false; 201  } 202     int index = 0; 203     LList* tmp = head; 204     while( index != pos - 1 && tmp ) 205  { 206         index ++; 207         tmp = tmp->next; 208  } 209     if( tmp == NULL ) 210  { 211         cout << "位置大於表長且不在表長的后一位!插入元素失敗!" << endl; 212         return false; 213  } 214     LList* PNew = new LList; 215     PNew->data = value; 216     PNew->next = tmp->next; 217     tmp->next = PNew; 218     len ++; 219     return true; 220 } 221 
222 bool linkedList::getElementByValue( int& pos, int value ) 223 { 224     ios::sync_with_stdio(false); 225     if( isEmpty() ) 226  { 227         cout << "鏈表為空!獲取元素失敗!" << endl; 228         return false; 229  } 230     int index = 1; 231     LList* tmp = head->next; 232     while(tmp) 233  { 234         if( tmp->data == value ) 235  { 236             pos = index; 237             return true; 238  } 239         tmp = tmp->next; 240         index ++; 241  } 242     return false; 243 } 244 
245 bool linkedList::removeListNodeByPosition( int pos, int& value ) 246 { 247     ios::sync_with_stdio(false); 248     if( isEmpty() ) 249  { 250         cout << "鏈表為空!刪除元素失敗!" << endl; 251         return false; 252  } 253     if( pos > len ) 254  { 255         cout << "位置大於表長!刪除元素失敗!" << endl; 256         return false; 257  } 258     if( pos <= 0 ) 259  { 260         cout << "位置必須大於0!刪除元素失敗!" << endl; 261         return false; 262  } 263     LList* tmp = head; 264     int index = 0; 265     while( index != pos - 1 && tmp ) 266  { 267         tmp = tmp->next; 268         index ++; 269  } 270     LList* PDel = tmp->next; 271     value = PDel->data; 272     tmp->next = tmp->next->next; 273     delete PDel; 274     len --; 275     return true; 276 } 277 
278 bool linkedList::insertListSort( int value ) 279 { 280     ios::sync_with_stdio(false); 281     if( isEmpty() ) 282  { 283         cout << "鏈表為空!插入元素失敗!" << endl; 284         return false; 285  } 286     LList* tmp = head; 287     while( tmp->next && tmp->next->data < value )//下一個節點的data比value小就繼續循環 288     //寫成下面這樣導致比最后一個節點的data大的value無法插入!因為循環結束時tmp->next為NULL,無法插入。 289     //while( tmp && tmp->next->data < value )
290  { 291         //if( tmp->data < value )
292             tmp = tmp->next; 293  } 294     LList* PNew = new LList; 295     PNew->data = value; 296     PNew->next = tmp->next; 297     tmp->next = PNew; 298     return true; 299 } 300 
301 bool linkedList::oddEvenSort( linkedList& LA,linkedList& LB ) 302 { 303     ios::sync_with_stdio(false); 304     if( isEmpty() ) 305  { 306         cout << "原鏈表為空!分配元素失敗!" << endl; 307         return false; 308  } 309     //if( !LA.head->next && !LB.head->next )
310     if( !LA.head && !LB.head ) 311  { 312  LA.initiateLinkedList(); 313  LB.initiateLinkedList(); 314  } 315     LList* tmp = head->next; 316     while(tmp) 317  { 318         if( tmp->data >= 0 && ( tmp->data & 1 ) ) 319             LA.addLinkedListNodeLast( tmp->data ); 320         //else if( tmp->data >= 0 && !( tmp->data & 1 ) )
321         else
322             LB.addLinkedListNodeLast( tmp->data ); 323         tmp = tmp->next; 324  } 325     return true; 326 }

 

 1 // LinkedList.cpp : Defines the entry point for the console application.  2 //  3 
 4 #include "stdafx.h"
 5 #include "linkedList1.h"
 6 
 7 int main(int argc, char* argv[])  8 {  9     ios::sync_with_stdio(false);  10     freopen( "1.in", "r", stdin );  11     
 12     linkedList L1;//, L2;
 13     int n;  14     cin >> n;  15  L1.createLinkedListFront(n);  16     cout << "原表表長為:" << endl;  17     cout << L1.linkedListLength() << endl;  18     cout << "原表元素為:" << endl;  19  L1.printLinkedList();  20     /*
 21  L1.~linkedList();  22  cout << "現表表長為:" << endl;  23  cout << L1.linkedListLength() << endl;  24  cout << "現表元素為:" << endl;  25  L1.printLinkedList();  26  //L2.createLinkedListFront(5);  27  //cout << L2.linkedListLength() << endl;  28  //L2.printLinkedList();  29  22  30  30 70 92 91 15 47 84 10 43 34 9 62 60 26 79 96 38 4 92 24 25 5  31     
 32  linkedList L3;  33  int n;  34  cin >> n;  35  L3.createLinkedListRail(n);  36  cout << "原表表長為:" << endl;  37  cout << L3.linkedListLength() << endl;  38  cout << "原表元素為:" << endl;  39  L3.printLinkedList();//5,n,0,n+1,n+2  40     
 41  int value = -100;  42  int num;  43  cin >> num;  44  for( int i = 0; i < num; i ++ )  45  {  46  int pos;  47  cin >> pos;  48  if( L3.getElementByPosition( pos, value ) )  49  {  50  cout << "第 " << pos << " 個元素的值為:" << value << endl;  51 
 52  }  53  else  54  cout << "不存在位置為 " << pos << " 的元素!" << endl;  55  }  56 
 57  linkedList L4;  58  int n;  59  cin >> n;  60  L4.createLinkedListRail(n);  61  cout << "原表表長為:" << endl;  62  cout << L4.linkedListLength() << endl;  63  cout << "原表元素為:" << endl;  64  L4.printLinkedList();//x=100, i分別為5,n,n+1,0,1,n+2  65  int value = 100;  66  int num;  67  cin >> num;  68  for( int i = 0; i < num; i ++ )  69  {  70  int pos;  71  cin >> pos;  72  if( L4.insertListByPosition( pos, value ) )  73  {  74  cout << "value = " << value << " 的值已插在 pos = " << pos << "的位置上!" << endl;  75  cout << "現表表長為:" << endl;  76  cout << L4.linkedListLength() << endl;  77  cout << "現表元素為:" << endl;  78  L4.printLinkedList();  79  }  80  }  81     
 82  linkedList L5;  83  int n;  84  cin >> n;  85  L5.createLinkedListRail(n);  86  cout << "原表表長為:" << endl;  87  cout << L5.linkedListLength() << endl;  88  cout << "原表元素為:" << endl;  89  L5.printLinkedList();  90  int index = -1;  91  //1,17,20,88  92  for( int i = 0; i < 4; i ++ )  93  {  94  int value;  95  cin >> value;  96  if( L5.getElementByValue( index, value ) )  97  {  98  cout << "pos = " << index << ", value = " << 1 << endl;  99  } 100     
101  else 102  { 103  cout << "鏈表中不存在值為 " << value << " 的值" << endl; 104  } 105  } 106     
107  linkedList L6; 108  int n; 109  cin >> n; 110  L6.createLinkedListRail(n); 111  L6.printLinkedList(); 112  cout << L6.linkedListLength() << endl; 113  int value = -1; 114  //5,n,1,n+1,0 115  if( L6.removeListNodeByPosition( 5, value ) ) 116  { 117  cout << "pos = " << 5 << ", value = " << value << "已刪除!" << endl; 118  } 119  L6.printLinkedList(); 120  if( L6.removeListNodeByPosition( n , value ) ) 121  { 122  cout << "pos = " << n << ", value = " << value << "已刪除!" << endl; 123  } 124  else 125  { 126  cout << "不存在位置等於 " << n << " 的元素!" << endl; 127  } 128  L6.printLinkedList(); 129  if( L6.removeListNodeByPosition( 1, value ) ) 130  { 131  cout << "pos = " << 1 << ", value = " << value << "已刪除!" << endl; 132  } 133  L6.printLinkedList(); 134  if( L6.removeListNodeByPosition( n + 1, value ) ) 135  { 136  cout << "pos = " << n + 1 << ", value = " << value << "已刪除!" << endl; 137  } 138  else 139  { 140  cout << "不存在位置等於 " << n + 1 << " 的元素!" << endl; 141  } 142  L6.printLinkedList(); 143  if( L6.removeListNodeByPosition( 0, value ) ) 144  { 145  cout << "pos = " << 0 << ", value = " << value << "已刪除!" << endl; 146  } 147  else 148  { 149  cout << "不存在位置等於 " << 0 << " 的元素!" << endl; 150  } 151  L6.printLinkedList(); 152 
153     
154  linkedList L7; 155  int n; 156  cin >> n; 157  L7.createLinkedListRail(n); 158  cout << "原表表長為:" << endl; 159  cout << L7.linkedListLength() << endl; 160  cout << "原表元素為:" << endl; 161  L7.printLinkedList(); 162     
163  //int value = -1; 164  //5,n,1,n+1,0 165  for( int i = 0; i < 1; i ++ ) 166  { 167  int value; 168  cin >> value; 169  if( L7.removeListNodeByPosition( 5, value ) ) 170  { 171  cout << "pos = " << 5 << ", value = " << value << "已刪除!" << endl; 172  cout << "現表表長為:" << endl; 173  cout << L7.linkedListLength() << endl; 174  cout << "現表元素為:" << endl; 175  L7.printLinkedList(); 176  } 177  if( L7.removeListNodeByPosition( n , value ) ) 178  { 179  cout << "pos = " << n << ", value = " << value << "已刪除!" << endl; 180  } 181  else 182  { 183  cout << "不存在位置等於 " << n << " 的元素!" << endl; 184  } 185  } 186     
187  linkedList L8; 188  int n; 189  cin >> n; 190  L8.createLinkedListRail(n); 191  cout << "原表表長為:" << endl; 192  cout << L8.linkedListLength() << endl; 193     
194  cout << "原表元素為:" << endl; 195  L8.printLinkedList(); 196  int value; 197  for( int i = 0; i < 4; i ++ ) 198  { 199  cin >> value; 200  if( L8.insertListSort(value) ) 201  { 202  cout << "插入元素 " << value << " 后表長為:" << endl; 203  cout << L8.linkedListLength() << endl; 204  cout << "插入元素 " << value << " 表元素為:" << endl; 205  L8.printLinkedList(); 206  } 207  else 208  cout << "Error!" << endl; 209  } 210     
211  int n; 212  linkedList L9, LA, LB; 213  cin >> n; 214  L9.createLinkedListRail(n); 215  //LA.initiateLinkedList(), LB.initiateLinkedList(); 216  cout << "原鏈表表長為:" << endl; 217  cout << L9.linkedListLength() << endl; 218  cout << "原鏈表元素為:" << endl; 219  L9.printLinkedList(); 220  L9.oddEvenSort( LA, LB ); 221  cout << "奇數鏈表表長為:" << endl; 222  cout << LA.linkedListLength() << endl; 223  cout << "奇數鏈表元素為:" << endl; 224  LA.printLinkedList(); 225  cout << "偶數鏈表表長為:" << endl; 226  cout << LB.linkedListLength() << endl; 227  cout << "偶數鏈表元素為:" << endl; 228  LB.printLinkedList(); 229     */
230     return 0; 231 }

圖1 測試(1)

 

圖2 測試(2)

 

圖3 測試(3)

 

圖4 測試(4)

 

圖5 測試(5)

 

 

      圖6 測試(5)

 

圖7 測試(6)

 

圖8 測試(6)

 

圖9 測試(7)

 

圖10 測試(8)

 

圖11 測試(8)

 

圖12 測試(9)

 

圖13 測試(10)

 

圖14 測試(10)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM