用指针实现表

// list.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include"iostream.h"
template<class T> class list;

template<class T>
class node
{
 friend list<T>;
 private:
  T data;
  node<T>*next;
};

template<class T>
class list
{
 firend iterator <T>;
 public:
  list(){first=0;}
  ~list();
  bool empty()const{return fistt==0;}
  int size() const;
  bool retrieve(int k,T& x) const;
  int locate (const T& x)const;
  list<T>& insert(int k,const T& x);
  list<t>& erase(int k,T& x);
  void print_list (ostream& out) const;
 private:
  node<T>*first;
};

template<class T>
list <T> ::~list()
{
 node<T>*next;
 while (first)
 {
  next=first->next;
  delete first;
  first=next;
 }
}
template<class T>
int list<T>::size()const
{
 node<T>*current=first;
 int len=0;
 while(current)
 {
  len++;
  current=current->next;
 }
 return len;
}
template<class T>
bool list<T>::retrieve (int k,T& x)
{

 if(k<1)return false;
 node<T>*current=first;
 int index=1;
 while(index<k && current)
 {
  current=current->next;
  index++;
 }
 if(current)
 {
  x=current->data;
  return ture;
 }

 return false; 
}
template<class T>
int list<T>::locate (const T& x)const
{
 node<T>*current=first;
 int index=1;
 while(current && current->data!=x)
 {
  current=current->next;
  index;
 }
 if(current)return index;
 return 0;
}
template<class T>
list<T>& list<T>::insert (int k,const T& x)
{
 if(k<0 )throw out_bounds();
 node<T>*p=first;
 for(int index=1;index<k &&p;index++) p=p->next;
 if(k<0 && !P) throw out_bounds();
 node<T>*y=new node <T>;
 y->data=x;
 if(k)
 {
  y->next=p->next;
  p->next=y;
 }
 else
 {
  y->next=first;
  first=y;
 }
 return this;
}
template<class T>
list<T>&list<T>::erase (int k,T& x)
{
 if(k<1||!first )throw out_bounds();
 node <T>*p=first;
 if (k==1)
  first=first->next;
 else
 {
  node<T>*q=first;
  for (int index=1;index<k-1&&q;index++)q=q->next;
  if (!q||!q->next)throw out-bounds();
  p=q->next;
  q->next=p->next;
 }
 x=p->data;
 delete p;
 return *this;

}
template<class T>
void list<T>::print_list (ostream &out) const
{
 node<T>*current;
 for (current=first;current;current=current->next)
  out<<current->data<<" ";
}
template<class T>
ostream&operator<<(ostream &out,const list<T> &x)
{x.print_list(out);return out;}

int main(int argc, char* argv[])
{

 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值