1. 程式人生 > >c++資料結構之連結串列詳情1(順序連結串列)

c++資料結構之連結串列詳情1(順序連結串列)

長大是人必經的潰爛        ---大衛塞林格

程式碼是年輕人的新生!!!!!!

程式 = 資料結構  + 演算法   --Niklaus EmilWirth

這篇部落格在參考一些書籍和教學視訊的基礎上整理而來,中間夾雜了一些自己寫的程式碼

1、List

先貼一個基礎的整型順序連結串列list的c++程式碼

List.cpp

  1 #include <iostream>
  2 #include "List.h"
  3 
  4 using namespace std;
  5 
  6
//建立連結串列 7 List::List(int Size) 8 { 9 m_iSize =Size; 10 m_pList = new int[m_iSize]; 11 m_iLength = 0; 12 } 13 List::~List() 14 { 15 delete []m_pList; 16 m_pList = NULL; 17 } 18 //清空連結串列 19 void List::ClearList() 20 { 21 m_iLength = 0; 22 } 23 bool List::IsEmpty()
24 { 25 if (m_iLength==0) 26 { 27 return true; 28 } 29 else 30 { 31 return false; 32 } 33 } 34 int List::GetLength() 35 { 36 return m_iLength; 37 } 38 bool List::GetElement(int i,int *e) 39 { 40 if (i<0||i>m_iSize) 41 { 42 return
false; 43 } 44 *e = m_pList[i]; 45 return true; 46 } 47 int List::LocateElement(int *e) 48 { 49 for (int i=0;i<m_iLength;i++) 50 { 51 if (m_pList[i] == *e) 52 { 53 return i; 54 } 55 } 56 } 57 /********************************************************** 58 獲取指定元素的前驅 59 **********************************************************/ 60 bool List::PriorElement(int *PreElement,int *CurrentElement) 61 { 62 int temp = LocateElement(CurrentElement); 63 if (temp == -1) 64 { 65 return false; 66 } 67 else 68 { 69 if (temp == 0) 70 { 71 return false; 72 } 73 else 74 { 75 *PreElement = m_pList[temp-1]; 76 return true; 77 } 78 } 79 } 80 /******************************************************* 81 獲取指定元素的後繼 82 ********************************************************/ 83 bool List::NextElement(int *NeElment,int *CurrentElement) 84 { 85 int temp = LocateElement(CurrentElement); 86 if (temp == -1) 87 { 88 return false; 89 } 90 else 91 { 92 if (temp == m_iLength) 93 { 94 return false; 95 } 96 else 97 { 98 *NeElment = m_pList[temp+1]; 99 return true; 100 } 101 } 102 } 103 /************************************ 104 在順序表中的指定位置插入元素 105 *************************************/ 106 bool List::ListInsert(int i,int *e) 107 { 108 if(i<0||i>m_iLength) 109 { 110 return false; 111 } 112 for (int k=m_iLength-1;k>=i;k--) 113 { 114 m_pList[k+1] = m_pList[k]; 115 } 116 m_pList[i] = *e; 117 m_iLength++; 118 } 119 /*********************************** 120 刪除順序表指定的元素 121 ************************************/ 122 bool List::ListDelete(int i,int *e) 123 { 124 *e = m_pList[i]; 125 if(i<0||i>m_iLength) 126 { 127 return false; 128 } 129 for (int k=i+1;k<m_iLength;i++) 130 { 131 m_pList[k-1] = m_pList[k]; 132 } 133 m_iLength--; 134 } 135 /*********************************** 136 遍歷順序表中的資料 137 ************************************/ 138 void List::ListTranverse() 139 { 140 for (int i=0;i<m_iLength;i++) 141 { 142 cout<<m_pList[i]<<endl; 143 } 144 } 145 //bool List::ListInsertHead(int *e) 146 //{ 147 // 148 //} 149 //bool List::ListInsertTail(int *e) 150 //{ 151 // 152 //}

List.h

#ifndef LIST_H
#define LIST_H


class List
{
public:
    List(int Size);
    ~List();
    void ClearList();
    bool IsEmpty();
    int GetLength();
    bool GetElement(int i,int *e);
    int LocateElement(int *e);
    bool PriorElement(int *PreElement,int *CurrentElement);
    bool NextElement(int *NeElment,int *CurrentElement);
    bool ListInsert(int i,int *e);
    bool ListInsertHead(int *e);
    bool ListInsertTail(int *e);
    bool ListDelete(int i,int *e);
    void ListTranverse();
protected:
    int *m_pList;
    int m_iSize;
    int m_iLength;
};

#endif

主函式:

#include <stdio.h>
#include <stdlib.h>
#include "List.h"

using namespace std;

int main(void)
{
    int e1 = 3;
    List *list1 = new List(10);
    list1->ListInsert(0,&e1);
    list1->ListTranverse();
    system("pause");
    return 0;
}

以上程式碼為整型的順序list連結串列,加入一個模板(ps:由於vs中模板的定義個宣告不能分開寫,我的程式碼做了一點小小的修改),

修改以上程式碼,如下所示:

List.h

 1 #ifndef LIST_H
 2 #define LIST_H
 3 
 4 
 5 template<typename T1> 
 6 class List
 7 {
 8 public:
 9     List(int Size);
10     ~List();
11     void ClearList();
12     bool IsEmpty();
13     int GetLength();
14     bool GetElement(int i,T1 *e);
15     int LocateElement(T1 *e);
16     bool PriorElement(T1 *PreElement,T1 *CurrentElement);
17     bool NextElement(T1 *NeElment,T1 *CurrentElement);
18     bool ListInsert(int i,T1 *e);
19     bool ListInsertHead(T1 *e);
20     bool ListInsertTail(T1 *e);
21     bool ListDelete(int i,T1 *e);
22     void ListTranverse();
23 protected:
24     T1 *m_pList;
25     int m_iSize;
26     int m_iLength;
27 };
28 
29 #endif

List.cpp

  1 #include <iostream>
  2 #include "List.h"
  3 
  4 using namespace std;
  5 
  6 //建立連結串列
  7 template<typename T1> List<T1>::List(int Size)
  8 {
  9     m_iSize  =Size;
 10     m_pList = new T1[m_iSize];
 11     m_iLength = 0;
 12 }
 13 template<typename T1> List<T1>::~List()
 14 {
 15     delete []m_pList;
 16     m_pList = NULL;
 17 }
 18 //清空連結串列
 19 template<typename T1> void List<T1>::ClearList()
 20 {
 21     m_iLength = 0;
 22 }
 23 template<typename T1> bool List<T1>::IsEmpty()
 24 {
 25     if (m_iLength==0)
 26     {
 27         return true;
 28     }
 29     else
 30     {
 31         return false;
 32     }
 33 }
 34 template<typename T1> int List<T1>::GetLength()
 35 {
 36     return m_iLength;
 37 }
 38 template<typename T1> bool List<T1>::GetElement(int i,T1 *e)
 39 {
 40     if (i<0||i>m_iSize)
 41     {
 42         return false;
 43     }
 44     *e = m_pList[i];
 45     return true;
 46 }
 47 template<typename T1> int List<T1>::LocateElement(T1 *e)
 48 {
 49     for (int i=0;i<m_iLength;i++)
 50     {
 51         if (m_pList[i] == *e)
 52         {
 53             return i;
 54         }
 55     }
 56 }
 57 /**********************************************************
 58 獲取指定元素的前驅
 59 **********************************************************/
 60 template<typename T1> bool List<T1>::PriorElement(T1 *PreElement,T1 *CurrentElement)
 61 {
 62     int temp = LocateElement(CurrentElement);
 63     if (temp == -1)
 64     {
 65         return false;
 66     }
 67     else 
 68     {
 69         if (temp == 0)
 70         {
 71             return false;
 72         }
 73         else
 74         {
 75             *PreElement = m_pList[temp-1];
 76             return true;
 77         }
 78     }
 79 }
 80 /*******************************************************
 81 獲取指定元素的後繼
 82 ********************************************************/
 83 template<typename T1> bool List<T1>::NextElement(T1 *NeElment,T1 *CurrentElement)
 84 {
 85     int temp = LocateElement(CurrentElement);
 86     if (temp == -1)
 87     {
 88         return false;
 89     }
 90     else 
 91     {
 92         if (temp == m_iLength)
 93         {
 94             return false;
 95         }
 96         else
 97         {
 98             *NeElment = m_pList[temp+1];
 99             return true;
100         }
101     }
102 }
103 /************************************
104 在順序表中的指定位置插入元素
105 *************************************/
106 template<typename T1> bool List<T1>::ListInsert(int i,T1 *e)
107 {
108     if(i<0||i>m_iLength)
109     {
110         return false;
111     }
112     for (int k=m_iLength-1;k>=i;k--)
113     {
114         m_pList[k+1] = m_pList[k];
115     }
116     m_pList[i] = *e;
117     m_iLength++;
118 }
119 /***********************************
120 刪除順序表指定的元素
121 ************************************/
122 template<typename T1> bool List<T1>::ListDelete(int i,T1 *e)
123 {
124     *e = m_pList[i];
125     if(i<0||i>m_iLength)
126     {
127         return false;
128     }
129     for (int k=i+1;k<m_iLength;i++)
130     {
131         m_pList[k-1] = m_pList[k];
132     }
133     m_iLength--;
134 }
135 /***********************************
136 遍歷順序表中的資料
137 ************************************/
138 template<typename T1> void List<T1>::ListTranverse()
139 {
140     for (int i=0;i<m_iLength;i++)
141     {
142         cout<<m_pList[i]<<endl;
143     }
144 }
145 //bool List::ListInsertHead(int *e)
146 //{
147 //
148 //}
149 //bool List::ListInsertTail(int *e)
150 //{
151 //
152 //}

main.cpp

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "List.h"
 4 #include "List.cpp"
 5 
 6 using namespace std;
 7 
 8 int main(void)
 9 {
10     float e1 = 3.2;
11     List<float> *list1 = new List<float>(10);
12     list1->ListInsert(0,&e1);
13     list1->ListTranverse();
14     system("pause");
15     return 0;
16 }

好了,完整實現,以上是一個順序連結串列,換句話說就是一個數組的原始碼。接下來繼續分享連結串列的原始碼