社区讨论
求双向链表如何插入?
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m6ht7bi1
- 此快照首次捕获于
- 2025/01/29 19:14 去年
- 此快照最后确认于
- 2025/11/04 10:11 4 个月前
刚学链表,还很蒟蒻(有的时候极其简单的单向链表都写错)
不是查错,大可放心
CPP#include<bits/stdc++.h>
using namespace std;
int n,m;
struct Node{
int value;
Node *next,*prev;
}a1[100001],a2[100001],*head1,*head2;
//_________________________________________
void Hblin(){
Node *p=head2;
for(Node *t=head1;t;t=t->next){
while(t->value>p->value){
if(t==head1){
p->next=head1;
head1=p;
}
else{
t->prev=p->prev;
p->next=t;
}
p=p->next;
}
}
}
//__________________________________________
int main(){
scanf("%d%d",&n,&m);
head1=NULL,head2=NULL;
Node *p1,*p2;
for(int i=1;i<=n;i++){
scanf("%d",&a1[i].value);
if(head1==NULL){
head1=&a1[i];
p1=head1;
}
else{
p1->next=&a1[i];
p1=p1->next;
}
}
for(int i=1;i<=m;i++){
scanf("%d",&a2[i].value);
if(head2==NULL){
head2=&a2[i];
p2=head2;
}
else{
p2->next=&a2[i];
p2=p2->next;
}
}
Hblin();
for(Node *t=head1;t;t=t->next){
printf("%d ",t->value);
}
return 0;
}
就是Hblin函数里面的while中的if以及else语句中把链表插入进去的语句是哪里有问题吗?只需要帮忙看插入的步骤对不对就行了,如果发生了本质的错误,比如插入的数不对之类的,这些都不用管,只要帮我看看到底怎么没有连起来就行了。
(我觉得答案对不对都算了,至少好歹连起来吧)
谢谢各位了
回复
共 0 条回复,欢迎继续交流。
正在加载回复...