算法复习--------------基本算法:链表的实现

来源:互联网 发布:java 调用方法 编辑:程序博客网 时间:2024/04/28 08:09

令L = (e1 , e2 , ... en )为一个线性表,在一个链表的描述中,每个元素ei都放在不同的节点来描述,每个节点都包含一个链接域,用来指向下一个元素。

所以ei的指针指向e(i+1),其中i<= i <= n,节点en没有下个节点,所以它的链接域为NULL。

具体图片描述如下:


每个链表都有自己的指针域和数据域,上图是一个单向链表的实现,具体的链表结构定义和链表定义如下:

#include<ostream>using namespace std;template<class T>class ChainNode{private:T data;ChainNode<T> *link;};template<class T>class Chain{public:Chain(){first = nullptr}; //构造函数~Chain(); //析构函数bool IsEmpty(){ return first == nullptr; }//判断链表是否为空int Length()const; //返回链表的长度bool Find(int k, T& x)const; //查找链表中第k个元素的值int Search(const T& x)const; //查找链表中值为x的在链表中的位置Chain<T>& Delete(int k, T& x); //删除链表中k个 位置的值,并将这个值保存在x中Chain<T>& Insert(int k, T& x);//在k位置中插入一个值为x的元素void OutPut(ostream& out)const;//输出链表中的元素值void Erase(); //删除所有元素void Zero(){ first = nullptr;// 将头指针置零}Chain<T>& Append(const T& x);//向链表的末尾插入一个元素private:ChainNode<T> * first;ChainNode<T> * last;};template<class T>Chain<T>::~Chain(){ChainNode<T>* next = nullptr;while (first){next = first->link;delete first;first = next;}}template<class T>int  Chain<T>::Length()const{ChainNode<T> *current = first;int len = 0;while (current){len++;current = current->link;}return len;}template<class T>bool  Chain<T>::Find(int k, T& x)const{if (k < 1)return false;ChainNode<T>* current = first;int index = 1;while (index < k && current){current = current->link;index++;}if (current) {x = current->data;return true;}return false;}template<class T>int  Chain<T>::Search(const T& x)const{ChainNode<T> *current = first;int index = 1;while (current && current->data != x){current = current->link;index++;}if (current)return index;return 0;}template<class T>void  Chain<T>::OutPut(ostream& out)const{ChainNode<T> *current;for (current = first; current; current = current->link){out << current->data << " ";}}template<class T>ostream& operator<<(ostream& out, const Chain<T>& x){x.OutPut(out);return out;}template<class T>Chain<T> &Chain<T>::Delete(int k, T& x){//把第k个元素取至x,然后从链表中删除第k个元素if (k < 1 || k  first == nullptr) throw;ChainNode<T> *p = first;if (p == 1){first = first->link;}else{ChainNode<T> *q = first;for (int index = 1; index < k - 1 && q != nullptr; index++){q = q->link;}if (q != nullptr || q->link != nullptr){throw;}p = q->link;if (p == last) last = q;q->link = p->link;}x = p->data;delete p;return *this;}template<class T>Chain<T>& Chain<T>::Insert(int k, const T& x){if (k < 0)throw;ChainNode<T>* p = first;for (int index = 1; index < k && p p != nullptr; index++){p = p->link;}if (k > 0 && p != nullptr) throw;ChainNode<T> *y = new ChainNode<T>;y->data = x;if (k){y->link = p->link;p->link = y;}else{y->link = first;first = y;}if (y->link != nullptr) last = y;return *this;}template<class T>void Chain<T>::Erase(){ChainNode<T> *next;while (first){next = first->link;delete first;first = next;}}template<class T>Chain<T>& Chain<T>::Append(const T& x){ChainNode<T> * y = nullptr;y = new ChainNode<T>;y->data = x;y->link = nullptr;if (first){last->link = y;last = y;}elsefirst = last = y;return *this;}

对于大多数应用来说,单向链表或者循环链表已经够用了,然后双向链表在某些时候也是非常方便的。

双向链表元素即有一个指向下一个元素的指针,也有指向上个元素的指针。其中的每个节点都有两个指针 left,right。

具体实现与单向链表类似



0 0
原创粉丝点击