1. 程式人生 > 實用技巧 >單鏈表(C風格的C++實現)

單鏈表(C風格的C++實現)

  1 /*
  2     Author:    
  3     
  4 */
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <cstdio>
  8 #include <cstdlib>
  9 using namespace std;
 10 
 11 typedef struct LNode{
 12     struct LNode *next;
 13     int data;
 14 }Node,*LinkedList;
 15 
 16 /*
 17 *@this function is create an empty linkedlist,
18 * 19 * 20 *建立一個帶頭節點的空連結串列 21 */ 22 void create_empty_Linkedlist(LinkedList &L){ 23 L = (LinkedList)malloc(sizeof(Node)); 24 if(L == NULL) 25 exit(0); 26 else 27 L->next = NULL; 28 cout<<"create_empty_list successfully,congraduation!"<<endl;
29 } 30 //判斷是否空連結串列 31 bool isEmpty(LinkedList L){ 32 if(L == NULL) return true; 33 else return false; 34 } 35 /* 36 在連結串列末尾新增元素,新增成功返回true否則失敗 37 */ 38 void add_elem_tail(LinkedList &L,int num){ 39 //首先判斷是否是空連結串列,空連結串列直接刪除 40 if(isEmpty(L)==true){ 41 cout<<"
新增失敗,因為是空連結串列"<<endl; 42 return ; 43 } 44 Node *temp = L; 45 while(temp->next != NULL){ 46 temp = temp->next; 47 } 48 Node *new_Node = (Node*)malloc(sizeof(Node)); 49 new_Node->next = NULL; 50 new_Node->data = num; 51 temp->next = new_Node; 52 53 } 54 55 //刪除末尾元素 56 void deletetail(LinkedList &L){ 57 if(isEmpty(L) == true){ 58 cout<<"刪除失敗,因為是空連結串列"<<endl; 59 return; 60 } 61 Node *temp = L; 62 Node *pre = L; 63 temp = temp->next; 64 while(temp->next !=NULL){ 65 temp = temp->next; 66 pre = pre->next; 67 } 68 pre->next = NULL; 69 free(temp); 70 } 71 //統計連結串列元素個數 72 int linkedList_size(LinkedList L){ 73 int count = 0; 74 Node *temp = L; 75 // temp = temp->next; 76 while(temp){ 77 count ++; 78 temp = temp->next; 79 } 80 return count; 81 } 82 //在指定位置新增元素 83 bool add_elem_to_position(LinkedList &L,int num,int position){ 84 //判斷是否為空連結串列 85 if(isEmpty(L) == true ){ 86 cout<<"新增失敗,因為是一個空連結串列"<<endl; 87 return false; 88 } 89 //判斷position位置是否合法 90 int size = linkedList_size(L); 91 if(position < 0 || position > size){ 92 cout<<"新增失敗,因為position資料違法"<<endl; 93 return false; 94 } 95 96 //合法位置有三種情況: 97 Node *temp = L; 98 99 if(position == 0){ 100 Node *s = (Node*)malloc(sizeof(Node)); 101 s->data = num; 102 s->next = L->next; 103 L->next = s; 104 } 105 else if(position == size){ 106 add_elem_tail(L,num); 107 } 108 else{ 109 for(int i = 0;i<position-1;i++){ 110 temp = temp->next; 111 } 112 Node *s = (Node*)malloc(sizeof(Node)); 113 s->data = num; 114 s->next = temp ->next; 115 temp->next = s; 116 } 117 return true; 118 } 119 //刪除指定位置的元素 120 void delete_elem_position(LinkedList &L,int position){ 121 int size = linkedList_size(L); 122 if(size == 0){ 123 cout<<"刪除失敗,因為連結串列為空"<<endl; 124 return ; 125 } 126 Node *pre = L; 127 Node *del; 128 129 for(int i = 1;i<position-1;i++){ 130 pre = pre->next; 131 } 132 del = pre->next; 133 pre->next = del->next; 134 free(del); 135 } 136 /* 137 從頭遍歷整個連結串列 138 */ 139 void display(LinkedList L){ 140 if(isEmpty(L) == true){ 141 cout<<"遍歷失敗,因為這是一個空連結串列"<<endl; 142 } 143 Node *temp = L; 144 temp = temp->next; 145 bool flag = true; 146 while(temp != NULL){ 147 if(flag == true){ 148 cout<<temp->data; 149 flag = false; 150 }else{ 151 cout<<"->"<<temp->data; 152 } 153 temp = temp->next; 154 } 155 cout<<endl; 156 } 157 int main(){ 158 LinkedList L; 159 cout<<"建立帶頭結點的空連結串列:"; 160 create_empty_Linkedlist(L); 161 // add_elem_tail(L,3); 162 for(int i = 1;i<12;i++){ 163 add_elem_to_position(L,i,i); 164 } 165 cout<<"遍歷連結串列:"; 166 display(L); 167 deletetail(L); 168 display(L); 169 for(int i = 1;i<9;i++){ 170 delete_elem_position(L,1); 171 display(L); 172 } 173 return 0; 174 }