文章

49

粉丝

140

获赞

27

访问

16.7k

头像
链表合并 题解:易错点:先判断是否为空
P1025 贵州大学2019机试
发布于2024年1月18日 13:35
阅读数 293

#include<iostream>
using namespace std;
int main(){
	int n=0;
	struct node{
		int data;
		struct node *next;	};
	cin >> n;
	node *a=new node;
	a->next=NULL;
	node *b=a;
	for(int i=0;i<n;i++){
     cin >> a->data;
	 if(i==n-1)
		 break;
		else{
			a->next=new node;
		a=a->next;
		a->next=NULL;
	 }
	}
	int m=0;
	cin >>m;
	node *c=new node;
	c->next=NULL;
	node *d=c;
	for(int i=0;i<m;i++){
    cin >> c->data;
		 if(i==m-1)
			 break;
		else{c->next=new node;
		c=c->next;
		c->next=NULL;
		}
	}
	for(int i=0;i<m+n;i++){
		if(i!=m+n-1){//不是最后一次
 		  if((d!=NULL)&&(b!=NULL)){
			 if (b->data<d->data){
	 cout << b->data <<' ';
	 b=b->next;
	 }
		  else if(b->data>=d->data){
	 cout << d->data <<' ';
	 d=d->next;
	 }
		  }
		  else if(d!=NULL){
			   cout << d->data <<' ';
	           d=d->next;
...
登录查看完整内容


登录后发布评论

2 条评论
snake
2024年1月18日 14:01

跑一下这组数据

4
1 2 3 4
4
6 7 8 9

赞(0)

孙某人 : 回复 snake: 确实不通过,谢谢

2024年1月18日 18:17