C++ 單鏈表基本操作分析與實現


鏈表

  鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域。 相比於線性表順序結構,鏈表比較方便插入和刪除操作。

 

創建頭節點

  手動new一個新的Node,將Node的next置為NULL即可。

  head = new Node(0);head->next = NULL;

從頭插入一個新的節點

   手動new出一個新的節點p,使p的next的指向head->next所指向的地址,然后將head->next從新指向p即可。

   Node * p = new Node(int);  p->next = head->next;   head->next = p;

刪除指定節點

  先遍歷到指定節點的前一個節點,然后通過將前一個節點的next指針指向指定節點的下一個節點,達到懸空指定節點的效果,然后刪除指定節點即可。代碼請參照后面的完整代碼。

 

修改指定節點

  遍歷到指定節點的位置,將其data修改為要修改的值即可。修改代碼請參考后面的完整代碼。

 

鏈表反轉

  定義三個臨時節點指向頭結點之后的第1個節點p,第2個節點q和第3個節點m。將p->next置為空,然后將q->next = p,然后將p向后移動一個節點,即p = q,最后將q向后移動一個節點,即q = m,最后把m向后移動一個節點,即m = m->next;依此類推直到m等於NULL,然后將q->next = p,最后將head->next指向q(即目前第一個節點疑,也就是原本最后的一個節點)。

  通過三個節點達到從頭開始逐個逆序的目的。反轉代碼請參考后面的完整代碼。

  


完整代碼如下:

  1 //
  2 //  List.cpp
  3 //  List
  4 //
  5 //  Created by scandy_yuan on 13-1-6.
  6 //  Copyright (c) 2013年 Sam. All rights reserved.
  7 //
  8 
  9 #include <iostream>
 10 using namespace std;
 11 
 12 class List {
 13 public:
 14     List(){create_List();}
 15     ~List(){clear();}
 16     //創建頭結點
 17     void create_List();
 18     //插入函數
 19     void insert(const int& d);
 20     //在指定位置插入
 21     void insert_pos(const int& d,const int& d1);
 22     //刪除指定數據的節點
 23     void erase(const int& d);
 24     //修改指定數據
 25     void updata(const int& d,const int& d1);
 26     //反轉鏈表函數
 27     void reverse();
 28     //打印
 29     void print();
 30 private:
 31     //節點結構
 32     struct Node{
 33         int data;
 34         Node * next;
 35         Node(const int& d):data(d),next(NULL){}
 36     };
 37     Node * head;//頭節點
 38     //清理鏈表函數
 39     void clear(){
 40         Node * p = head;
 41         //從頭節點開始循環刪除
 42         while(p){
 43             Node * q = p->next;
 44             delete p;
 45             p = q;
 46         }
 47     }
 48     //查找數據d的上一個節點位置的函數
 49     //為了方便后面刪除操作
 50     Node* find(const int& d){
 51         Node * p = head;
 52         for(;p;p=p->next){
 53             if(p->next->data==d)
 54                 break;
 55         }
 56         return p;
 57     }
 58 };
 59 
 60 //創建頭結點
 61 void List::create_List()
 62 {
 63      head = new Node(0);
 64 }
 65 //從頭插入一個節點
 66 void List::insert(const int& d)
 67 {
 68     Node *p = new Node(d);
 69     p->next = head->next;
 70     head->next = p;
 71 }
 72 //打印函數
 73 void List::print()
 74 {
 75     for(Node * p = head->next;p;p=p->next){
 76         cout << p->data << endl;
 77     }
 78 }
 79 //在d位置之前插入d1
 80 void List::insert_pos(const int& d,const int& d1)
 81 {
 82     Node * p = find(d);
 83     Node * q = new Node(d1);
 84     q->next = p->next;
 85     p->next = q;
 86 }
 87 
 88 //刪除
 89 void List::erase(const int& d)
 90 {
 91     Node * p = find(d);
 92     //因為p是上一個節點的位置,用q來保存
 93     //要刪除的節點的地址
 94     Node *q = p->next;
 95     //通過將上一個節點的next指針指向要刪除節點的next指
 96     //針志向的節點實現斷開要刪除節點的目的
 97     p->next = p->next->next;
 98     //刪除要刪除的節點q
 99     delete q;
100 }
101 
102 //修改指定數據
103 void List::updata(const int& d,const int& d1)
104 {
105     Node * p = find(d);
106     p->next->data = d1;
107 }
108 
109 //反轉鏈表
110 void List::reverse()
111 {
112     Node * p = head->next;//頭結點之后的第1個節點
113     Node * q = head->next->next;//頭結點之后的第2節點
114     Node * m = head->next->next->next;//頭結點之后的第3個節點
115     p->next = NULL;//將頭接點之后的第1個節點的next指針置為空
116     //根據m是否為空來判斷 以此逆序每一個節點
117     while(m){
118         q->next = p;
119         p = q;
120         q = m;
121         m = m->next;
122     }
123     //將最后一個節點逆序
124     q->next = p;
125     //將頭從新指向新的的第1個節點(之前的最后一個節點)
126     head ->next = q;
127 }
128 
129 int main(int argc, const char * argv[])
130 {
131 
132     // insert code here...
133     List list;
134     list.insert(30);
135     list.insert(20);
136     list.insert(10);
137     list.insert_pos(10, 5);
138     list.print();
139     cout << "---------------------" << endl;
140     list.erase(10);
141     list.print();
142     cout << "---------------------" << endl;
143     list.reverse();
144     list.print();
145     cout << "---------------------" << endl;
146     list.updata(5, 8);
147     list.print();
148     return 0;
149 }

 


免責聲明!

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



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