#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
node* creat()
{
node *head,*tail,*p;
int x;
head=tail=new node;
while(scanf("%d",&x),x!=-1)
{
p=new node;
p->data=x;
tail->next=p;
tail=p;
}
tail->next=NULL;
return head;
}
void show(node* head)
{
while(head->next!=NULL)
{
head=head->next;
printf("%d ",head->data);
}
puts("");
}
node* find(node *head,int i)
{
int j;
for(j=0;head->next!=NULL,j<i;j++)
head=head->next;
if(j==i) return head;
return NULL;
}
void insert(node *head,int x,int y)
{
node *n=find(head,x-1);
if(n==NULL) exit(1);
else
{
node *m=new node;
m->data=y;
m->next=n->next;
n->next=m;
}
}
void del(node *head,int x)
{
node *n=find(head,x-1);
if(n==NULL) exit(2);
else
{
node *m=n->next;
if(m==NULL) exit(3);
else
{
n->next=m->next;
delete m;
}
}
}
int main()
{
int x,y;
node *head=creat();
show(head);
cin>>x>>y;
insert(head,x,y);
show(head);
cin>>x;
del(head,x);
show(head);
return 0;
}
链表的创建插入删除
最新推荐文章于 2022-02-15 22:08:19 发布