鬼吹灯之怒睛湘西

添加链接描述
将已存在的一个节点查到另一个节点的旁边,可以将要插入的节点当作直接不存在,直接删除,找到要插入的地方,在重新建一个节点插入。注意要处理好删除后和插入时指针的指向

/**/
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node * next;
int date;
};
struct node * creat(int n)
{
struct node * head,*pr,*p;
int i;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
pr=head;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->next=NULL;
p->date=i;
pr->next=p;
pr=p;
}
return head;
};
struct node * inser(struct node * head,int x,int y)
{
struct node *pr,*p,*q;
pr=head->next;
p=head;
while(pr->date!=x&&pr)//找到要插入的节点
{
p=pr;//插入节点的前一个
pr=pr->next;//插入节点
}
p->next=pr->next;//改变指针指向
pr=p->next;
q=(struct node *)malloc(sizeof(struct node));//重建节点q
q->next=NULL;
q->date=x;
pr=head;
while(pr->next->date!=y&&pr->next)//找到要插入的地方
{
pr=pr->next;
}
q->next=pr->next;//插入;注意是q->next先指向pr->next,顺序不可颠倒
pr->next=q;
return head;
};
void findx(struct node * head,int x)
{
struct node * pr;
pr=head;
for(pr=head;pr->next;pr=pr->next)
{
if(pr->next->date= =x)
{
if(pr= =head)
{
printf(“cyk666\n”);
return ;
}
else
{
printf("%d\n",pr->date);
return ;
}
}
}
printf(“cky666\n”);
return ;
}
int main()
{
int n,t,x,y,m;
char ch;
struct node * head;
scanf("%d",&t);
while(t–)
{
scanf("%d %d",&n,&m);
head=creat(n);
while(m–)
{
getchar();
scanf("%c",&ch);
if(ch==‘A’)
{
scanf("%d %d",&x,&y);
head=inser(head,x,y);
}
if(ch==‘Q’)
{
scanf("%d",&x);
findx(head,x);
}
}
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值