C++實現線性錶鏈式儲存(單鏈)
阿新 • • 發佈:2020-05-26
本文例項為大家分享了C++實現線性錶鏈式儲存的具體程式碼,供大家參考,具體內容如下
實現的功能:
1、定義了三中傳入不同引數的建構函式,用於初始化建立不同的連結串列;
2、能實現增、刪、查等基本功能;
存在的問題:
當建立一個已知大小的空連結串列後,連結串列中的資料並不為空,見下圖:
下面是程式碼及測試結果:
singlelinklist.h
#pragma once #include "iostream" #include "exception" #include "stdlib.h" #include "malloc.h" using namespace std; //結點類 template<class T> class Node { public: T data; Node<T> *next; Node() { this->next = NULL; } Node(T data,Node<T>* next = NULL) { this->data = data; } ~Node() {}; }; //定義連結串列類 template<class T> class SLinkList { public: Node<T> node; //結點 Node<T>* head; //頭指標 SLinkList(); //建立連結串列 SLinkList(int num,T elem[]); SLinkList(int num); ~SLinkList(); int LengthSLinkList(); //表長 void InsertNode(int i,T elem); //插入結點 void InsertNode(T elem); void DeleteNode(int i); //刪除結點 void DeleteAllNode(); //刪除表 T GetElem(int i); //按序號查詢 int* GetNum(T elem); //按元素查詢 void OutputList(); //輸出 }; template<class T> SLinkList<T>::SLinkList() { Node<T>* p = new Node<T>(); this->head = p; cout << "finish<SLinkList()>!" << endl; } template<class T> SLinkList<T>::SLinkList(int num,T elem[]) { try { if(num < 1) throw length_error("輸入初始化num引數必須大於1!"); else { Node<T>* p = new Node<T>(); this->head = p; Node<T>* temp = this->head; for (int i = 0; i < num; i++) { temp->next = new Node<T>(*(elem + i)); //這裡犯了一個錯,就是把elem[i]直接丟進去,()裡面放的是T型別初始值,實參傳進來的是T* elem[],無法用下標進行訪問 temp = temp->next; } temp->next = NULL; cout << "finish<SL]inkList(int num,T elem[])>!" << endl; } } catch(length_error e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> SLinkList<T>::SLinkList(int num) { try { if (num < 1) throw length_error("輸入初始化引數num必須大於1!"); else { Node<T>* p = new Node<T>(); this->head = p; Node<T>* temp = this->head; for(int i = 1; i <= num; i++) { temp->next = new Node<T>(); temp = temp->next; } temp->next = NULL; cout << "finish<SLinkList(int num)>!" << endl; } } catch(length_error e) { cout << "info:" << e.what() << endl; exit(0); } } //呼叫函式析構 /* template<class T> SLinkList<T>::~SLinkList() { this->DeleteAllNode(); } */ template<class T> SLinkList<T>::~SLinkList() { Node<T>* p = this->head; Node<T>* temp = p->next; Node<T>* q = NULL; while(temp) { q = temp; temp = temp->next; delete q; } delete p; delete temp; cout << "finish ~SLinkList()" << endl; } template<class T> int SLinkList<T>::LengthSLinkList() { int count = 0; Node<T>* p = this->head; Node<T>* temp = p->next; while (temp) { count++; temp = temp->next; } return count; } template<class T> void SLinkList<T>::InsertNode(int i,T elem) { try { if(i<1 || i>LengthSLinkList() + 1) throw out_of_range("輸入的引數i值超出了連結串列的範圍!"); else { if (i != LengthSLinkList()+1) //1-len之間插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this->head; for (int j = 1; j < i; j++) temp = temp->next; Node<T>* p = temp->next; temp->next = Elem; Elem->next = p; } else InsertNode(elem); //在末尾插入 cout << "finish<Insert(int i,T elem)>!" << endl; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(0); } } template<class T> void SLinkList<T>::InsertNode(T elem) //末尾插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this->head; Node<T>* p = NULL; while (temp) { p = temp; temp = temp->next; } p->next = Elem; Elem->next = NULL; cout << "finnish<InsertNode(T elem)>!" << endl; } template<class T> void SLinkList<T>::DeleteNode(int i) //返回的是被刪除的值 { try { if (i<1 || i > LengthSLinkList()) throw out_of_range("輸入的引數i值超出了連結串列的範圍!"); else { Node<T>* temp = this->head; for (int j = 1; j < i; j++) temp = temp->next; Node<T> *p = temp->next; temp->next = p->next; cout << "finish<DeleteNode(int i)>!" << endl; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> void SLinkList<T>::DeleteAllNode() { Node<T>* temp = this->head; int count = this->LengthSLinkList(); while(count) { DeleteNode(count); count--; } delete temp; cout << "finish<DeleteAllNode()>!" << endl; } template<class T> T SLinkList<T>::GetElem(int i) { try { if(i<1 || i > LengthSLinkList()) throw out_of_range("輸入引數i值超出了連結串列的範圍!"); else { Node<T>* temp = this->head; for(int j = 1; j <=i; j++) temp = temp->next; cout << "finish!" << endl; cout << "finish<GetElem(int i)>! " << endl; return temp->data; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> int* SLinkList<T>::GetNum(T elem) { Node<T>* p = this->head; Node<T>* temp = p->next; int* count = new int[LengthSLinkList()]; int j = 0; for(int i = 1; i <= LengthSLinkList(); i++) { if(temp->data == elem) { count[j] = i; j++; } else temp = temp->next; } cout << "finish<GetNum(T elem)>!" << endl; return count; } template<class T> void SLinkList<T>::OutputList() { Node<T>* p = this->head; Node<T>* temp = p->next; cout << "output list:" ; for(int i = 1; i <= LengthSLinkList(); i++) { cout << temp->data << " " ; temp = temp->next; } cout << endl; }
main.cpp
#include "iostream" #include "singlelinklist.h" using namespace std; int main() { //Init:測試初始化建構函式 //operator:測試增、刪、查 //標註行程式碼為異常測試,主要包括length_error和out_of_range //最後輸出的是解構函式,下面程式碼行中未給出,見.h檔案中的解構函式定義 cout << "**************************************************" << endl; cout << "test Innit<SLinkList()>" << endl; SLinkList<int> sqlinklist1; cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num,T elem[])>" << endl; int num = 5; int a[] = {1,2,3,4,5}; cout << "input list:" ; for(int i = 0; i < num; i++) cout << a[i] << " "; cout << endl; SLinkList<int> sqlinklist2(num,a); cout << "length:" << sqlinklist2.LengthSLinkList() << endl; sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> error1(0,a); cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num)>" << endl; int num1 = 5; SLinkList<int> slistlink3(num1); cout << "length:" << slistlink3.LengthSLinkList() << endl; slistlink3.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> errror(0); cout << "**************************************************" << endl; cout << "test operation<InsertNode(int i,T elem)>" << endl; sqlinklist2.InsertNode(2,1996); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0,0); //error.InsertNode(7,0); cout << "test operation<InsertNode(T elem)>" << endl; sqlinklist2.InsertNode(256); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0); //error.InsertNode(7); cout << "**************************************************" << endl; cout << "test operation<DeleteNode(int i)>" << endl; sqlinklist2.DeleteNode(1); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.DeleteNode(18); //cout << "test operation<DeleteAllNode()>" << endl; //sqlinklist2.DeleteAllNode(); cout << "**************************************************" << endl; cout << "test operation<GetElem(int i)>" << endl; cout << "num '2' is:" << sqlinklist2.GetElem(2) << endl; //cout << "test error" << endl; //error.DeleteNode(18); cout << "test operation<GetNum(T elem)>" << endl; cout << "elem '2' is :" << *(sqlinklist2.GetNum(2)) << endl; cout << endl; return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。