其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
链表数据结构 list.h

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

///////////////////////////
//    //
//   链表数据结构  list.h //
//    //
//////////////////////////


#include<iostream.h>

template<class type>
class list;

template<class type>
class listnode
{
public:
friend class list<type>;
private:
type data;
listnode<type> * next;
};


template<class type>
class list
{
public:
list();
~list();
void insertend(type); //向链表尾部插入元素
bool insert(type,int); //向链表任意位置插入元素
void delnode(int i);  //删除元素
int find(type T);   //查找元素
void makeempty();   //销毁链表
bool print();  //打印链表
int getlen();  //得到链表长度
  private:
listnode<type> *first,*last;
int length;
};

template<class type>
void initlist(type &tmp);

template<class type>
void list_exit(list<type> &L,type tmp);

void initation();

template<class type>
void list_insertend(list<type> &L,type tmp);


template<class type> int list<type>::getlen()
{
return length;
}

template<class type> void list<type>::makeempty()
{
listnode<type> *p1,*p2;

p1=first->next;
first->next=NULL;
while(p1!=NULL)
  {
p2=p1;
p1=p1->next;
delete p2;
}
length=0;   
}

template<class type> void list<type>::insertend(type t)
{

listnode<type> *p;
p=new listnode<type>;
p->data=t;
p->next=NULL;
last->next=p;
last=p;

length++;
}

template<class type> bool list<type>::insert(type t,int i)
{
listnode<type> *p;
p=first;

int k=1;
while(p!=NULL&&k<i)
{
p=p->next;
k++;
}
if(p==NULL&&k!=i)
return false;
else
{
   listnode<type> *tp;
  tp=new listnode<type>;
  tp->data=t;
  tp->next=p->next;
  p->next=tp;
  length++;
   
  return true;
}
}

template<class type> void list<type>::delnode(int i)
{
int k=1;
listnode<type> *p,*t;
p=first;

while(p->next!=NULL&&k!=i)
{
p=p->next;
   k++;
}
  t=p->next;
cout<<"你已经将数据项 "<<t->data<<"删除"<<endl;

p->next=p->next->next;
length--;
delete t;
}

template<class type> bool list<type>::print()
{
listnode<type> *p=first->next;
if(length==0)
return false;
else
{
cout<<"链表中有"<<length<<"项数据: "<<endl;
while(p)
{
  cout<<p->data<<" ";
  p=p->next;
}
}
cout<<endl;


return true;
}

template<class type> int list<type>::find(type T)
{
listnode<type> *p=first->next;
int i=1;
while(p&&p->data!=T)
{
p=p->next;
i++;
}
if(p)
return i;
else
   return 0;
}


template<class type> list<type>::~list()
{
delete first;
cout<<"欢迎再次使用 (!^!) "<<endl;
}

template<class type> list<type>::list()
{
     listnode<type> *node=new listnode<type>;
  node->next=NULL;
  first=last=node;
  length=0;
}



相关文章

相关软件