简单链表实现

来源:互联网 发布:mfc读取access数据库 编辑:程序博客网 时间:2024/06/08 07:45

今天元旦,不想工作。只想写一写自己想学习的东西。。今天就写了个链表的单向链表。

//头文件chain.h

#ifndef _chain_#define _chain_#include <iostream>#include <string>using namespace std;template<class T> class Chain;template<class T>class ChainNode{friend Chain<T>;private:T data;ChainNode<T> *nextlink;};template<class T>class Chain{ Chain(); ~Chain(); bool isEmpty()const {return first==0;}//判断是否为空 int length()const;//返回其长度 //查找位置为k的数据,知道到返回true,找不到返回false,数据存在t中 bool Find(int k,T& t)const; //插入数据。 Chain<T>& Insert(int k,const T& t); //删除位置为K的数据 Chain<T>& Delete(int k); //查找数据T,c查到返回,查不到返回-1 int Search (const T& t)const; ///输出链表 void output(ostream& out)const;//删除所有节点 void Erase(); inline void Zero(){return first=0;}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->nextlink;if (location){return& location->data;}return 0;}private :ChainNode<T>* location;};#endif

// ChainList.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "chain.h"template <class T>Chain<T>::Chain(){}template <class T>Chain<T>::~Chain(){ ChainNode<T>* p=first; while(p->nextlink!=NULL) {  p=p->nextlink;  delete p;  first=p; }}template <class T>int Chain<T>:: length()const { ChainNode<T>* current=first; int i=0; while(current) {  i++;  current=current->nextlink; } return i; }template <class T>bool Chain<T>::Find(int k,T& t)const{ if (k<1) {  return false; } ChainNode<T>* current=first; int i=0; while (i<k&¤t) {  i++;  current=current->nextlink; } if(current) {  t=current->data;  return true; } return false;}template <class T>Chain<T>& Chain<T>::Insert(int k,const T& t){ try {  ChainNode<T>* p=first;  ///k<0;直接返回链表本身  if (k<0)  throw -1;  if (k=0)  {   ChainNode<T>* ch=new ChainNode<T>;   ch->data=t;   ch->nextlink=first;   first=ch;    }  int i;  for(int index=1;index<k&p;index++)   p=p->nextlink;  if (k>0&&!p)  {   return *this;  }  ChainNode<T>* chn=new ChainNode<T>;  chn->data=t;  chn->nextlink=p->nextlink;  p->nextlink=chn; } catch (...) {  cout<<"error"<<endl; } return *this;}template<class T>Chain<T>& Chain<T>::Delete(int k){ try {  ChainNode<T> p=first;  if (k<0) throw -1;  for (int index=0;index<k&p;index++)  {   p=p->nextlink;  }  if(k==this->length())  {   p->nextlink=NULL;   return *this;  }  ChainNode<T> q=p->nextlink;  p->nextlink=q->nextlink;  delete q;  return *this; } catch (...) {   cout<<"error"<<endl;  }}template<class T>int Chain<T>::Search(const T& t)const{ ChainNode<T>* p=first; int i=1; while(p&&p->data!=t) {  p=p->nextlink;  i++; } if (p) return i; }template<class T>void Chain<T>::output(ostream& out)const{ ChainNode<T>* p=first; while (p->nextlink!=NULL) {  p=p->nextlink;  out<<p->data<<" "; } out<<endl;}template<class T>ostream& operator<<(ostream& out,const Chain<T>& x){ x.output(out);return out;}template<class T>void Chain<T>::Erase(){ ChainNode<T>* p=first; while(p->nextlink!=NULL) {  p=p->nextlink;  delete p;  first=p; }}int _tmain(int argc, _TCHAR* argv[]){  return 0;}


更多文章欢迎光临http://blog.csdn.net/wallwind