类模板实现多数据类型线性链表

#include<iostream.h>
#include<stdio.h>
template<class T> 
class linklist
{
public:
linklist(int);  //构造
~linklist();//析构
void create();//创建
void print();//显示
void del();//删除
void insert();//插入
void search();//检索
void length();//表长
void fun();    //......
private:
struct node
{
T ch;
node *next;
};
node *head;
};
template<class T>
linklist<T>::linklist(int a)
{
head       = new node;
head->next = NULL;
head->ch   = a;
}
template<class T>
linklist<T>::~linklist()
{
node *p = head->next;            //使指针p指向链表的第一个节点
while(p != NULL)
{
head->next = p->next;     //使头指针指向p的下一个节点
delete p;
p = head->next;              //使p节点指向头指针向的那个节点
}
delete head;                     //最后将头节点也删除
cout<<"\t已经删除链表!"<<endl;
}
template<class T>
void linklist<T>::create()      //创建链表
{
    node *s,*p;
T m ;
cout<<"请输入各个节点的数据,用空格间隔:"<<endl;
p = head;         //此时p作为第一个有意义的节点
while( 1 )
{
if( head->ch == 1 )
scanf("%d",&m);
else if( head->ch == 2 )
scanf("%f",&m);
else
scanf("%c",&m);
s = new node;      //动态的申请一个节点
s->ch  = m;          //数据域赋值
p->next= s;          //把s 节点接在p节点之后
p      = s;          //使p向后移一个节点


if( getchar() == '\n' )
break;
}
p->next = NULL;
cout<<"\t链表建成!"<<endl;
}
template<class T>
void linklist<T>::print()      //显示链表
{
node *p;
p = head->next;
cout<<"\t\t链表为: "<<(char)1<<"->"; //美化装饰,输出“笑脸”
while(p != NULL)          //逐个节点输出
{
cout<<p->ch<<"->";
p = p->next;
}
cout<<(char)1<<endl;
}
template<class T>
void linklist<T>::del()        //删除节点函数
{
T m;
node *p,*q;
cout<<"\n输入要删除的元素: ";
cin>>m;
p = head;
q = p->next;
while(q!=NULL && q->ch != m) //循环,使得q为要删除的节点,而p则为q的前一个节点
{
p = q;
q = q->next;
}
    if(q == NULL)                //此if 和else 位置不要颠倒,否则容易出现错误
cout<<"\t\t"<<m<<"不存在"<<endl;
else          
{
p->next = q->next;
delete q;
cout<<"\t\t"<<"节点成功删除!"<<endl;
}

}
template<class T>
void linklist<T>::insert()         //插入节点函数
{
int i,k;
T m;
node *p,*s;
cout<<"\n要插入元素的位置: ";
cin>>i;
cout<<"\n要插入的元素: ";
cin>>m;
p = head;
k = 1;
while(k < i && p != NULL)  //利用k值循环,找到要插入位置的前一个节点p
{
p = p->next;
k++;
}
if(k == i)                 //实现插入
{
s = new node;
s->ch = m;
s->next = p->next;     //节点p的后面插入节点s
   p->next = s;
cout<<"\t\t节点成功插入!"<<endl;
}
    else
        cout<<"\t\t插入位置错误!"<<endl;

template<class T>
void linklist<T>::search()        //检索函数

T m;
int i=1;
node *p;
cout<<"请输入要检索的元素:";
cin>>m;
p = head->next;
while(p != NULL && p->ch != m)
{
p=p->next;
i++;
}
if(p == NULL)
cout<<"\t\t"<<m<<"不存在"<<endl;
else
cout<<"\t\t"<<m<<"在第"<<i<<"个位置"<<endl;
}
template<class T>
void linklist<T>::length()       //求表长函数
{
node *p;
p = head->next;
    int i = 0;
while(p != NULL)
{
p=p->next;
i++;
}
cout<<"\t\t链表的长度为: "<<i<<endl;
}
template<class T>
void linklist<T>::fun()
{
int b;
cout<<"\t\t\t ***单链表的相关操作***"<<endl;
while(1)
{
cout<<"\t    ************************************************"<<endl;
cout<<"\t    菜单:1.创建  2.表长 3.删除 4.插入 5.检索 0.退出"<<endl;
cout<<"\t    ************************************************"<<endl;
cout<<"请输入选择: ";
cin>>b;
if( b == 1 )
{
create();  
print();
}
else if( b == 2 )
{
length();
print();
}
else if( b == 3 )
{
del();
print();
}
else if( b == 4 )
{
insert();
print();
}
else if( b == 5 )
{
search();
print();
}
else
break;
}
}
int main()
{
int a ;
while(1)
{
cout<<"\t\t   ***模板类实现多数据类型的线性链表***\n"<<endl;
        cout<<"请选择类模板T的类型:\n\t\t1.int     2.float    3.char     4.退出 "<<endl;
cin>>a;
if( a == 1 )
{
linklist<int> L(1);
L.fun();
}
else if( a == 2 )
{
linklist<float> L(2);
L.fun();
}
else if( a == 3 )
{
linklist<char> L(3);
L.fun();
}
else
break;
}
return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值