文章

60

粉丝

361

获赞

41

访问

496.1k

头像
记录
P1025 贵州大学2019机试
发布于2021年1月29日 16:10
阅读数 7.3k

原本用创建链表的方法创建第三个链表,后面发现会内存溢出,故用指针指向已经创建了的

#include <bits/stdc++.h>
using namespace std;
struct node
{
	int date;
	struct node *next;
};

struct node * creat(int n)
{
	struct node *head=new node;
	struct node *now,*tail=head;
	
	for(int i=1;i<=n;i++)
	{
		now=new node;
		cin>>now->date ;
		tail->next=now;
		tail=now;
	}
	tail->next=NULL;//尾指针置空
	return head;
}

int main()
{
	int s1,s2;
	cin>>s1;
	struct node *head1=creat(s1);
	cin>>s2;
	struct node *head2=creat(s2);

	struct node *head3=new node;
	struct node *tail=head3;
	head1=head1->next;
	head2=head2->next;
	while(head1!=NULL&&head2!=NULL)
	{
		if( head1->date >=head2->date )
		{
			tail->next=head2;
			tail=tail->next;
			head2=head2->next;
		}
		else if( head1->date <head2->date )
		{
			tail->next=head1;
			tail=tail->next;
			head1=head1->next;				
		}
	}
	if(head2==NULL)
		tail->nex...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发