64,637
社区成员
发帖
与我相关
我的任务
分享
//带头结点的建表,判空,删除,插入,找x相同值的结点数,就地逆置,用一主函数实现
#include <iostream>
using namespace std;
template <class ElemType>
class List
{
public:
List();
void InitList();//建表
void Show() const;//输出链表
bool IsEmpty();//判空
void Delete(int i);//删除
void Insert(ElemType e,int i);//插入
void FindEqual(int x);//找x相同的节点数
List<int> *Getnode (int n);//取任意节点
void Reverse();//逆置
~List();
private:
ElemType elem;
List *head;//头指针
List *next;//后继
int length;
};
template <class ElemType>
List<ElemType>::List()
{
elem=0;
head=NULL;
next=NULL;
length=0;
}
template <class ElemType>
void List<ElemType>::InitList()
{
List*p1,*p2;
head=new List;
p1=head;
p2=new List;
int count=0;
cout<<"请输入数值(-1 to quit): ";
cin>>p2->elem;
while(p2->elem!=-1)
{
p1->next=p2;
p1=p2;
++count;
p2=new List;
cin>>p2->elem;
}
p1->next=NULL;
length=count;
}
template <class ElemType>
void List<ElemType>::Show() const
{
if(head!=NULL)
{
List *p=head->next;
cout<<"显示链表的值为: ";
while(p)
{
cout<<p->elem<<" ";
p=p->next;
}
cout<<endl;
}
else
cout<<"表为空表!"<<endl;
}
template <class ElemType>
bool List<ElemType>::IsEmpty()
{
if(length==0)
return true;
else
return false;
}
template <class ElemType>
void List<ElemType>::Insert(ElemType e,int i)
{
if(!IsEmpty())
{
List *p=head;
int j=0;
while(p && j<i-1)
{
p=p->next;
++j;
}
if(!p || j>i-1)
cout<<"插入失败!"<<endl;
else
{
List *s=new List;
s->elem=e;
s->next=p->next;
p->next=s;
++length;
cout<<"插入成功!"<<endl;
}
}
else
cout<<"不能插入,表为空表!"<<endl;
}
template <class ElemType>
void List<ElemType>::Delete(int i)
{
int e;
if(!IsEmpty())
{
List *p=head;
int j=0;
while(p && j<i-1)
{
p=p->next;
++j;
}
if(!p && j>i-1)
cout<<"删除失败!"<<endl;
else
{
e=p->next->elem;
p->next=p->next->next;
--length;
cout<<"删除成功!"<<endl;
cout<<"删除的元素是"<<e<<endl;
}
}
else
cout<<"不能删除,表为空表!"<<endl;
}
template <class ElemType>
void List<ElemType>::FindEqual(int x)
{
if(!IsEmpty())
{
List *p;
int i=0;
bool flag=false;
for(p=head->next;p!=NULL;p=p->next)
{
++i;
if(p->elem==x)
{
flag=true;
cout<<"第"<<i<<"个节点的值与"<<x<<"相等"<<endl;
}
}
if(!flag)
cout<<"没有与"<<x<<"相同的值!"<<endl;
}
else
cout<<"表为空表!"<<endl;
}
template <class Elemtype>
List<int> * List<Elemtype>::Getnode(int n)
{
int i=0;
List* p;
p=head;
while(p && i<n)
{
p=p->next;
++i;
}
return p;
}
template <class ElemType>
void List<ElemType>::Reverse()
{
int center=length/2,i;
ElemType temp;
List *p1,*p2;
p1=head;
for(i=length;i>center;--i)
{
p2=Getnode(i);
temp=p1->next->elem;
p1->next->elem=p2->elem;
p2->elem=temp;
p1=p1->next;
}
cout<<"逆置成功!"<<endl;
//Show();
}
template <class ElemType>
List<ElemType>::~List()
{
while(head!=NULL)
{
delete head;
head=head->next;
}
}
int main()
{
char ch;
List<int> obj;
int e,i;
cout<<"\t\t1.创建表"<<endl;
cout<<"\t\t2.插入值"<<endl;
cout<<"\t\t3.删除值"<<endl;
cout<<"\t\t4.相同值"<<endl;
cout<<"\t\t5.输出表"<<endl;
cout<<"\t\t6.逆置表"<<endl;
cout<<"\t\t0.退出"<<endl;
do
{
cout<<"请选择操作: ";
cin>>ch;
switch(ch)
{
case '1':
obj.InitList();
break;
case '2':
cout<<"请输入要插入的值: ";
cin>>e;
cout<<"请输入要插入的位置: ";
cin>>i;
obj.Insert(e,i);
break;
case '3':
cout<<"请输入要删除的节点: ";
cin>>i;
obj.Delete(i);
break;
case '4':
cout<<"请输入要查找相同的值: ";
cin>>i;
obj.FindEqual(i);
break;
case '5':
obj.Show();
break;
case '6':
obj.Reverse();
break;
case '0':
cout<<"\t操作完毕!"<<endl;
break;
default :
cout<<"输入错误,请重新输入: ";
break;
}
}while(ch!='0');
system("pause");
return 0;
}