线性表的链式存储结构,用一组任意的存储单元存储数据元素。除了要存储数据元素信息外,还要存储他的后继元素的存储地址。
优点:(相对于线性表的顺序存储结构)
1、不需要连续的存储单元。
2、单链表不需要预分配存储空间,只要空间就可以分配,元素的个数也不受限制。
3、单链表在找出某位置的指针后,插入和删除的时间仅为O(1).(但是,查找的时间还是O(n)?这个是否算优点?)
缺点:(相对于线性表的顺序存储结构)
1、不仅需要存储数据元素,还需要存储后继元素的位置信息,占用空间增加。
2、查找元素的时间复杂度是O(n),顺序存储结构的查找时间复杂度是O(1).
代码:线性表链式存储结构的复制构造函数相比插入和删除函数要复杂(个人观点)。
第一部分是 ChainNode.h文件,第二部分ChainNode.cpp文件,第三部分是测试代码。
#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; }