简单链表实现

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

//头文件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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值