Home  >  Article  >  Backend Development  >  C language implements the merge of two ordered linked lists (code example)

C language implements the merge of two ordered linked lists (code example)

青灯夜游
青灯夜游forward
2020-10-26 15:07:564042browse

This article introduces the method of merging two ordered linked lists using c language through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

C language implements the merge of two ordered linked lists (code example)

Tutorial recommendation: "c language tutorial video"

C language implements two Merging of ordered linked lists

There are two ordered singly linked lists, and the code can be used to merge the two singly linked lists into a new ordered list, which requires the use of the space of the old table. Memory cannot be newly allocated

#include <stdio.h>#include <stdlib.h>typedef struct List{
	int a;
	struct List *next;}list;void newList(list *l){
	//初始化头节点
	l->next = NULL;}void setList(list * l){
	//建立链表
	int i = 1;
	int j;
	while (i)
	{
		scanf_s("%d", &j);
		if (j == -1)
		{
			i = 0;
		}
		else
		{
			list *l1 = (list *)malloc(sizeof(list));//为新的结点分派内存
			l1->a = j;//储存数据
			/*
			将最后结点的next区域指向新结点
			将新结点的next区域指向设置为空
			*/
			l->next = l1;
			l1->next = NULL;
			l = l->next;
		}
	}}void printfList(list *l){
	printf("该链表内容为:\n");
	while (l->next)
	{
		printf("%d\t", l->next->a);
		l = l->next;
	}
	printf("\n");}list *add(list *LA, list *LB){
	//记录两个链表的头结点
	list *la=LA;
	list *l = LA;
	list *lb = LB;
	//移动指针
	LA = LA->next;
	LB = LB->next;
	la->next = NULL;
	while (LA!=NULL&&LB!=NULL)
	{
		/*
		将两个结点的数据进行比较,数据较小的结点接在头结点后面,
		*/
		if (LA->a < LB->a)
		{
			la->next = LA;
			la = LA;
			LA = LA->next;
		}
		else
		{
			la->next = LB;
			la = LB;
			LB = LB->next;
		}
	}
	//若其中一个链表的结点已经全接在新表中则将另一个链表的剩余结点接在新表的后面
	if (LA)
	{
		la->next = LA;
	}
	if(LB)
	{
		la->next = LB;
	}
	free(lb);
	return l;}int main(){
	//为结点分配内存
	list *LA = (list *)malloc(sizeof(list));
	list *LB = (list *)malloc(sizeof(list));
	//初始化结点
	newList(LA);
	newList(LB);
	//建立链表
	setList(LA);
	setList(LB);
	//输出链表的内容
	printf("LA的数据:\n");
	printfList(LA);
	printf("LB的数据:\n");
	printfList(LB);
	list *LC = add(LA, LB);
	//输出合并后的新表
	printfList(LC);
	system("pause");
	return 0;}

C language implements the merge of two ordered linked lists (code example)

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of C language implements the merge of two ordered linked lists (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete