#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{int data;
struct node *pri;
struct node *next;
}NODE,*N;
N press_list(int n)
{ N p,q,head;int i;
head=(N)malloc(sizeof N);
head->pri=NULL;
head->next=NULL;
p=head;
printf("请输入双链表数据:");
for(i=0;i<n;i++)
{q=(N)malloc(sizeof N);
scanf("%d",&q->data);
p->next=q;
q->pri=p;
p=p->next;
}
q->next=NULL;
return head;
}
void out_list(N head)
{
N p;
p=head;
while(p->next!=NULL)
{p=p->next;
printf(" %d ",p->data);
}
}
void ins(N head,int Q,int k)
{N p,q,s;int i;
p=head;
q=(N)malloc(sizeof N);
q->data=Q;
for(i=1;i<k;i++)
{ p=p->next;
}
s->next=p->next;
p->next=s;
s->pri=p->next;
s->pri=p;
}
int main()
{ printf("请输入双链表长度");
int n,Q,K;
N head;N s;
scanf("%d",&n);
head=press_list(n);
out_list(head);
printf("请输入插入数据和插入点位置:");
scanf("%d%d",&Q,&K);
ins(head,Q,K);
printf("新链表为:");
out_list(head);
return 0;
}
节点应该插入到尾结点吧,你这个好像放到头节点了,要先循环到尾结点,再插入就不会有问题了。
在添加节点的函数最前面,把头节点赋值给p, 然后开始循环,p =p.next ,直到p.next =null, 退出循环开始添加节点
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632