求双向链表如何插入?
  • 板块学术版
  • 楼主go_your_a_head
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/29 19:14
  • 上次更新2025/1/30 13:00:03
查看原帖
求双向链表如何插入?
1558515
go_your_a_head楼主2025/1/29 19:14

刚学链表,还很蒟蒻(有的时候极其简单的单向链表都写错) 不是查错,大可放心

#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语句中把链表插入进去的语句是哪里有问题吗?只需要帮忙看插入的步骤对不对就行了,如果发生了本质的错误,比如插入的数不对之类的,这些都不用管,只要帮我看看到底怎么没有连起来就行了。 (我觉得答案对不对都算了,至少好歹连起来吧)

谢谢各位了

2025/1/29 19:14
加载中...