#include<iostream>
using namespace std;
struct node{
char date;
node *next;
};
node *head;
int n=0;
void createList()//建立链表
{
node *p,*s;
char x;//输入的字符
int z=1;
p=head;
cout<<"说明:输入字符,以#结尾:"<<endl;
while(z)
{
cout<<"\t\t输入"<<endl;
cin>>x;
if(x!='#')
{
s=new node;
n++;
s->date=x;
p->next=s;
p=s;
}
else
z=0;
}
}
void insList(int i,char x)//插入字符,i为插入的位置,x为插入的字符
{
node *p,*s;
int j;
s=new node;
s->date=x;
if(i==0)
cout<<"插入位置不合理"<<endl;
else
{
p=head;
j=1;
while(p&&j<i){
j++;
p=p->next;
}
if(p!=NULL)
{
n++;
s->next=p->next;
p->next=s;
}
else
cout<<"\t\t插入的位置非法"<<endl;
}
}
void delList(char x)//删除元素,
{
node *q,*p;
if(head==NULL)
{
cout<<"链表下溢"<<endl;
return;
}
if(head->next==NULL)
{
cout<<"链表已经为空\n";
return;
}
q=head;
p=head->next;
while(p!=NULL&&p->date!=x)
{
q=p;
p=p->next;
}
if(p!=NULL)
{
q->next=p->next;
delete p;
n--;
cout<<x<<"已经被删除"<<endl;
}
else
cout<<"未找到\n";
}
void showList()//显示链表
{
node *p=head;
cout<<"\n\t\t显示链表的所有元素:";
if(head->next==NULL||p==NULL)
cout<<"\n\t\t链表为空"<<endl;
else
{
cout<<"\n\t\t"<<endl;
while(p->next !=NULL)
{
cout<<p->next->date<<'\t';
p=p->next;
}
}
cout<<endl;
}
void main()
{
head=new node;
head->next=NULL;
int chioce,i,j=1;
char x;
while(j)
{
cout<<"\n";
cout<<"\t\t\t 单链表\n";
cout<<"\n\t\t*************************************************";
cout<<"\n\t\t* 1-----------建表 *";
cout<<"\n\t\t* 2-----------插入 *";
cout<<"\n\t\t* 3-----------删除 *";
cout<<"\n\t\t* 4-----------显示 *";
cout<<"\n\t\t* 5-----------求表长 *";
cout<<"\n\t\t* 0-----------返回 *";
cout<<"\n\t\t*************************************************";
cout<<"\t\t请选择菜单:";
cin>>chioce;
if(chioce==1)
createList();
else if(chioce==2)
{
cout<<"\n\t\t输入插入的位置及字符:";
cin>>i>>x;
insList(i,x);
}
else if(chioce==3)
{
cout<<"输入删除的字符";
cin>>x;
delList(x);
}
else if(chioce==4)
showList();
else if(chioce==5)
cout<<"表长为:"<<n<<endl;
else if(chioce==0)
j=0;
else
cout<<"输入错误";
}
}
using namespace std;
struct node{
char date;
node *next;
};
node *head;
int n=0;
void createList()//建立链表
{
node *p,*s;
char x;//输入的字符
int z=1;
p=head;
cout<<"说明:输入字符,以#结尾:"<<endl;
while(z)
{
cout<<"\t\t输入"<<endl;
cin>>x;
if(x!='#')
{
s=new node;
n++;
s->date=x;
p->next=s;
p=s;
}
else
z=0;
}
}
void insList(int i,char x)//插入字符,i为插入的位置,x为插入的字符
{
node *p,*s;
int j;
s=new node;
s->date=x;
if(i==0)
cout<<"插入位置不合理"<<endl;
else
{
p=head;
j=1;
while(p&&j<i){
j++;
p=p->next;
}
if(p!=NULL)
{
n++;
s->next=p->next;
p->next=s;
}
else
cout<<"\t\t插入的位置非法"<<endl;
}
}
void delList(char x)//删除元素,
{
node *q,*p;
if(head==NULL)
{
cout<<"链表下溢"<<endl;
return;
}
if(head->next==NULL)
{
cout<<"链表已经为空\n";
return;
}
q=head;
p=head->next;
while(p!=NULL&&p->date!=x)
{
q=p;
p=p->next;
}
if(p!=NULL)
{
q->next=p->next;
delete p;
n--;
cout<<x<<"已经被删除"<<endl;
}
else
cout<<"未找到\n";
}
void showList()//显示链表
{
node *p=head;
cout<<"\n\t\t显示链表的所有元素:";
if(head->next==NULL||p==NULL)
cout<<"\n\t\t链表为空"<<endl;
else
{
cout<<"\n\t\t"<<endl;
while(p->next !=NULL)
{
cout<<p->next->date<<'\t';
p=p->next;
}
}
cout<<endl;
}
void main()
{
head=new node;
head->next=NULL;
int chioce,i,j=1;
char x;
while(j)
{
cout<<"\n";
cout<<"\t\t\t 单链表\n";
cout<<"\n\t\t*************************************************";
cout<<"\n\t\t* 1-----------建表 *";
cout<<"\n\t\t* 2-----------插入 *";
cout<<"\n\t\t* 3-----------删除 *";
cout<<"\n\t\t* 4-----------显示 *";
cout<<"\n\t\t* 5-----------求表长 *";
cout<<"\n\t\t* 0-----------返回 *";
cout<<"\n\t\t*************************************************";
cout<<"\t\t请选择菜单:";
cin>>chioce;
if(chioce==1)
createList();
else if(chioce==2)
{
cout<<"\n\t\t输入插入的位置及字符:";
cin>>i>>x;
insList(i,x);
}
else if(chioce==3)
{
cout<<"输入删除的字符";
cin>>x;
delList(x);
}
else if(chioce==4)
showList();
else if(chioce==5)
cout<<"表长为:"<<n<<endl;
else if(chioce==0)
j=0;
else
cout<<"输入错误";
}
}