#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;
}
#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;
}