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

在这里插入图片描述


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

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

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

	return list;
}

SingleList* create_SingleListByTail(SingleList* list)
{
	SingleList* head, * pmove, * pcreate;
	int length, data;
	head = (SingleList*)malloc(sizeof(SingleList));
	head = list;
	if (head == NULL)
	{
		return NULL;
	}
	else
	{
		head->next = NULL;
	};
	pmove = head;
	printf("请输入要创建的单链表的长度:");
	scanf_s("%d", &length);
	for (int i = 0; i < length; 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;
	}
	return list;
}
//获取链表长度
int foreach_SingleList(SingleList* list)
{
	SingleList* pmove;
	pmove = list->next;
	int count = 0;
	while (pmove != NULL)
	{
		count++;
		pmove = pmove->next;
	}
	return count;
}

void bubblesort_SingleList(SingleList* list, int length)
{
	SingleList* pmove;
	int i, j, temp;
	
	for (i = 0; i < length - 1; i++)
	{
		pmove = list->next;
		for (j = 0; j < length - 1 - i; j++)
		{
			if (pmove->data > pmove->next->data)
			{
				temp = pmove->data;
				pmove->data = pmove->next->data;
				pmove->next->data = temp;
			}
			pmove = pmove->next;
		}
	}
}
//算法1:判断当前结点的值和其后继结点的值是否相等,不等指针后移,若相等则删除当前结点的后继结点
void deleteSameValueFromSingleList(SingleList* list)
{
	SingleList* pmove, * save;
	pmove = list->next;
	while (pmove->next != NULL)
	{
		if (pmove->data != pmove->next->data)
			pmove = pmove->next;
		else
		{
			save = pmove->next;//保存后继结点
			pmove->next = save->next;
			free(save);
		}
	}
}
//算法2:使用快慢指针
void deleteSameValue(SingleList* list)
{
	SingleList* slow, * fast, * save, * delete;
	slow = list;//慢指针指向表头结点
	fast = list->next;//快指针指向表头后一个结点
	save = NULL;
	delete = NULL;
	if (list == NULL)
	{
		printf("链表已为空!\n");
		exit(-1);
	}
	while (fast != NULL)
	{
		if (slow->data != fast->data)//值不等时快慢指针各自后移
		{
			slow = fast;
			fast = fast->next;
		}
		else
		{
			//值相等时删除快指针指向的结点,快指针后移,慢指针不动
			delete = fast;
			save = fast->next;
			slow->next = save;
			fast = save;
			free(delete);
		}
	}
}

//销毁单链表
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;
}

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

int main(void)
{
	SingleList* list, * mylist;
	int length;
	list = init_SingleList();
	mylist = create_SingleListByTail(list);//尾插法创建单链表
	printf("初始单链表为:\n");
	printf_SingleList(mylist);
	length = foreach_SingleList(mylist); //链表的长度
	//printf("length=%d\n", length);
	bubblesort_SingleList(mylist, length);//冒泡排序
	printf("排序后的链表为:\n");
	printf_SingleList(mylist);

	//deleteSameValueFromSingleList(mylist);//算法1
	deleteSameValue(mylist);//算法2
	printf("删除相同元素后的链表为:\n");
	printf_SingleList(mylist);
	destory_SingleList(mylist);//销毁单链表

	system("pause");
	return EXIT_SUCCESS;
}

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

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