您的位置:首页 > 理论基础 > 数据结构算法

复习(数据结构):链表:c++:标准

2016-07-17 13:28 585 查看

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: