您好,登錄后才能下訂單哦!
這篇文章主要講解了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; }
看完上述內容,是不是對C++如何實現線性表鏈式存儲有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。