#include <iostream>
#include<string>
using namespace std;
int count=0;
struct node
{
int
data;
node * next;
};
node *Creat()
{
node *p1,*p2,*head;
int a;
head=0;
cout<<"输入数据以-1结尾"<<endl;
cin>>a;
while(a!=-1)
{
p1=new node;
p1->data=a;
count++;
if(head==0)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
cin>>a;
}
p2->next=0;
return(head);
}
node *Delete (node * head,int num)
{
node *p1,*p2;
if(head->data==num)
{
p1=head;
head=head->next;
delete p1;
}
else
{
p1=head;
while(p1!=NULL)
{
if(p1->next->data==num)
{
p2=p1->next;
p1->next=p2->next;
delete p2;
}
p1=p1->next;
}
}
return (head);
}
node *Add(node * head,int num,int con)
{
node *p1,*p2;
if(num-1==0)
{
p1->next=head;
head=p1;
head->data=con;
}
else if(num-1>0)
{
p1=head;
int ii=1;
p2=new node;
while(p1!=NULL)
{
ii++;
if(ii==num)
{
p2->next=p1->next;
p2->data=con;
p1->next=p2;
}
p1=p1->next;
}
}
return head;
}
void view(node *head)
{
cout <<"此时链表内容为"<<endl;
node *p1;
for (p1=head;p1!=NULL;p1=p1->next)
{
cout<<p1->data<<endl;
}
}
node *Change(node * head,int num,int con)
{
node *p1,*p2;
if(num-1==0)
{
head->data=con;
}
else
{
p1=head;
int o=1;
while(p1!=NULL)
{
if (o==num)
{
p1->data=con;
}
o++;
p1=p1->next;
}
}
return head;
}
int Search(node *head,int num)
{
int p=1;
int save;
node *p1=head;
while(p1!=NULL)
{
if(p==num)
{
save=p1->data;
}
p1=p1->next;
p++;
}
return save;
}
int main()
{
node *head;
head=Creat();
string a="continue";
while(a=="continue")
{
cout<<"请输入要执行的操作"<<endl;
cout<<"1:增加
"<<"2:删除
"<<"3:修改
"<<"4:查找
"<<endl;
int count;
cin>>count;
switch(count)
{
case 1:
cout<<"要增加的位置"<<endl;
int position;
cin>>position;
cout<<"要增加的数"<<endl;
int w;
cin>>w;
head=Add(head,position,w);
view(head);
break;
case 2:
cout<<"要删除的数"<<endl;
int dp;
cin>>dp;
head=Delete(head,dp);
view(head);
break;
case 3:
cout<<"要修改元素的位置"<<endl;
int cp;
cin>>cp;
cout<<"要修改成的值"<<endl;
int cp2;
cin>>cp2;
head=Change(head,cp,cp2);
view(head);
break;
case 4:
cout<<"要查找的元素的位置"<<endl;
int sp;
cin>>sp;
cout<<"该位置的元素为"<<Search(head,sp)<<endl;
break;
}
cout<<"请输入下一步操作"<<endl;
cout<<"输入continue继续操作,输入其他任意键退出程序"<<endl;
cin>>a;
}
}
#include<string>
using namespace std;
int count=0;
struct node
{
};
node *Creat()
{
}
node *Delete (node * head,int num)
{
}
node *Add(node * head,int num,int con)
{
}
void view(node *head)
{
}
node *Change(node * head,int num,int con)
{
}
int Search(node *head,int num)
{
}
int main()
{
}