线性表概述
一、線性表的定義
線性表:零個或多個數據元素的有限序列。
包括順序表和鏈表:順序表(其實就是數組)里面元素的地址是連續的,鏈表里面節點的地址不是連續的,是通過指針連起來的。
二、線性表的抽象數據類型
線性表的抽象數據類型定義如下:
ADT 線性表(List)
Data
線性表的數據對象集合為{a1,a2,....,an},每個元素的類型均為DataType。其中,除了第一個元素a1外,每一個元素有且只有一個直接前驅元素,除最后一個元素an外,每一個元素有且只有一個直接后繼元素。數據元素之間的關系是一對一的關系。
OperationInitList(*L):初始化操作,建立一個空的線性表。ListEmpty(L):若線性表為空,返回true,否則返回false。ClearList(*L):線性表清空。GetElem(L,i,*e):將線性表L中第i個位置元素返回給e。LocateElem(L,e):在線性表L中查找與給定值e相等的元素,如果查找成功,返回該元素在表中的序列號;否則,返回0表示失敗。ListInsert(*L,i,e):在線性表的第i個位置插入元素e。ListDelete(*L,i,*e):刪除線性表L中的第i個元素,并用e返回其值ListLength(L):返回線性表L的元素個數。PrintList(L):打印線性表對于不同的應用,線性表的基本操作是不同的,上述操作是最基本的。對于實際問題中涉及的關于線性表的更復雜操作,完全可以用這些基本操作的組合來實現。
三、線性表的順序存儲
3.1 順序存儲定義
順序表,一般使用數組實現,事實上就是在內存中找個初始地址,然后通過占位的形式,把一定連續的內存空間給占了,然后把相同數據類型的數據元素依次放在這塊空地中,數組大小有兩種方式指定,一是靜態分配,二是動態擴展。
??
順序表的封裝需要三個屬性:
(1) 存儲空間的起始位置。數組data的存儲位置就是線性表存儲空間的存儲位置
(2) 線性表的最大存儲容量。數組長度MAXSIZE
(3)線性表的當前長度。length
數組的長度與線性表的當前長度是不一樣的。數組的長度是存放線性表的存儲空間的總長度,一般初始化后不變。而線性表的當前長度是線性表中元素的個數,是會改變的。
3.2 順序存儲的算法實現
//順序表的模板類
const int MaxSize = 100;
template <class DataType>
class SeqList
{
public:SeqList(){length=0;} //無參數構造方法SeqList(DataType a[],int n); //有參數構造方法~SeqList(){} //析構函數int Length(){return length;} //線性表長度DataType Get(int i); //按位查找int Locate(DataType x); //按值查找void Insert(int i,DataType x); //插入DataType Delete(int i); //刪除void PrintList(); //遍歷
private:DataType data[MaxSize]; //順序表使用數組實現int length; //存儲順序表的長度
};
3.3 順序表的各個功能算法實現
順序表相關的操作跟數組有關,一般都是移動數組元素
3.3.1 有參數構造:
創建一個長度為n的順序表,需要將給定的數組元素作為線性表的數據元素傳入順序表中,并將傳入的元素個數作為順序表的長度
template <class DataType>
SeqList<DataType>::SeqList(DataType a[],int n)
{if(n>MaxSize) throw "wrong parameter";for(int i=0;i<n;i++)data[i]=a[i];length=n;
}
3.3.2 按位查找
template <class DataType>
DataType SeqList<DataType>::Get(int i)
{if(i<1 && i>length) throw "wrong Location";else return data[i-1];
}
//按位查找的時間復雜度為O(1)
3.3.3 按值查找
template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{for(int i=0;i<length;i++)if(data[i]==x) return i+1;return 0;
}
//按值查找,需要對順序表中的元素依次進行比較
3.3.4 插入
插入的過程中需要注意元素移動的方向,必須從最后一個元素開始移動,如果表滿了,則引發上溢;如果插入位置不合理,則引發位置異常.
template <class DataType>
void SeqList<DataType>::Insert(int i,DataType x)
{if(length>=MaxSize) throw "Overflow";if(i<1 || i>length+1) throw "Location";for(int j=length;j>=i;j--)data[j]=data[j-1];data[i-1]=x;length++;
}
3.3.5 刪除
注意算法中元素移動方向,移動元素之前必須取出被刪的元素,如果表為空則發生下溢,如果刪除位置不合理,則引發刪除位置異常。
template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{int x;if(length==0) throw "Underflow";if(i<1 || i>length) throw "Location";x = data[i-1];for(int j=i;j<length;j++)data[j-1] = data[j];length--;return x;
}
3.3.6 遍歷
template <class DataType>
void SeqList<DataType>::PrintList()
{for(int i=0;i<length;i++)cout<<data[i]<<endl;
}
//按下標依次輸出各元素
完整代碼示例:
#include<iostream>
using namespace std;const int MaxSize = 100;
template <class DataType>
class SeqList
{
public:SeqList(){length=0;} SeqList(DataType a[],int n); ~SeqList(){} int Length(){return length;} DataType Get(int i); int Locate(DataType x); void Insert(int i,DataType x); DataType Delete(int i); void PrintList();
private:DataType data[MaxSize]; int length;
};template <class DataType>
SeqList<DataType>::SeqList(DataType a[],int n)
{if(n>MaxSize) throw "wrong parameter";for(int i=0;i<n;i++)data[i]=a[i];length=n;
}template <class DataType>
DataType SeqList<DataType>::Get(int i)
{if(i<1 && i>length) throw "wrong Location";else return data[i-1];
}template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{for(int i=0;i<length;i++)if(data[i]==x) return i+1;return 0;
}template <class DataType>
void SeqList<DataType>::Insert(int i,DataType x)
{if(length>=MaxSize) throw "Overflow";if(i<1 || i>length+1) throw "Location";for(int j=length;j>=i;j--)data[j]=data[j-1];data[i-1]=x;length++;
}template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{int x;if(length==0) throw "Underflow";if(i<1 || i>length) throw "Location";x = data[i-1];for(int j=i;j<length;j++)data[j-1] = data[j];length--;return x;
}template <class DataType>
void SeqList<DataType>::PrintList()
{for(int i=0;i<length;i++)cout<<data[i]<<endl;
}int main()
{SeqList<int> p;p.Insert(1,5);//插入元素5p.Insert(2,9);//插入元素9p.PrintList();//打印列表p.Insert(2,3);//插入元素3cout<<p.Length()<<endl;//輸出列表長度p.PrintList();//打印列表cout<<p.Get(3)<<endl;//輸出第3個元素9p.Delete(2); //刪除第2個元素p.PrintList();//打印列表fgetchar();return 0;
}
運行結果:
??
3.3 順序存儲的優缺點
優點:隨機訪問特性,查找O(1)時間,存儲密度高;邏輯上相鄰的元素,物理上也相鄰;無須為表中元素之間的邏輯關系而增加額外的存儲空間。
缺點: 插入和刪除需移動大量元素; 當線性表長度變化較大時,難以確定存儲空間的容量; 造成存儲空間的“碎片”。
四、線性表的鏈式存儲
4.1 鏈式存儲定義
線性表的鏈式存儲結構的特點是用一組任意的存儲單元存儲線性表的數據元素,這組存儲單元可以是連續的,也可以是不連續的。這就意味著,這些元素可以存在內存未被占用的任意位置。
鏈表的定義是遞歸的,它或者為空null,或者指向另一個節點node的引用,這個節點含有下一個節點或鏈表的引用,線性鏈表的最后一個結點指針為“空”(通常用NULL或“^”符號表示)。
4.2 鏈式存儲的算法實現
template<class DataType>
struct Node
{DataType data; //存儲數據Node<DataType> *next; //存儲下一個結點的地址
};
結點由存放數據元素的數據域和存放后繼結點地址的指針域組成:
單鏈表的模板類的代碼:
template<class DataType>
class LinkList
{
public:LinkList(); LinkList(DataType a[], int n); ~LinkList(); int Length(); DataType Get(int i); int Locate(DataType x); void Insert(int i, DataType x); DataType Delete(int i); void PrintList();
private:Node<DataType> *first;
};
用一組任意的存儲單元存儲線性表的數據元素, 這組存儲單元可以存在內存中未被占用的任意位置; 順序存儲結構每個數據元素只需要存儲一個位置就可以了,而鏈式存儲結構中,除了要存儲數據信息外,還要存儲它的后繼元素的存儲地址。
無參數構造(生成只有頭結點的空鏈表)
template<class DataType>
LinkList<DataType>::LinkList()
{first = new Node<DataType>;first->next = NULL;
}
4.2.1 頭插法構造單鏈表
頭插法是每次將新申請的結點插在頭結點后面 :
template<class DataType>
LinkList<DataType>::LinkList(DataType a[], int n)
{first = new Node<DataType>;first->next = NULL;for (int i = 0; i < n; i++){Node<DataType> *s = new Node<DataType>;s->data = a[i];s->next = first->next;first->next = s;}
}
4.2.2 尾插法構造單鏈表
尾插法就是每次將新申請的結點插在終端節點的后面
template<class DataType>
LinkList<DataType>::LinkList(DataType a[], int n)
{first = new Node<DataType>;Node<DataType> *r = first;for (int i = 0; i < n; i++){Node<DataType> *s = new Node<DataType>;s->data = a[i];r->next = s;r = s;}r->next = NULL;
}
4.2.3 析構函數
單鏈表類中的結點是用new申請的,在釋放的時候無法自動釋放,所以,析構函數要將單鏈表中的結點空間釋放
template<class DataType>
LinkList<DataType>::~LinkList()
{while (first != NULL){Node<DataType>* q = first;first = first->next;delete q;}
}
4.2.4 計算長度
單鏈表中不能直接求出長度,所以我們只能將單鏈表掃描一遍,所以時間復雜度為O(n)。
template<class DataType>
int LinkList<DataType>::Length()
{Node<DataType>* p = first->next;int count = 0;while (p != NULL){p = p->next;count++;}return count;
}
4.2.5 按位查找
單鏈表中即使知道節點位置也不能直接訪問,需要從頭指針開始逐個節點向下搜索,平均時間性能為O(n),單鏈表是順序存取結構。
template<class DataType>
DataType LinkList<DataType>::Get(int i)
{Node<DataType>* p = first->next;int count = 1;while (p != NULL && count<i){p = p->next;count++;}if (p == NULL) throw "Location";else return p->data;
}
4.2.6按值查找
單鏈表中按值查找與順序表中的實現方法類似,對鏈表中的元素依次進行比較,平均時間性能為O(n)。
template<class DataType>
int LinkList<DataType>::Locate(DataType x)
{Node<DataType> *p = first->next;int count = 1;while (p != NULL){if (p->data == x) return count;p = p->next;count++;}return 0;
}
4.2.7 插入
單鏈表在插入過程中需要注意分析在表頭、表中間、表尾的三種情況,由于單鏈表帶頭結點,這三種情況的操作語句一致,不用特殊處理,時間復雜度為O(n)。
template<class DataType>
void LinkList<DataType>::Insert(int i, DataType x)
{Node<DataType> *p = first;int count = 0;while (p != NULL && count<i - 1){p = p->next;count++;}if (p == NULL) throw "Location";else {Node<DataType> *s = new Node<DataType>;s->data = x;s->next = p->next;p->next = s;}
}
4.2.8 刪除
刪除操作時需要注意表尾的特殊情況,此時雖然被刪結點不存在,但其前驅結點卻存在。因此僅當被刪結點的前驅結點存在且不是終端節點時,才能確定被刪節點存在,時間復雜度為O(n)。
template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{Node<DataType> *p = first;int count = 0;while (p != NULL && count<i - 1){p = p->next;count++;}if (p == NULL || p->next == NULL) throw "Location";else {Node<DataType> *q = p->next;int x = q->data;p->next = q->next;return x;}
}
4.2.9 遍歷
遍歷單鏈表時間復雜度為O(n)。
template<class DataType>
void LinkList<DataType>::PrintList()
{Node<DataType> *p = first->next;while (p != NULL){cout << p->data << endl;p = p->next;}
}
完整代碼:
#include<iostream>
using namespace std;template<class DataType>
struct Node
{DataType data;Node<DataType> *next;
};template<class DataType>
class LinkList
{
public:LinkList(); LinkList(DataType a[], int n); ~LinkList(); int Length(); DataType Get(int i); int Locate(DataType x); void Insert(int i, DataType x); DataType Delete(int i); void PrintList();
private:Node<DataType> *first;
};template<class DataType>
LinkList<DataType>::LinkList()
{first = new Node<DataType>;first->next = NULL;
}template<class DataType>
LinkList<DataType>::LinkList(DataType a[], int n)
{first = new Node<DataType>;first->next = NULL;for (int i = 0; i < n; i++){Node<DataType> *s = new Node<DataType>;s->data = a[i];s->next = first->next;first->next = s;}
}template<class DataType>
LinkList<DataType>::~LinkList()
{while (first != NULL){Node<DataType>* q = first;first = first->next;delete q;}
}template<class DataType>
int LinkList<DataType>::Length()
{Node<DataType>* p = first->next;int count = 0;while (p != NULL){p = p->next;count++;}return count;
}template<class DataType>
DataType LinkList<DataType>::Get(int i)
{Node<DataType>* p = first->next;int count = 1;while (p != NULL && count<i){p = p->next;count++;}if (p == NULL) throw "Location";else return p->data;
}template<class DataType>
int LinkList<DataType>::Locate(DataType x)
{Node<DataType> *p = first->next;int count = 1;while (p != NULL){if (p->data == x) return count;p = p->next;count++;}return 0;
}template<class DataType>
void LinkList<DataType>::Insert(int i, DataType x)
{Node<DataType> *p = first;int count = 0;while (p != NULL && count<i - 1){p = p->next;count++;}if (p == NULL) throw "Location";else {Node<DataType> *s = new Node<DataType>;s->data = x;s->next = p->next;p->next = s;}
}template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{Node<DataType> *p = first;int count = 0;while (p != NULL && count<i - 1){p = p->next;count++;}if (p == NULL || p->next == NULL) throw "Location";else {Node<DataType> *q = p->next;int x = q->data;p->next = q->next;return x;}
}template<class DataType>
void LinkList<DataType>::PrintList()
{Node<DataType> *p = first->next;while (p != NULL){cout << p->data << endl;p = p->next;}
}int main()
{LinkList<int> p;p.Insert(1, 6);p.Insert(2, 9);p.PrintList();p.Insert(2, 3);p.PrintList();cout << p.Get(2) << endl;cout << p.Locate(9) << endl;cout << p.Length() << endl;p.Delete(1);p.PrintList();getchar();return 0;
}
鏈式存儲的優缺點
優點:插入、刪除不需移動其他元素,只需改變指針;鏈表各個節點在內存中空間不要求連續,空間利用率高
缺點: 查找需要遍歷操作,比較麻煩。
五、其他線性表
5.1 循環鏈表
循環鏈表是另一種形式的鏈式存儲結構。它的特點是表中最后一個結點的指針域指向頭結點,整個鏈表形成一個環。(通常為了使空表和非空表的處理一致,附加一個頭結點) 。
在很多實際問題中,一般都使用尾指針來指示循環鏈表,因為使用尾指針查找開始結點和終端結點都很方便。
循環鏈表沒有增加任何存儲量,僅對鏈接方式稍作改變,循環鏈表僅在循環條件與單鏈表不同。從循環鏈表的任一結點出發可掃描到其他結點,增加了靈活性。但是,由于循環鏈表沒有明顯的尾端,所以鏈表操作有進入死循環的危險。通常以判斷指針是否等于某一指定指針來判定是否掃描了整個循環鏈表。
5.2 雙鏈表
循環鏈表雖然可以從任意結點出發掃描其他結點,但是如果要查找其前驅結點,則需遍歷整個循環鏈表。為了快速確定任意結點的前驅結點,可以再每個節點中再設置一個指向前驅結點的指針域,這樣就形成了雙鏈表。
存儲方法如下:
template<class DataType>
struct Node
{DataType data;Node<DataType> *prior,*next;
};
結點p的地址既存儲在其前驅結點的后繼指針域內,又存儲在它后繼結點的前驅指針域中。需要注意:
循環雙鏈表中求表長、按位查找、按值查找、遍歷等操作的實現與單鏈表基本相同。插入操作需要修改4個指針,并且要注意修改的相對順序。
完整代碼:
#include<iostream>
using namespace std;template<class DataType>
struct DulNode
{DataType data;DulNode<DataType> *prior,*next;
};template<class DataType>
class LinkList
{
public:LinkList(); LinkList(DataType a[], int n); //有參數構造~LinkList(); //析構int Length(); // 計算列表長度DataType Get(int i); // 按位查找int Locate(DataType x); // 按值查找void Insert(int i, DataType x); // 插入DataType Delete(int i); // 刪除void PrintList(); // 打印列表
private:DulNode<DataType> *first; // 附加頭結點
};template<class DataType>
LinkList<DataType>::LinkList()
{first = new DulNode<DataType>;first->next = first;first->prior = first;
}template<class DataType>
LinkList<DataType>::LinkList(DataType a[], int n)
{first = new DulNode<DataType>;first->next = NULL;for (int i = 0; i < n; i++){DulNode<DataType> *s = new DulNode<DataType>;s->data = a[i];s->prior = first;s->next = first->next;first->next->prior = s;first->next = s;}
}template<class DataType>
LinkList<DataType>::~LinkList()
{while (first->next != first){DulNode<DataType>* q = first;first->prior->next = first->next;first = first->next;delete q;}delete first;
}template<class DataType>
int LinkList<DataType>::Length()
{DulNode<DataType>* p = first->next;int count = 0;while (p != first){p = p->next;count++;}return count;
}template<class DataType>
DataType LinkList<DataType>::Get(int i)
{DulNode<DataType>* p = first->next;int count = 1;while (p != first && count<i){p = p->next;count++;}if (p == first && count!=1) throw "Location";else return p->data;
}template<class DataType>
int LinkList<DataType>::Locate(DataType x)
{DulNode<DataType> *p = first->next;int count = 1;while (p != first){if (p->data == x) return count;p = p->next;count++;}return 0;
}template<class DataType>
void LinkList<DataType>::Insert(int i, DataType x)
{DulNode<DataType> *p = first;int count = 0;while (p->next != first && count<i - 1){p = p->next;count++;}if (count!=0 && p == first) throw "Location";else {DulNode<DataType> *s = new DulNode<DataType>;s->data = x;s->prior = p;s->next = p->next;p->next->prior = s;p->next = s;}
}template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{DulNode<DataType> *p = first;int count = 0;while (p->next != first && count<i - 1){p = p->next;count++;}if (p == first && count!=0) throw "Location";else {DulNode<DataType> *q = p->next;int x = q->data;q->next->prior = q->prior;q->prior->next = q->next;return x;}
}template<class DataType>
void LinkList<DataType>::PrintList()
{DulNode<DataType> *p = first->next;while (p != first){cout << p->data << endl;p = p->next;}
}int main()
{LinkList<int> p;p.Insert(1, 6);p.Insert(2, 9);p.PrintList();p.Insert(2, 3);p.PrintList();cout << p.Get(2) << endl;cout << p.Locate(9) << endl;cout << p.Length() << endl;p.Delete(1);p.PrintList();getchar();return 0;
}
運行結果:
5.3 靜態鏈表
靜態鏈表是用數組來表示單鏈表,用數組元素的下標來模擬單鏈表的指針。
靜態鏈表存儲示意圖:
靜態鏈表插入操作示意圖:
靜態鏈表刪除操作示意圖:
靜態鏈表雖然是用數組來存儲線性表的元素,但在插入和刪除操作時,只需要修改游標,不需要移動表中的元素,從而改進了在順序表中插入和刪除操作需要移動大量元素的缺點,但是它并沒有解決連續存儲分配帶來的表長難以確定的問題。
完整代碼:
#include<iostream>
using namespace std;const int MaxSize = 100;template <class DataType>
class StaticList
{
public:typedef struct{DataType data;int next;}Node;StaticList(); //StaticList(DataType a[],int n); //~StaticList(){} //int Length(){return length;} //DataType Get(int i); //int Locate(DataType x); //void Insert(int i,DataType x); //DataType Delete(int i); //void PrintList(); //
private:Node SList[MaxSize]; //int length; //int avail;
};template<class DataType>
StaticList<DataType>::StaticList()
{length = 0;for(int i = 1;i<MaxSize-1;i++){SList[i].next = i+1;}SList[0].next = -1;SList[MaxSize-1].next = -1;avail = 1;
}template<class DataType>
StaticList<DataType>::StaticList(DataType a[],int n)
{length = 0;avail = 1;for(int i = 0;i<MaxSize-1;i++){SList[i].next = i+1;}SList[MaxSize-1].next = -1;SList[0].next = avail;for(int j = avail;j<=n;j++){SList[j].data = a[j-1];SList[j].next = j+1;}
}template<class DataType>
DataType StaticList<DataType>::Get(int i)
{int m = 0;if(i>length || i<1) throw "Location";else{for(int j = 0;j<i;j++){m = SList[m].next;}return SList[m].data;}
}template<class DataType>
int StaticList<DataType>::Locate(DataType x)
{int m = 0;for(int i = 0;i<length;i++){m = SList[m].next;if(SList[m].data == x) return i;}return 0;
}template<class DataType>
void StaticList<DataType>::Insert(int i,DataType x)
{if(length == MaxSize - 2) throw "Full";else if(i<1 || i>length+1) throw "Location";else{int m = 0;for(int j = 0;j<i-1;j++){m = SList[m].next;}int t = avail;avail = SList[avail].next;int s = SList[m].next;SList[m].next = t;SList[t].data = x;SList[t].next = s;length++;}
}template<class DataType>
DataType StaticList<DataType>::Delete(int i)
{if(i<1 || i>length) throw "Location";else{int m = 0;for(int j = 0;j<i-1;j++){m = SList[m].next;}DataType temp = SList[SList[m].next].data;int t = SList[m].next;SList[m].next = SList[SList[m].next].next;SList[t].next = avail;avail = t;length--;return temp;}
}template<class DataType>
void StaticList<DataType>::PrintList()
{int m = 0;for(int i = 0;i<length;i++){m = SList[m].next;cout<<m<<":";cout<<SList[m].data<<" ";}cout<<endl;
}int main()
{StaticList<int> p;p.Insert(1,3);p.Insert(2,6);p.PrintList();p.Insert(2,9);p.PrintList();cout<<p.Get(2)<<endl;cout<<p.Length()<<endl;cout<<p.Locate(6)<<endl;p.Delete(1);p.PrintList();return 0;
}
運行結果:
5.4 間接尋址
間接尋址是將數組和指針結合起來的一種方法,它將數組中存儲的單元改為存儲指向該元素的指針。該算法的時間復雜度仍為O(n),但當每個元素占用較大空間時,比順序表的插入快的多。線性表的間接尋址保持了順序表隨機存取的優點,同時改進了插入和刪除操作的時間性能,但是它也沒有解決連續存儲分配帶來的表長難以確定的問題。
完整代碼:
#include<iostream>
using namespace std;const int MaxSize = 100;template <class DataType>
class SeqList
{
public:typedef struct{DataType data;}Node;SeqList(){length=0;} //SeqList(DataType a[],int n); //~SeqList(); //int Length(){return length;} //DataType Get(int i); //int Locate(DataType x); //void Insert(int i,DataType x); //DataType Delete(int i); //void PrintList(); //
private:Node *IndAdd[MaxSize]; //int length; //
};template <class DataType>
SeqList<DataType>::SeqList(DataType a[],int n)
{for(int i = 0;i<n;i++){Node *p = new Node;p->data = a[i];IndAdd[i] = p;}length = n;
}template <class DataType>
SeqList<DataType>::~SeqList()
{for(int i = 0;i<length;i++){delete IndAdd[i];}
}template <class DataType>
DataType SeqList<DataType>::Get(int i)
{if(i<1 || i>length) throw "Location";else{return IndAdd[i-1]->data;}
}template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{for(int i = 0;i<length;i++){if(IndAdd[i]->data = x) return i;}return 0;
}template <class DataType>
void SeqList<DataType>::Insert(int i,DataType x)
{if(length>=MaxSize) throw "FULL";else if(i>length+1 || i<1) throw "Location";else{for(int j = length;j>=i-1;j--){IndAdd[j] = IndAdd[j-1];}Node *p = new Node;p->data = x;IndAdd[i-1] = p;length++;}
}template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{Node *p = IndAdd[i-1];DataType x = p->data;delete p;for(int j = i-1;j<length;j++){IndAdd[j] = IndAdd[j+1];}length--;return x;
}template <class DataType>
void SeqList<DataType>::PrintList()
{for(int i = 0;i<length;i++){Node *p = IndAdd[i];cout<<p->data<<" ";}cout<<endl;
}int main()
{SeqList<int> p;p.Insert(1,5);p.Insert(2,9);p.PrintList();p.Insert(2,3);cout<<p.Length()<<endl;p.PrintList();cout<<p.Get(3)<<endl;p.Delete(2);p.PrintList();getchar();return 0;
}
運行結果:
總結
- 上一篇: 上海同济口腔医院 补牙多少钱?拔智齿多少
- 下一篇: 雪地画梅的下一句是什么呢?