線性表的鏈式存儲結構,用一組任意的存儲單元存儲數據元素。除了要存儲數據元素信息外,還要存儲他的後繼元素的存儲地址。node
優勢:(相對於線性表的順序存儲結構)ios
一、不須要連續的存儲單元。ide
二、單鏈表不須要預分配存儲空間,只要空間就能夠分配,元素的個數也不受限制。函數
三、單鏈表在找出某位置的指針後,插入和刪除的時間僅爲O(1).(可是,查找的時間仍是O(n)?這個是否算優勢?)測試
缺點:(相對於線性表的順序存儲結構)this
一、不只須要存儲數據元素,還須要存儲後繼元素的位置信息,佔用空間增長。spa
二、查找元素的時間複雜度是O(n),順序存儲結構的查找時間複雜度是O(1).指針
代碼:線性錶鏈式存儲結構的複製構造函數相比插入和刪除函數要複雜(我的觀點)。code
第一部分是 ChainNode.h文件,第二部分ChainNode.cpp文件,第三部分是測試代碼。blog
#pragma once template<class T> struct ChainNode { T node; ChainNode<T>* next; ChainNode(){} ChainNode(const T& element){ this->node = element; } ChainNode(const T& element, ChainNode<T>* Next){ this->node = element; this->next = Next; } }; template<class T> class ChainLinear { public: ChainLinear(); //構造函數 ChainLinear(const ChainLinear<T>&); //複製構造 ~ChainLinear(); //析構函數 void chainInsert(int index,const T&); //插入index處插入元素 void chainDelete(int index); //刪除index處的元素 T& getElement(int index); //返回index處的元素 int getLength(){ return this->length; } //返回元素個數 private: int length; ChainNode<T>* firstNode; };
#include "ChainLinear.h" #include<iostream> using namespace std; template<class T> ChainLinear<T>::ChainLinear() { length = 0; firstNode = NULL; } template<class T> ChainLinear<T>::ChainLinear(const ChainLinear<T>& theChain) { length = theChain.length; if (length == 0) //若鏈表爲空,則首地址指向空,並返回 { firstNode = NULL; return; } ChainNode<T>* sourceNode = theChain.firstNode; firstNode = new ChainNode<T>(sourceNode->node); sourceNode = sourceNode->next; ChainNode<T>* targeNode = firstNode; //當前鏈表 *this 的最後一個節點 while (sourceNode != NULL) { targeNode->next = new ChainNode<T>(sourceNode->node); targeNode = targeNode->next; sourceNode = sourceNode->next; } targeNode->next = NULL; } template<class T> ChainLinear<T>::~ChainLinear() { while (firstNode != NULL) { ChainNode<T>* currentNode = firstNode->next; delete firstNode; firstNode = currentNode; } } template<class T> void ChainLinear<T>::chainInsert(int index, const T&element) //插入index處插入元素 { if (index < 0) { cout << "請輸入正確的插入位置" << endl; system("pause"); } if (index >= this->length) index = this->length; if (index == 0) firstNode = new ChainNode<T>(element, firstNode); else { ChainNode<T>* temp = firstNode; for (int i = 0; i < index-1; i++) temp = temp->next; temp->next = new ChainNode<T>(element, temp->next); } this->length++; } template<class T> void ChainLinear<T>::chainDelete(int index) //刪除index處的元素 { if (index >= this->length || index < 0) { cout << "Error!!!" << endl; cout << "要刪除的元素超出範圍!" << endl; system("pause"); } ChainNode<T>*DeleteNode = firstNode; if (index == 0) { firstNode = firstNode->next; } else { ChainNode<T>*p = firstNode; for (int i = 0; i < index - 1; i++) p = p->next; DeleteNode = p->next; p->next = p->next->next; // p->next = DeleteNode->next; } delete DeleteNode; this->length--; } template<class T> T& ChainLinear<T>::getElement(int index) //返回index處的元素 { if (index >= this->length || index < 0) { cout << "Error!!!" << endl; cout << "要刪除的元素超出範圍!" << endl; system("pause"); } ChainNode<T>* theElement = firstNode; for (int i = 0; i < index; i++) theElement = theElement->next; return theElement->node; }
測試代碼:
#include<iostream> #include<string> #include"ChainLinear.h" #include"ChainLinear.cpp" using namespace std; struct Teacher { int age; string name; }; void print(ChainLinear<Teacher>list) { ChainLinear<Teacher>List(list); int size = List.getLength(); for (int i = 0; i < size; i ++ ) { Teacher t = List.getElement(0); cout << "the teacher's year is: " << t.age << endl; List.chainDelete(0); } } int main() { ChainLinear<Teacher>list; Teacher t0,t1, t2, t3, t4,tt; t0.age = 1; t1.age = 10; t2.age = 20; t3.age = 30; t4.age = 40; tt.age = 50; list.chainInsert(0, t0); list.chainInsert(0, t1); list.chainInsert(0, t2); list.chainInsert(0, t3); list.chainInsert(0, t4); print(list); list.chainInsert(2, tt); print(list); list.chainDelete(2); list.chainDelete(3); print(list); cout << "hello world" << endl; system("pause"); return 0; }