[04.28更新]C++ 单向链表的实现

来源:互联网 发布:数据噪声 编辑:程序博客网 时间:2024/06/16 05:44

参考:http://www.cnblogs.com/wxxweb/archive/2011/05/26/2058245.html

http://my.oschina.net/ijaychen/blog/164330

链表节点类ChainNode和链表类Chain

chain.h 

// 链表模板类前置声明template<class T>class Chain;// 节点类 ChainNodetemplate<class T>class ChainNode{// Chain<T>可以访问ChainNode<T>的所有成员(尤其是私有成员)friend class Chain<T>; // 或 friend Chain<T>private:T data;  // 数据域ChainNode<T> *link; // 指针域};// 单链表类 Chaintemplate <class T>class Chain{public:// 构造函数,无参数,初始化为:第一个节点的指针为空;Chain(){ first = 0; }  // 或者 first = NULL// 析构函数~Chain();// 判断单链表是否为空,如果第一个节点指针的first为Null或零,则为空bool IsEmpty() const { return first == 0; } // 单链表长度函数(节点个数)int Length() const;// 查找第k个元素并保存在x中bool Find(int k, T& x) const; // 查找第k个元素并保存在x中// 在单链表中寻找x,如果发现x,则返回x的地址(index,次序) int Search(const T& x) const;// 把第k个元素取至x,然后从链表中删除第k个元素,返回删除第k个元素后的链表Chain<T>& Delete(int k, T& x);// 在第k个元素之后插入xChain<T>& Insert(int k, const T& x);// 按顺序输出单链表void Output() const;private:ChainNode<T> *first; // 指向第一个节点的指针}; // 析构函数,从开始删除template<class T>Chain<T>::~Chain(){ChainNode<T> *next; // 下一个节点while (first){next = first->link;delete first;first = next;}}// 在链表中插入元素template<class T>Chain<T>& Chain<T>::Insert(int k, const T& x){// 在第k个元素之后插入xChainNode<T> *p = first; // p最终指向第k个节点// 将p指向第k个元素(第k-1个元素的指针域)for (int index = 1; index < k && p; ++index)p = p->link;// 插入ChainNode<T> *y = new ChainNode<T>; // 新的节点y->data = x;if (k) // 插入节点{y->link = p->link;p->link = y;}else // 插入第一个节点(k=0){y->link = first;first = y;}return *this;}// 输出链表template<class T>void Chain<T>::Output() const{ChainNode<T> *current;int count = 1;for (current = first; current; current = current->link){cout << "第" << count << "个元素: "<< current->data << ";" << endl;; // (*current).data++count;}}// 确定链表的长度template<class T>int Chain<T>::Length() const{ChainNode<T> *current = first;int len = 0;while (current){++len;current = current->link;}return len;}// 在链表中查找第k个元素template<class T>bool Chain<T>::Find(int k, T& x) const{// 寻找链表中第k个元素,并将其返回给x// 如果不存在第k个元素,则返回false,否则返回trueif (k < 1) return false;ChainNode<T> *current = first;int index = 1; // current的索引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{// 寻找x,如果发现x,则返回x的地址// 如果x不再链表中,则返回0ChainNode<T> *current = first;int index = 1; // current的索引while (current && current->data != x){current = current->link;++index;}if (current) return index;return 0;}// 在链表中删除一个元素template<class T>Chain<T>& Chain<T>::Delete(int k, T& x){// 把第k个元素取至x,然后从链表中删除第k个元素ChainNode<T> *p = first; // p最终指向第k个元素if (k == 1) // p已经指向第k个元素first = first->link; // 删除else{ChainNode<T> *q = first;for (int index = 1; index < k - 1 && q; ++index)q = q->link;p = q->link; // 存在第k个元素q->link = p->link; // 删除第k个元素}x = p->data;delete p;return *this;}

test.cpp

#include<iostream>#include "chainnode.h"using namespace std;int main(){Chain<int> myChain;// 插入新元素int data[] = { 0, 2, 9, 13, 6, 2, 8, 25, 6 };int len = sizeof(data) / sizeof(int);for (int i = 0; i < len; ++i)myChain.Insert(i, data[i]);// 输出链表myChain.Output();// 链表长度cout << "链表长度为: " << myChain.Length() << "." << endl;// 查找第k个元素int x = 0;int k = 3;myChain.Find(k, x);cout << "第" << k << "个元素为: " << x << "." << endl;// 搜索x = 8;cout << x << " 是第 " << myChain.Search(x) << " 个元素." << endl;// 删除一个元素k = 3;myChain.Delete(k, x);cout << "删除第 " << k << " 个元素后,变为:" << endl;myChain.Output();return 0;}

输出结果为:

第1个元素: 0;
第2个元素: 2;
第3个元素: 9;
第4个元素: 13;
第5个元素: 6;
第6个元素: 2;
第7个元素: 8;
第8个元素: 25;
第9个元素: 6;
链表长度为: 9.
第3个元素为: 9.
8 是第 7 个元素.
删除第 3 个元素后,变为:
第1个元素: 0;
第2个元素: 2;
第3个元素: 13;
第4个元素: 6;
第5个元素: 2;
第6个元素: 8;
第7个元素: 25;
第8个元素: 6;
请按任意键继续. . .




                                             
0 0
原创粉丝点击