合并链表C语言

 

1.待合并的两个链表分别为A,B。

2.用a,b分别指向a,b,以便比较数据域大小。

3.用指针p指向待插入合成链表的节点(红色所指)。

4.红色连线即头插法的顺序 。

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node,*list;

list create(){
	list L;
	int c;
	L = (list)malloc(sizeof(node));
	L->next = NULL;
	node *p , *tail;
	tail = L;
	c = getchar();
	while(c != '#'){
		p = (node *)malloc(sizeof(node));
		p->data = c;
		tail->next = p;
		tail = p;
		c = getchar();
	}
	tail->next = NULL;
	return L;
}

void Showlist(list A)
{
	node *p = A->next;
	while(p != NULL)
	{
		printf("%c",p->data );
		p = p->next ;
	}
	
	return ;
}

list Mergelist(list A,list B)
{
	node *a,*b,*p;//p用来指向下一个被操作的结点
	list newlist;//新建一个头结点 
	newlist  = A;//用A的头结点,不浪费                   
	newlist = (node*)malloc(sizeof(node));
	p = NULL;
	a = A->next ;
	b = B->next ;
	while(a&&b)
	{
		if(a->data <= b->data )
		{
			p = a;
			a = a->next ;
			p->next = newlist->next ;//头插法,实现倒序 
			newlist->next = p; 
		}
		else
		{
			p = b;
			b = b->next ;
			p->next = newlist->next ;
			newlist->next = p;
			
		}
	}
	if(a)a=b;//其中一个已遍历完
	while(a)
	{
		p = a;
		a = a->next ;
		a->next = newlist->next ;
		newlist->next = a;
	 } 
	return newlist;
}
int main()
{
	list A,B,newlist;
	
	A = (list)malloc(sizeof(node));
	B = (list)malloc(sizeof(node));
	A = create();
	printf("展示链表A:");
	Showlist(A);
	B = create();
	printf("展示链表B:");
	Showlist(B); 
	newlist = Mergelist(A,B);
	printf("展示合并后的链表:");
	Showlist(newlist);
	
	return 0;
	
 } 

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个示例程序,可以实现输入两个链表合并它们: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 创建新节点 struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } // 合并两个链表 struct ListNode* mergeLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode dummy = {0, NULL}; struct ListNode* tail = &dummy; while (l1 && l2) { if (l1->val < l2->val) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } tail->next = l1 ? l1 : l2; return dummy.next; } // 释放链表内存 void freeList(struct ListNode* head) { while (head) { struct ListNode* node = head; head = head->next; free(node); } } int main() { struct ListNode *l1 = NULL, *l2 = NULL; int val; // 输入第一个链表 printf("Input the first list (separated by spaces, end with a negative number):\n"); while (scanf("%d", &val) == 1 && val >= 0) { struct ListNode* node = createNode(val); if (!l1) l1 = node; else { struct ListNode* tail = l1; while (tail->next) tail = tail->next; tail->next = node; } } // 输入第二个链表 printf("Input the second list (separated by spaces, end with a negative number):\n"); while (scanf("%d", &val) == 1 && val >= 0) { struct ListNode* node = createNode(val); if (!l2) l2 = node; else { struct ListNode* tail = l2; while (tail->next) tail = tail->next; tail->next = node; } } // 合并链表 struct ListNode* merged = mergeLists(l1, l2); // 输出合并后的链表 printf("Merged list:\n"); struct ListNode* p = merged; while (p) { printf("%d ", p->val); p = p->next; } printf("\n"); // 释放内存 freeList(l1); freeList(l2); freeList(merged); return 0; } ``` 示例输入: ``` Input the first list (separated by spaces, end with a negative number): 1 3 5 7 9 -1 Input the second list (separated by spaces, end with a negative number): 2 4 6 8 10 -1 ``` 示例输出: ``` Merged list: 1 2 3 4 5 6 7 8 9 10 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值