王道数据结构链表算法题第十三题

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 

typedef struct node
{
	int data;
	struct node* next;
	int size;
}SingleList;

SingleList* init_SingleList()
{
	SingleList* list = malloc(sizeof(SingleList));
	if (list == NULL)
	{
		return NULL;
	}
	list->data = 0;
	list->size = 0;
	list->next = NULL;

	return list;
}

//尾插法创建单链表
void create_SingleListByTail(SingleList* list)
{
	SingleList* pmove, * pcreate, * head;
	int len, data;
	head = (SingleList*)malloc(sizeof(SingleList));
	head = list;
	if (head == NULL)
	{
		return NULL;
	}
	else
	{
		head->next = NULL;
	}
	pmove = head;
	printf("请输入要创建的链表的长度:");
	scanf_s("%d", &len);
	if (len == 0)
		list=NULL;
	else
	{
		printf("请输入数据:\n");
		for (int i = 0; i < len; i++)
		{
			scanf_s("%d", &data);
			pcreate = (SingleList*)malloc(sizeof(SingleList));
			if (pcreate == NULL)
			{
				return NULL;
			}
			pcreate->data = data;
			pcreate->next = NULL;

			//插入操作
			pmove->next = pcreate;
			pmove = pcreate;

			list->size++;
		}
	}
}

//冒泡排序
void bubbleSort(SingleList* list,int flag)
{	
	SingleList* pCurrent, * pAfter;
	for (int i = 0; i < list->size - 1; i++)
	{
		pCurrent = list->next;
		pAfter = pCurrent->next;
		for (int j = 0; j < list->size - i - 1; j++)
		{
			if (flag == 1)
			{
				if (pCurrent->data > pAfter->data)
				{
					int temp = pCurrent->data;
					pCurrent->data = pAfter->data;
					pAfter->data = temp;
				}
			}
			else
			{
				if (pCurrent->data < pAfter->data)
				{
					int temp = pCurrent->data;
					pCurrent->data = pAfter->data;
					pAfter->data = temp;
				}
			}
			pCurrent = pCurrent->next;
			pAfter = pAfter->next;
		}
	}
}
//算法1:先将两个升序的链表合并成一个升序链表,在进行按降序排序
void merge(SingleList* la, SingleList* lb)
{
	SingleList* pmove, * save, * pa, * pb;
	
	pmove = la;
	pa = la->next;
	pb = lb->next;
	pmove->next = NULL;//将a表的头结点断开,做合并后的链表的表头
	while (pa != NULL && pb != NULL)
	{
		if (pa->data <= pb->data)//处理a表
		{
			save = pa->next;//保存后继结点
			pa->next = NULL;//断链
			pmove->next = pa;//链接
			//指针下移继续处理下一个结点
			pmove = pa;
			pa = save;
		}
		else//处理b表
		{
			save = pb->next;
			pb->next = NULL;
			pmove->next = pb;
			pmove = pb;
			pb = save;
		}
	}
	//a表没有处理完成,将a表剩余数据链接到合并链表后
	if (pa != NULL)
		pmove->next = pa;
	//b表没有处理完成,将b表剩余数据链接到合并链表后
	if (pb != NULL)
		pmove->next = pb;
	la->size += lb->size;
	free(lb);//释放b表的头结点
	
}
//算法2:用链表的头插法进行合并
void MergeTwoSingleList(SingleList* la, SingleList* lb)
{
	SingleList* save, * pa, * pb;
	save = NULL;
	pa = la->next;
	pb = lb->next;
	la->next = NULL;//将a表的头结点断开,用作合并后的链表的头结点
	while (pa != NULL && pb != NULL)
	{
		if (pa->data <= pb->data)
		{
			save = pa->next;//保存后继结点
			//链表的头插
			pa->next = la->next;
			la->next = pa;
			pa = save;//指针下移
		}
		else
		{
			save = pb->next;
			pb->next = la->next;
			la->next = pb;
			pb = save;
		}
	}
	while (pa != NULL)
	{
		save = pa->next;//保存后继结点
			//链表的头插
		pa->next = la->next;
		la->next = pa;
		pa = save;//指针下移
	}
	while (pb != NULL)
	{
		save = pb->next;//保存后继结点
			//链表的头插
		pb->next = la->next;
		la->next = pb;
		pb = save;//指针下移
	}
	free(lb);//释放b表的头结点
}

void printf_SingleList(SingleList* list)
{
	SingleList* pmove;
	pmove = list->next;
	while (pmove != NULL)
	{
		printf("%d ", pmove->data);
		pmove = pmove->next;
	}
	printf("\n");
}

//销毁单链表
void destory_SingleList(SingleList* list)
{
	SingleList* currentNode, * nextNode;
	currentNode = list->next;
	while (currentNode != NULL)
	{
		nextNode = currentNode->next;
		free(currentNode);
		currentNode = nextNode;
	}
	free(list);
	list = NULL;
}

int main(void)
{
	SingleList* a_list, * b_list;
	a_list = init_SingleList();
	create_SingleListByTail(a_list);
	if (a_list != NULL)
	{
		bubbleSort(a_list,1);
	}
	
	printf("链表A为:\n");
	printf_SingleList(a_list);

	b_list = init_SingleList();
	create_SingleListByTail(b_list);
	if (b_list != NULL)
	{
		bubbleSort(b_list,1);
	}
	
	printf("链表B为:\n");
	printf_SingleList(b_list);

	//merge(a_list, b_list);
	//bubbleSort(a_list,0);
	MergeTwoSingleList(a_list, b_list);
	printf("合并后的链表为:\n");
	printf_SingleList(a_list);

	destory_SingleList(a_list);
	destory_SingleList(b_list);

	system("pause");
	return EXIT_SUCCESS;
}



王道数据结构链表算法题第十三题_第1张图片

王道数据结构链表算法题第十三题_第2张图片

你可能感兴趣的:(链表,数据结构,算法)