C++實現線性表的鏈接存儲結構(單鏈表)


將線性表的抽象數據類型定義在鏈接存儲結構下用C++的類實現,由於線性表的數據元素類型不確定,所以采用模板機制。

  1 頭文件linklist.h
  2 #pragma once
  3 #include <iostream>
  4 // 單鏈表的節點
  5 template<class T>
  6 struct Node
  7 {
  8     T data;//數據域
  9     Node<T> *next;// 指針域,指向后繼節點
 10 };
 11 // 單鏈表的類實現
 12 template<class T>
 13 class LinkList
 14 {
 15 public:
 16     LinkList();// 無參構造函數,建立只有頭節點的空鏈表
 17     LinkList(T a[],int n);// 有參構造函數,建立有n個元素的單鏈表
 18     ~LinkList();// 析構函數
 19     int Length();// 求單鏈表的長度
 20     T Get(int i);// 查找第i個元素
 21     int Locate(T x);// 查找值為x的元素
 22     void Insert(int i, T x);// 在第i個元素處插入x
 23     T Delete(int i);// 刪除第i個節點
 24     void PrintList();// 遍歷各個元素
 25 private:
 26     Node<T>* first;// 單鏈表的頭節點
 27 };
 28 
 29 template<class T>
 30 inline LinkList<T>::LinkList()
 31 {
 32     first = new Node<T>;    // 生成頭節點
 33     first->next = NULL;        // 頭節點指針域為空
 34 }
 35 
 36 // 頭插法建立單鏈表
 37 template<class T>
 38 LinkList<T>::LinkList(T a[], int n)
 39 {
 40     first = new Node<T>;    
 41     first->next = NULL;        // 初始化一個空鏈表
 42     for (int i = 0; i < n; i++)
 43     {
 44         Node<T>* S = new Node<T>;
 45         S->data = a[i];        // 為每個數據元素建立一個節點
 46         S->next = first->next;
 47         first->next = S;    // 將節點S插入頭節點之后
 48     }
 49 }
 50 // 尾插法建立單鏈表
 51 template<class T>
 52 LinkList<T>::LinkList(T a[], int n)
 53 {
 54     first = new Node<T>;// 建立頭節點
 55     first->next = NULL;
 56     Node<T>* r = first;// 尾指針初始化
 57     for(int i = 0; i < n; i++)
 58     {
 59         Node<T>* S = new Node<T>;
 60         S->data = a[i];        // 為每個數據元素建立一個節點
 61         r->next = S;
 62         r = S;                // 插入節點S,並將尾指針指向S節點
 63     }
 64     r->next = NULL;            // 單鏈表建立完畢之后,將尾指針置空
 65 }
 66 
 67 template<class T>
 68 LinkList<T>::~LinkList()
 69 {
 70     while (first != NULL)
 71     {
 72         Node<T>* p = first;        // 暫存將被釋放節點
 73         first = first->next;    // 指向下一個節點
 74         delete p;
 75     }
 76 }
 77 
 78 template<class T>
 79 int LinkList<T>::Length()
 80 {
 81     int count = 0;                // 計數
 82     Node<T>* p = first->next;    // 將工作指針指向第一個節點
 83     while (p != NULL)
 84     {
 85         count++;
 86         p = p->next;
 87     }
 88     return count;
 89 }
 90 
 91 template<class T>
 92 T LinkList<T>::Get(int i)
 93 {
 94     int count = 0;                // 計數
 95     Node<T>* p = first->next;    // 將工作指針指向第一個節點
 96     while (p != NULL)
 97     {
 98         count++;
 99         if (count == i)
100             return p->data;
101         p = p->next;
102     }
103     return -1;                    // 越界
104 }
105 
106 template<class T>
107 int LinkList<T>::Locate(T x)
108 {
109     int count = 0;                // 計數
110     Node<T>* p = first->next;    // 將工作指針指向第一個節點
111     while (p != NULL)
112     {
113         count++;
114         if (p->data == x)
115             return count;
116         p = p->next;
117     }
118     return 0;                // 查找失敗
119 }
120 
121 template<class T>
122 void LinkList<T>::Insert(int i, T x)
123 {
124     int count = 0;                // 計數
125     Node<T>* p = first;            // 將工作指針指向頭節點
126     while (p != NULL)
127     {        
128         if (count == i - 1)        // 找第i-1個節點
129         {
130             Node<T>* S = new Node<T>;
131             S->data = x;
132             S->next = p->next;
133             p->next = S;
134         }
135         p = p->next;
136         count++;
137     }
138     if (p == NULL)
139         throw "位置越界";
140 }
141 
142 template<class T>
143 T LinkList<T>::Delete(int i)
144 {
145     int count = 0;                // 計數
146     Node<T>* p = first;            // 將工作指針指向頭節點
147     while (p != NULL)
148     {
149         if (count == i - 1)
150         {
151             Node<T>* q = p->next;// 暫存被刪節點
152             T x = q->data;
153             p->next = q->next;
154             delete q;
155             return x;
156         }
157         p = p->next;
158         count++;
159     }
160     return -1;
161 }
162 
163 template<class T>
164 void LinkList<T>::PrintList()
165 {
166     Node<T>* p = first->next;    // 將工作指針指向第一個節點
167     while (p != NULL)
168     {
169         cout << p->data << " ";
170         p = p->next;
171     }
172 }
主函數
#include"linklist.h"
using namespace std;

int main()
{
    int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    LinkList<int>* linklist = new LinkList<int>(arry, 10);
    cout << linklist->Length() << endl;
    cout << linklist->Get(5) << endl;
    cout << linklist->Locate(6) << endl;
    linklist->Insert(3, 11);
    linklist->Delete(10);
    linklist->PrintList();

    system("pause");
    return 0;
}

 運行結果如下:

 


免責聲明!

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



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