复习(数据结构):链表:c++:标准
来源:互联网 发布:软件项目方案 编辑:程序博客网 时间:2024/05/03 14:21
1 . 数据结构
Chain<T>是ChainNode<T>的友类,所以Chain<T>可以访问ChainNode<T>的所有成员(私有或公有)
#include<iostream>#include<vector>#include<cmath>#include<algorithm>using namespace std;template<class T>class ChainNode{ friend Chain<T>;private: T data; ChainNode<T> *link;};template <class T>class Chain{public: Chain(){first=0;} // 0 代表NULL ~Chain(); bool IsEmpty() const{return first==0;} int Length() const; bool Find(int k,T& x) const; int Search(const T& x) const; Chain<T>& Delete(int k,T& x); Chain<T>& Insert(int k,const T& x); void Output(ostream& out) const;private: ChainNode<T> *first; // 指向第一个节点的指针};
2. 函数的实现
#include<iostream>#include<vector>#include<cmath>#include<algorithm>using namespace std;template <class T> class Chain;template <class T>class ChainNode{ friend class Chain<T>;private: T data; ChainNode<T> *link;};template <class T>class Chain{public: Chain(){first=0;} // 0 代表NULL ~Chain(); bool IsEmpty() const{return first==0;} int Length() const; bool Find(int k,T& x) const; int Search(const T& x) const; Chain<T>& Delete(int k,T& x); Chain<T>& Insert(int k,const T& x); void Output(ostream& out) const;private: ChainNode<T> *first; // 指向第一个节点的指针};/***************************************************/template <class T>Chain<T>::~Chain() { ChainNode<T> *next; while(first){ next=first->data; delete first; first=next; }}template <class T>Chain<T>& Chain<T>::Delete(int k, T &x) { if(k<1||!first) throw OutOfBounds(); //no k'th ChainNode<T> *p=first; if(k==1) first=first->link; else{ ChainNode<T>* q = first; for(int index=1;index<k-1 && q;q=q->link) q=q->link; if(!q||!q->link) throw OutOfBounds(); //no k'th p=q->link; 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 OutOfBounds(); ChainNode<T>* p=first; for(int index=1;index<k && p;index++) p=p->link; if(k>0 && !p) throw OutOfBounds(); //insert ChainNode<T>* y=new ChainNode<T>; y->data=x; if(k){ // 在p后插入 y->link=p->link; p->link=first; }else{ // 如果是第一个元素 y->link=first; first=y; } return *this;}template <class T>void Chain<T>::Output(ostream& out)const{ ChainNode<T>* current; for(current=first;current;curent=current->link) cout<<current->data<< " ";}template <class T>ostream& operator<<(ostream& out,const Chain<T>& x){ x.Output(out); return Out;}/****************************************************/
3.测试
void main(void){ try{ chain<int> L; cout<<"Length= "<<L.length()<<endl; cout<<"isEmpty= "<<L.IsEmpty()<<endl; L.Insert(0,2).Insert(1,6); }catch(...){ cerr<<"An exception has occured"<<endl; }}
4. 添加遍历器
/***************************************/template <class T>class ChainNode{ friend class Chain<T>; firend class ChainIterator<T>;private: T data; ChainNode<T> *link;};/******************************************/template <class T> class ChainIterator;template <class T>class Chain{friend ChainIterator<T>;public: Chain(){first=0;} // 0 代表NULL ~Chain(); bool IsEmpty() const{return first==0;} int Length() const; bool Find(int k,T& x) const; int Search(const T& x) const; Chain<T>& Delete(int k,T& x); Chain<T>& Insert(int k,const T& x); void Output(ostream& out) const;private: ChainNode<T> *first; // 指向第一个节点的指针};/***************************************/template<class T>class ChainIterator{public: T* Initialize(const Chain<T>& c){ location = c.first; if(location) return &location->data; return 0; } T* Next(){ if(!location) return 0; location = location->link; if(location) return &location->data; return 0; }private: ChainNode<T>* location;}
- 迭代器的测试函数
void main(void){ chain<int> L; L.Insert(0,2); L.Insert(1,6); L.Insert(2,8); L.Insert(1,4); ChainIterator<int> p; int *q=p.Initialize(L); cout<<"List in sequece is "; while(q){ cout<<*q<<' '; q=p.next; } cout<<endl;}
0 0
- 复习(数据结构):链表:c++:标准
- 复习(数据结构):线性表 : c++_标准写法
- 复习(数据结构):链表:c语言
- 复习(数据结构):链表:c++:stl
- C 标准库 复习
- 复习(数据结构):线性表 : C
- 复习(数据结构):队列:c语言
- 复习(数据结构):字符串:c语言
- 复习(数据结构):树:C语言
- c语言数据结构复习(一)
- K&R复习,数据结构(C语言版)复习,算法导论复习
- 数据结构复习-链表
- C语言与数据结构复习重点
- 【C语言和数据结构复习】序
- 复习(数据结构):线性表 : C:动态分配内存
- 复习(数据结构):动态数组:c++_stl写法
- 复习(数据结构):栈:c语言:数组
- 复习(数据结构):栈:c语言:动态数组
- Github 创建新分支
- discuzx2后台自带备份功能问题
- 总结
- 第14章 高级I/O
- linux系统硬件配置查看指令
- 复习(数据结构):链表:c++:标准
- Servlet生命周期与工作原理
- FatMouse' Trade
- 导入三方库是出现NotFount
- 滨州学院CSDN高校俱乐部第二届线下编程比赛
- 软件总体测试计划
- CANoe CAPL语言Step2
- 操作系统之存储器管理
- Anaconda 镜像