无表头结点的有序环形链表的合并

//数据结构教程 蔡子经版 第一章课后习题16

//这题真是bt,想了很多方法发现没有一个比较方便,无奈只得把环形链表变成单向链表,最后再把单向链表转换成环形

//求大神指教简便方法

#include<stdio.h>
#include<stdlib.h>
struct node {   
	int num; 
    struct node* next; 
};
typedef struct node Node;
Node* create(int n)
{
	int i;
	Node*head,*p,*q;
	if(n==0)
		return NULL;
	head=(Node*)malloc(sizeof(Node));
	p=head;
	for(i=1;i<n;i++){
		scanf("%d",&p->num);
		q=(Node*)malloc(sizeof(Node));
		p->next=q;
		p=q;
	}
	scanf("%d",&p->num);
	p->next=head;
	return head;
}

Node* insert(Node* head,Node *ps)//单向链表的插入操作
{
	Node *p1=head,*p2;
	if(head==NULL)
		head=ps;
	else{
		while (ps->num>p1->num && p1->next){
			p2=p1;p1=p1->next;
		}
		if(ps->num<=p1->num){
			if(head==p1) 
				head=ps; 
			else 
				p2->next=ps;
			ps->next=p1;
		}
		else { 
			p1->next=ps;ps->next=NULL; 
		}
	}
	return head;
}

Node *merge(Node *ah,Node *bh)
{
	Node *head=NULL,*p,*pa=ah,*pb=bh,*ch;
	if(ah==NULL && bh==NULL)
		return NULL;
	if(ah==NULL && bh!=NULL){
		while(pb->next!=bh)
			pb=pb->next;
		pb->next=NULL;
		pb=bh;
		head=insert(head,pb);
	}
	if(ah!=NULL && bh!=NULL){
		while(pa->next!=ah)//将环形链表A变为单向链表
			pa=pa->next;
		pa->next=NULL;
		while(pb->next!=bh)//将环形链表B变为单向链表
			pb=pb->next;
		pb->next=NULL;
		pa=ah;pb=bh;
		head=insert(head,pa);
		while(pb){
			p=pb;
			pb=pb->next;
			head=insert(head,p);
		}
	}
	if(ah!=NULL && bh==NULL){
		while(pa->next!=ah)
			pa=pa->next;
		pa->next=NULL;
		pa=ah;
		head=insert(head,pa);
	}
	ch=head;
	while(ch->next)//将单向链表改成环形链表
		ch=ch->next;
	ch->next=head;
	ch=ch->next;
	return ch;
}


void print(Node *a)
{
	Node *p=a;
	if(a==NULL){
		printf("NULL\n");
		return ;
	}
	printf("%d ",a->num);
	while(a->next!=p){
		a=a->next;
		printf("%d ",a->num);
	}
	printf("\n");
}

int main()
{
	int m,n;
	while(scanf("%d%d",&m,&n)!=EOF){
		Node *ah,*bh,*ch;
		ah=create(m);
		bh=create(n);
		ch=merge(ah,bh);
		print(ch);
	}
	return 0;
}

为了合并两个递增的有序链表,可以采用以下步骤: 1. 分别读入两组递增数据,构建两个递增的有序链表,并使用头指针指向链表头节点。由于要求结果链表仍使用原来两个链表的存储空间,不另外占用其他存储空间。 2. 初始化两个工作指针pa和pb,分别指向两个链表的首元结点。 3. 从首元结点开始进行比较,当pa和pb都未到达各自链表的表尾结点时,依次摘取其中较小者重新连接在结果链表的最后。如果两个表中的元素相等,只摘取一个表中的元素,删除该表中的元素,以确保合并后的链表中无重复元素。 4. 当其中一个链表到达表尾结点为空时,将另一个非空链表的剩余元素直接链接在结果链表的最后。 #### 引用[.reference_title] - *1* [两个递增的有序链表合并](https://blog.csdn.net/m0_58789064/article/details/123613239)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [将两个递增的有序链表合并为一个递增的有序链表](https://blog.csdn.net/weixin_60593311/article/details/126152188)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值