一些關於鏈表的題

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef  int DataType;

typedef struct Node{
	DataType data;
	struct Node* next;
}Node ,*pNode ,*pList;



typedef  struct ComplexNode{
	DataType data;
	struct ComplexNode* next;
	struct ComplexNode* random;
}ComplexNode;


void InitLinkList(pList* pplist);  //初始化
void PushFront(pList* pplist, DataType d);//在頭部插入一個節點
void PopFront(pList* pplist);//在頭部刪除一個節點
void PushBack(pList* pplist, DataType d);//在尾部插入一個節點
void PopBack(pList* pplist);// 在尾部刪除一個節點
int Find1(pList* pplist, DataType d);// 查找一個節點
pList Find(pList pl, DataType d);// 查找一個節點
void Insert(pList* pplist, DataType d,int pos);// 插入一個節點
void Remove(pList* pplist, DataType d);// 刪除一個節點
void RemoveAll(pList* pplist, DataType d); // 刪除全部節點
void Display(pList pl);// 打印
void show(pList pl);// 打印
void DestroyList(pList* pplist); // 銷燬
void DelNotTail(pNode pos);// 刪除非尾節點
void InsertFrontNode(pNode pos, DataType d);// 指定位置插入
pNode CreatNode(DataType d);// 定義一個新節點
void JosephCycle(pList pl, int k, int n);  //約瑟夫環問題
void ReverseList(pList* pplist);// 逆序所有節點
void BubbleSortList(pList plist);// 冒泡排序
pList Merge(pList* ppl1, pList* ppl2); // 合併兩條鏈表
pList Mix(const pList* ppl1,const  pList* ppl2); // 合併兩條鏈表
void FindMidNode(pList pl); //查找中間節點
void DelLastKNode(pList* pplist, int k); //刪除倒數第k個節點
pNode CheckCycle(pList pl); // 檢查鏈表是否帶環
int GetCycleLenth(pList pl); // 帶環 環的長度
pNode GetCycleEntryNode(pList meet, pList plist); // 查找相遇節點
pNode CycleEntryNode(pList meet, pList plist, int n);//查找相遇節點
pNode CheckCross(pList pl1, pList pl2);  // 判斷環是否相交併求出交點
pNode _CheckCross(pList pl1, pList pl2); // 判斷環是否相交併求出交點
void PrintComplexList(ComplexNode* p); // 打印複雜鏈表
ComplexNode* BuyComplexNode(DataType d); // 定義複雜鏈表的新的節點
ComplexNode* CopyComplexList(ComplexNode *head);// 複雜鏈表的複製

#endif  //__LINKLIST_H__


#define _CRT_SECURE_NO_WARNINGS
#include "LinkList.h"
void InitLinkList(pList* pplist){
	assert(pplist != NULL);
	*pplist = NULL;

}
pNode CreatNode(DataType d){
	pNode temp = (pNode)malloc(sizeof(Node));
	if (temp == NULL){
		perror("use mallloc");
		exit(EXIT_FAILURE);
	}
	memset(temp, 0, sizeof(Node));
	temp->data = d;
	temp->next = NULL;
	return temp;

}
void PushFront(pList* pplist, DataType d){
	assert(pplist != NULL);
	pNode temp = (pNode)malloc(sizeof(Node));
	if (temp == NULL){
		perror("use mallloc");
		exit(EXIT_FAILURE);
	}
	memset(temp, 0, sizeof(Node));
	temp->data = d;
	temp->next = *pplist;
	*pplist = temp; 
		
}
void PopFront(pList* pplist){
	assert(pplist != NULL);
	pList temp = NULL;
	if ((*pplist) == NULL){
		return;
	}
		temp = (*pplist);
		(*pplist) = (*pplist)->next;
		free(temp);
		temp = NULL;

}
void PushBack(pList* pplist, DataType d){
	assert(pplist != NULL);
	pNode ptr = (*pplist);
	pNode temp = (pNode)malloc(sizeof(Node));
	if (temp == NULL){
		perror("use mallloc");
		exit(EXIT_FAILURE);
	}
	memset(temp, 0, sizeof(Node));
	temp->data = d;
	temp->next = NULL;
	if ((*pplist) == NULL){
		(*pplist) = temp;
	}
	else{
		while ( ptr->next != NULL){
			ptr = ptr ->next;
		}
		ptr->next = temp;
	}
}
void PopBack(pList* pplist){
	assert(pplist != NULL);
	pNode ptr = (*pplist);
	pNode cur = (*pplist);
	if ((*pplist) == NULL){
		return;
	}
	else if (ptr->next == NULL){
		free(ptr->next);
		ptr->next = NULL;
		*pplist = NULL;
	}
	else{
		while (ptr->next != NULL){
			cur = ptr;
			ptr = ptr->next;
		}
		free(ptr);
		ptr = NULL;
		cur->next = NULL;
	}
}
int  Find1(pList* pplist, DataType d){
	assert(pplist);
	pNode ptr = (*pplist);
	int i = 0;
	if (*pplist == NULL){

		return 0;
	}
	else if (ptr->next == NULL){
		if (ptr->data == d){
			return 1;
		}
		return -1;
	}
	else{
		while (ptr->next != NULL)
		{
			i++;
			if (ptr->data == d){
				return i;
			}
			ptr = ptr->next;
		}

		if (ptr->data == d){
			i++;
			return i;
		}
		return -1;
	}
}
pList Find(pList pl, DataType d){
	pNode cur = pl;
	if (pl == NULL){
		return NULL;
	}
	while (pl != NULL){
		if (pl->data == d)
			return pl;
		pl = pl->next;
	}
	return NULL;
}
void Display(pList pl){
	
	while (pl != NULL)
	{
		printf("%d  ", pl->data);
		pl = pl->next;
	}
	printf("\n");
}
void Insert(pList* pplist, DataType d, int pos){
	assert(pplist);
	int i = 0;
	pNode ptr = (*pplist);
	pNode cur = (*pplist);
	pNode temp = (pNode)malloc(sizeof(Node));
	if (temp == NULL){
		perror("use mallloc");
		exit(EXIT_FAILURE);
	}
	memset(temp, 0, sizeof(Node));
	temp->data = d;
	if (pos == 1){
		temp->next = *pplist;
		*pplist = temp;
	
	}
	else {
		for (i = 1; i < pos; i++){
			cur = ptr;
			ptr = ptr->next;
		}
		temp->next = cur->next;
		cur->next = temp;
	}
}
void Remove(pList* pplist, DataType d){
	assert(pplist);
	int pos = Find1(pplist ,d);
	int i = 0;
	pNode ptr = (*pplist);
	pNode cur = (*pplist);
	if (ptr == NULL){
		return;
	}
	else if (pos == 1){
		*pplist = ptr->next;
		free(ptr);
		ptr = NULL;
	}
	else if ((pos != -1) && (pos != 0)){
		
		for (i = 1; i < pos; i++){
			cur = ptr;
			ptr = ptr->next;
		}
		cur->next = ptr->next;
		free(ptr);
		ptr = NULL;
	}
}
void RemoveAll(pList* pplist, DataType d){
	assert(pplist);
	pNode ptr = (*pplist);
	pNode cur = (*pplist);
	pNode tem = (*pplist);
	if (*pplist == NULL){
		return ;
	}
	else if (ptr->next == NULL){
		if (ptr->data == d){
			free(ptr);
			ptr = NULL;
			*pplist = NULL;
		}
	}
	else {
		while (ptr !=  NULL){
			if (tem->data == d){
				(*pplist) = tem->next;
				free(tem);
				tem = (*pplist);
				ptr = (*pplist);
			}
			else if (ptr->data == d){
				cur->next = ptr->next;
				free(ptr);
				ptr = cur;
			}
			cur = ptr;
			ptr = ptr->next;
		}
	   if ((*pplist)->next == NULL){
		   if ((*pplist)->data == d){
			   free(*pplist);
			   *pplist = NULL;
		   }
		}

	}
}
void show(pList pl){
	
	if (pl == NULL){
		return;
	}
	else{
		show(pl->next);
		printf("%d  ", pl->data);
	}
}
void DestroyList(pList* pplist){
	assert(pplist);
	pNode ptr = *pplist;
	pNode cur = *pplist;
	if (*pplist == NULL){
		return;
	}
	else{
		while (ptr != NULL){
			*pplist = ptr->next;
			free(ptr);
			ptr = *pplist;
		}
	}
}
void DelNotTail(pNode pos){
	pNode Del = NULL;
	Del = pos->next;
	pos->data = Del->data;
	pos->next = Del->next;
	free(Del);
	Del = NULL;
}
void InsertFrontNode(pNode pos, DataType d){
	pNode cur = CreatNode(d);
	DataType tmp = 0;
	tmp = pos->data;
	pos->data = cur->data;
	cur->data = tmp;
	cur->next = pos->next;
	pos->next = cur;

}
void JosephCycle(pList pl, int k, int n){
	pNode cur = pl;
	pNode Del = NULL;
	int i = 0;
	int j = n;
	while (cur ->next != NULL){
		cur = cur->next;
	}
	cur->next = pl;
	cur = pl;

	while (k != 1){
		j = n;
		while (j > 2)
		{
			cur = cur->next;
			j--;
		}
		Del = cur ->next;
		cur ->next = Del->next;
		printf("%d->",Del->data);
		free(Del);
		cur = cur->next;
		k--;
	}
	printf("over\n");
}
void ReverseList(pList* pplist){
	pNode newhead = *pplist;
	pNode ptr = *pplist;
	pNode cur = *pplist;
	ptr = ptr->next;
	cur = cur->next;

	while (ptr  != NULL){
		ptr = ptr->next;
		cur->next = newhead;
		newhead = cur;
		cur = ptr;
	}
	(*pplist)->next = NULL;
	*pplist = newhead;
}
void BubbleSortList(pList plist){
	pNode cur = plist;
	pNode ptr1 = plist;
	pNode ptr2 = plist;
	pNode flag = NULL;
	DataType tmp = 0;
	while (cur != NULL)
	{
		while (flag != NULL)
		{
			ptr2 = ptr1;
			ptr1 = ptr1->next;
			if ((ptr2->data) > (ptr1->data)){
				tmp = ptr2->data;
				ptr2->data = ptr1->data;
				ptr1->data = tmp;
			}
			flag = flag->next;
		}
		ptr1 = plist;
		cur = cur->next;
		flag = cur;
	}
}
pList Merge(pList* ppl1, pList* ppl2){
	assert(ppl1);
	assert(ppl2);
	pNode head = NULL;
	pNode cur = *ppl1;
	pNode ptr = *ppl2;
	pNode temp = NULL;
	if (*ppl1 == NULL){
		head = *ppl2;
		return head;
	}
	else if (*ppl2 == NULL){
		head = *ppl1;
		return head;
	}
	else{

		if (cur->data > ptr->data){
			head = ptr;
			temp = ptr->next;
			while (cur->next != NULL || temp->next != NULL){
				if (cur->data > temp->data)
				{
					ptr->next = temp;
					ptr = ptr->next;
					temp = temp->next;
				}
				else{

					ptr->next = cur;
					ptr = ptr->next;
					cur = cur->next;
				}

			}
			if (temp->data > cur ->data ){
				ptr->next = cur;
				cur->next = temp;
			}
			else{
				temp->next = cur;
			
			}
			return head;
		}
		else{
			head = cur;
			temp = cur->next;
			while (ptr->next != NULL || temp->next != NULL){
				if (ptr->data > temp->data)
				{
					cur->next = temp;
					cur = cur->next;
					temp = temp->next;
				}
				else{
					cur->next = ptr;
					cur = cur->next;
					ptr = ptr->next;
				}

			}
			if (temp->data > ptr->data){
				cur->next = ptr;
				ptr->next = temp;
			}
			else{
				ptr->next = temp;
			}
			return head;
		}
	}
}
pList Mix(const pList* ppl1,const pList* ppl2){
	assert(ppl1);
	assert(ppl2);
	pNode head = NULL;
	pNode cur = *ppl1;
	pNode ptr = *ppl2;
	pNode temp = NULL;
	if (*ppl1 == NULL){
		head = *ppl2;
		return head;
	}
	else if (*ppl2 == NULL){
		head = *ppl1;
		return head;
	}
	else
	{
		if (cur->data < ptr->data){
			head = cur;
			head->next = Mix(&(cur->next),&ptr);
		}
		else{
			head = ptr;
			head->next = Mix(&cur, &(ptr->next));
		}
		return head;
	}

}
void FindMidNode(pList pl){
	pNode cur = pl;
	pNode ptr = pl;

	while (ptr->next !=  NULL)
	{
		cur = cur->next;
		ptr = ptr->next;
		if (ptr->next != NULL){
			ptr = ptr->next;
		}
		
	}

	printf("%d\n", cur->data);
}
void DelLastKNode(pList* pplist, int k){
	assert(pplist);
	pNode ptr = *pplist;
	pNode cur = *pplist;
	pNode temp = NULL;
	pNode Del = NULL;
	if (*pplist == NULL){
		return;
	}
	else{
		while (cur->next != NULL){
			if (--k <= 0)
			{    
				temp = ptr;
				ptr = ptr->next;
			}
			cur = cur->next;
		}
		printf("%d\n", ptr->data);
		if (ptr->next == NULL)
		{
			temp->next = NULL;
			free(ptr);
		}
		else
		{
			Del = ptr->next;
			ptr->data = Del->data;
			ptr->next = Del->next;
			free(Del);
		}
	}
}
pNode CheckCycle(pList pl){
	pNode cur = pl;
	pNode ptr = pl;
	while (ptr ->next != NULL){
		cur = cur->next;
		ptr = ptr->next->next;
		if (cur == ptr){ 
			return cur;
		}
		if (ptr == NULL){
			break;
		}
	}
	return NULL;
}
int GetCycleLenth(pList pl){
	int count = 0;
	pNode cur = pl;
	while (1){
		cur = cur->next;
		count++; 
		if (pl == cur){
			break;
		}
	
	}
	return count;
}
pNode GetCycleEntryNode(pList meet, pList plist){
	   
	pNode cur = meet;
	pNode ptr = plist;
	while (1){
		cur = cur->next;
		ptr = ptr->next;
		if (cur == ptr){
			return cur;
		}
	}
	/*return NULL;*/

}
pNode CycleEntryNode(pList meet,pList plist, int n){
	pNode cur = plist;
	pNode ptr = meet;
	while (n--){
		cur = cur->next;
	}
	while (/*cur != ptr*/1){
		
		if (cur == ptr){
			return cur;
		
		}
		cur = cur->next;
		ptr = ptr->next;
	}
	/*return cur;*/
}

pNode CheckCross(pList pl1, pList pl2)
{
	pNode cur = pl1;
	pNode ptr = pl2;
	pNode ret = NULL;
	pNode meet = NULL;
	pNode temp = NULL;
	while (cur->next != NULL){
		cur = cur->next;
	}
	while ( ptr->next != NULL ){
		ptr = ptr->next;
	}
	if (cur != ptr) 
	{
		
		return NULL;
	}
	else
	{
		cur->next = pl1;
		ret = CheckCycle(pl2);
		temp = GetCycleEntryNode(ret, pl2);
		cur->next = NULL;
		return temp;
	}
}
pNode _CheckCross(pList pl1, pList pl2)
{
	int len1 = 0;
	int len2 = 0;
	pNode cur = pl1;
	pNode ptr = pl2;
	int ret = 0;
	while (cur != NULL){
		len1++;
		cur = cur->next;
	}
	while (ptr != NULL){
		len2++;
		ptr = ptr->next;
	}
	cur = pl1;
	ptr = pl2;
	if (len1 > len2){
		ret = len1 - len2;
		while (ret--){
			cur = cur->next;
		}
		while (cur !=NULL && ptr != NULL)
 		{
			if (cur == ptr)
			{
				return cur;
			}
			cur = cur->next;
			ptr = ptr->next;
		}
		return NULL;
	}
	else if (len2 > len1){
		ret = len2 - len1;
		while (ret--){
			ptr = ptr->next;
		}
		while (cur != NULL && ptr != NULL)
		{
			if (cur == ptr)
			{
				return cur;
			}
			cur = cur->next;
			ptr = ptr->next;
		}
		return NULL;
	}
	else{

		while (cur != NULL && ptr != NULL)
		{
			if (cur == ptr)
			{
				return cur;
			}
			cur = cur->next;
			ptr = ptr->next;
		}
		return NULL;

	}
}
void PrintComplexList(ComplexNode* p)
{
	while (p != NULL){
	
		printf("%d->%d ",p->data, p->random->data);
		p = p->next;
	}
	printf("\n");
}
ComplexNode* BuyComplexNode(DataType d)
{
	ComplexNode* temp = (ComplexNode* )malloc(sizeof(ComplexNode));
	if (temp == NULL){
		perror("use mallloc");
		exit(EXIT_FAILURE);
	}
	memset(temp, 0, sizeof(ComplexNode));
	temp->data = d;
	temp->next = NULL;
	temp->random = NULL;
	return temp;
}
ComplexNode* CopyComplexList(ComplexNode *head)
{
	ComplexNode* cur = head;
	ComplexNode* ptr = NULL;
	ComplexNode* newhead = NULL;
	//複製節點,並串成一條鏈
	if (cur == NULL)
	{ 
		return NULL;
	}
	while (cur != NULL)
	{   
		ptr = cur;
		ComplexNode* temp = NULL;
		temp = BuyComplexNode(cur->data);
		cur = cur->next;
		ptr->next = temp;
		temp->next = cur;
		
	}
	//複製隨機指針
	cur = head->next;
	ptr = head;
	while (cur->next != NULL)
	{
		cur->random = ptr->random->next;
		ptr = ptr->next->next;
		cur = cur->next->next;
	}
	cur->random = ptr->random->next;
   //將混合鏈分開
	PrintComplexList(head);
	cur = head->next;
	ptr = head;
	newhead = head->next;
	while (cur->next != NULL)
	{
		ptr->next = cur->next;
		ptr = ptr->next;
		cur->next = ptr->next;
		cur = cur->next;
	}
	ptr->next = NULL;
	return newhead;
}

#define _CRT_SECURE_NO_WARNINGS

#include "LinkList.h"

void test1(){
	Node* head = NULL;
	pList ret = NULL;
	InitLinkList(&head);
	
	PushBack(&head, 1);
	PushBack(&head, 2);
	PushBack(&head, 3);
	PushBack(&head, 4);
	PushBack(&head, 5);
	PushBack(&head, 6);
	PushBack(&head, 7);
	PushBack(&head, 8);
	Display(head);
	//int i = Find(&head,1);
	//printf("%d\n", i);
	/*Insert(&head, 4, 4);*/
	/*Remove(&head,2);*/
	/*printf("%d\n", Find(head, 2)->data);*/
	ret = Find(head, 2);
	DelNotTail(ret);
	Display(head);
	DestroyList(&head);

	/*PopBack(&head);
	Display(head);
	PopBack(&head);
	Display(head);
	PopBack(&head);
	Display(head);
	PopBack(&head);
	Display(head);*/
	/*PopBack(&head);
	Display(head);*/
}


void test(){
	Node* head = NULL;
	InitLinkList(&head);
	PushFront(&head, 1);
	PushFront(&head, 2);
	PushFront(&head, 3);
	PushFront(&head, 4);
	Display(head);

	PopFront(&head);
	Display(head);
	PopFront(&head);
	Display(head);
	PopFront(&head);
	Display(head);
	PopFront(&head);
	Display(head);
}
void test2(){
	Node* head = NULL;
	pList ret = NULL;
	InitLinkList(&head);

	PushBack(&head, 1);
	PushBack(&head, 2);
	PushBack(&head, 3);
	PushBack(&head, 4);
	PushBack(&head, 5);
	PushBack(&head, 6);
	PushBack(&head, 7);
	PushBack(&head, 8);
	Display(head);
	ret = Find(head, 1);
	InsertFrontNode(ret, 2);
	Display(head);
}
void test3(){
	Node* head = NULL;
	pList ret = NULL;
	int i = 0;
	for (i = 1; i <= 41; i++){
		PushBack(&head, i);
	}
	Display(head);
	JosephCycle(head, 41, 3);
	//PushBack(&head, 1);
	//PushBack(&head, 2);
	//PushBack(&head, 3);
	//PushBack(&head, 4);
	//PushBack(&head, 5);
	//PushBack(&head, 6);
	//PushBack(&head, 7);
	//PushBack(&head, 8);
	//Display(head);
	//ReverseList(&head);
	/*Display(head);*/
}
void test4(){
	Node* head = NULL;
	pList ret = NULL;
	PushBack(&head, 8);
	PushBack(&head, 7);
	PushBack(&head, 6);
	PushBack(&head, 5);
	PushBack(&head, 4);
	PushBack(&head, 3);
	PushBack(&head, 2);
	PushBack(&head, 1);
	Display(head);
	/*ReverseList(&head);
	Display(head);*/
	BubbleSortList(head);
	Display(head);
}
void test5(){
	Node* head = NULL;
	Node* head1 = NULL;
	pList ret = NULL;
	PushBack(&head, 1);
	PushBack(&head, 3);
	PushBack(&head, 5);
	PushBack(&head, 7);
	PushBack(&head, 11);

	PushBack(&head1, 0);
	PushBack(&head1, 2);
	PushBack(&head1, 4);
	PushBack(&head1, 6);
	PushBack(&head1, 8);
	PushBack(&head1, 10);
	
	Display(head);
	Display(head1);
	/*ret = Merge(&head, &head1);*/
	ret = Mix(&head, &head1);
	Display(ret);
}
void test6(){
	Node* head = NULL;
	pList ret = NULL;
	pList temp = NULL;
	pList Entry = NULL;
	int len = 0;
	InitLinkList(&head);

	PushBack(&head, 1);
	PushBack(&head, 2);
	PushBack(&head, 3);
	PushBack(&head, 4);
	PushBack(&head, 5);
	PushBack(&head, 6);
	PushBack(&head, 7);
	PushBack(&head, 8);
	PushBack(&head, 9);
	PushBack(&head, 10);
	Display(head);
	/*FindMidNode(head);*/
	ret = Find(head, 10);
	ret->next = Find(head, 6);
	/*DelLastKNode(&head,1);*/
	temp = CheckCycle(head);
	/*printf("%d\n", temp->data);*/
	len = GetCycleLenth(temp);
	printf("%d\n", len);
	/*Entry = GetCycleEntryNode(temp, head);*/
	Entry = CycleEntryNode(temp, head, len);
	printf("%d\n",Entry->data);

}
void test7(){
	Node* head = NULL;
	Node* head1 = NULL;
	pList ret = NULL;
	pList temp = NULL;
	pList Entry = NULL;
	int len = 0;
	InitLinkList(&head);

	
	PushBack(&head, 3);
	PushBack(&head, 5);
	PushBack(&head, 6);
	PushBack(&head, 7);
	PushBack(&head, 8);
	Display(head);


	InitLinkList(&head1);

	PushBack(&head1, 0);
	PushBack(&head1, 2);
	PushBack(&head1, 4);
	PushBack(&head1, 4);
	
	PushBack(&head1, 7);
	PushBack(&head1, 8);
	PushBack(&head1, 9);
	PushBack(&head1, 10);
	PushBack(&head1, 11);
	Display(head1);
	ret = Find(head, 6);
	ret->next = Find(head1, 7);
	Display(head);
	temp = _CheckCross(head,head1);
	/*temp = CheckCycle(head);*/
	if (temp != NULL)
	printf("%d\n", temp->data);

}
void test8(){
	ComplexNode* head = NULL;
	ComplexNode* temp = NULL;
	temp = BuyComplexNode(1);
	head = temp;
	temp = BuyComplexNode(2);
	head->next = temp;
	temp = BuyComplexNode(3);
	head->next->next = temp;
	temp = BuyComplexNode(4);
	head->next->next->next = temp;
	head->random = head->next->next;
	head->next->random = head;
	head->next->next->random = head->next;
	head->next->next->next->random = head->next->next;
	PrintComplexList(head);
	CopyComplexList(head);
	
}
int main(){
	/*test();*/
	/*test1();*/
	/*test2();*/
	test3();
	//test4();
	//test5();
	//test6();
	//test7();
	/*test8();*/
	system("pause");
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章