链表 List.h

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

///////////////////////////

// //

// 链表 List.h //

// //

//////////////////////////

#include"list.h"

#include<iostream.h>

#include<stdlib.h>

template<class type>

void initlist(type &tmp)

{

list<type> List;

int n;

while(true)

{

cout<<"请选择你要对链表进行的操作 "<<endl

<<"1) 在末尾插入数据"<<endl

<<"2) 在任意处插入数据"<<endl

<<"3) 删除数据项"<<endl

<<"4) 删除整个链表"<<endl

<<"5) 打印链表"<<endl

<<"6) 查找数据项"<<endl

<<"7) 退出"<<endl;

cout<<">\ ";

cin>>n;

while(n<1||n>7)

{

cout<<"输入有误,请从新输入!"<<endl;

cout<<">\ ";

cin>>n;

}

switch(n)

{

case 1: list_insertend(List);break;

case 2: list_insert(List);break;

case 3: list_delnode(List);break;

case 4: list_makeempty(List);break;

case 5: list_print(List);break;

case 6: list_find(List);break;

case 7: return ;break;

}

}

}

void LIST()

{

int n;

cout<<"请选择你要构造的链表的数据类型 1)整型,2)字符型,3)浮点型"<<endl;

cout<<">\ ";

cin>>n;

while(n<1||n>3)

{

cout<<"输入有误,请从新输入!"<<endl;

cout<<">\ ";

cin>>n;

}

char t_c='c';

int t_i=12;

double t_f=23.3;

switch(n)

{

case 1:initlist(t_i);break;

case 2:initlist(t_c);break;

case 3:initlist(t_f);break;

}

}

template<class type>

void list_insertend(list<type> &L)

{

type t;

cout<<"请输入插入数据: >\";

cin>>t;

L.insertend(t);

}

template<class type>

void list_find(list<type> &L)

{

type T;

cout<<"请输入你要查找的数据项:>\ ";

cin>>T;

int i;

if(!(i=L.find(T)))

cout<<"你要查找的数据项不存在!"<<endl;

else

cout<<"你要查找的数据项在第"<<i<<"个位置"<<endl;

}

template<class type>

void list_insert(list<type> &L)

{

type t;

cout<<"请输入插入数据: >\";

cin>>t;

int n;

cout<<"请输入插入位置: >\";

cin>>n;

if(L.insert(t,n))

cout<<"插入成功! 在"<<n<<"位置 插入"<<t<<endl;

else

cout<<"插入失败! 插入位置不正确!"<<endl;

}

template<class type>

void list_delnode(list<type>& L)

{

int i;

cout<<"请输入要删除数据项的位置: >\";

cin>>i;

while(i<1||i>L.getlen())

{

cout<<"输入有误,可能大与链表长度,请从新输入!"<<endl;

cout<<">\ ";

cin>>i;

}

L.delnode(i);

}

template<class type>

void list_makeempty(list<type> &L)

{

L.makeempty();

}

template<class type>

void list_print(list<type> &L)

{

if(!L.print())

cout<<"链表为空!"<<endl;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航