链表的部分功能与题目

1.链表逆序。

2.删除链表中重复的数据,只保留一份。

3.检查链表中是否有环。

4.查找环型链表的入口。

5.检查两个链表是否是Y型链表,并找出入口。

6.合并两并两个有序链表,结果依然保持有序。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define TYPE int

typedef struct Node
{
	TYPE data;
	struct Node* next;
}Node;

// 创建节点
Node* creat_node(TYPE data)
{
	Node* node = malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	return node;
}

typedef struct List
{
	Node* head;
	Node* tail;
	size_t size;
}List;

// 创建
List* creat_list(void);
// 销毁
void destory_list(List* list);
// 头添加
void head_add_list(List* list,TYPE data);
// 尾添加
void tail_add_list(List* list,TYPE data);
// 头删除
bool head_del_list(List* list);
// 尾删除
bool tail_del_list(List* list);
// 插入
bool insert_list(List* list,int index,TYPE data);
// 位置删除
bool delete_index_list(List* list,int index);
// 值删除
bool delete_value_list(List* list,TYPE data);
// 排序
void sort_list(List* list);
// 遍历
void show_list(List* list);

void release(List* list)//链表逆序
{
	Node* p1 = list->head;
	Node* p2 = p1->next;
	list->tail = p1;
	
	while(NULL != p2->next)
	{
		Node* p3 = p2->next;
		p2->next = p1;
		p1 = p2;//现在的p2是P1,相当于 原来 0x11 变成了 现在的 0x12,赋值的是地址
		p2 = p3;//现在的P3是P2
	}

	p2->next = p1;
	list->head = p2;
	list->tail->next = NULL;//
}

void row_weight(List* list)//删除链表中重复的数据,只保留一份。
{
	for(Node* i=list->head; NULL!=i->next; i=i->next)
	{
		for(Node* j=i; NULL!=j->next;)
		{
			if(j->next->data != i->data)
			{
				j=j->next;
				continue;
			}
		
			Node* temp = j->next;//j是temp的前一个节点
			j->next = temp->next;//
			free(temp);  //目的是删掉j->next
		}
	}
}

bool is_ring(List* list)//检查链表中是否有环。
{
	Node* n1 = list->head;
	Node* n2 = n1->next;
	
	while(NULL != n2 && NULL != n2->next)
	{
		if(n1 == n2) return true;
		n1 = n1->next;
		n2 = n2->next->next;
	}
	
	return false;
}

TYPE* find_ring_in(List* list)//查找环型链表的入口
{
	if(!is_ring(list)) return NULL;


	Node* node = list->head;
	while(true)
	{
		for(Node* i=list->head; i!=node; i=i->next)
		{
			if(node->next == i)
			{
				return &i->data;
			}
		}
		node = node->next;
	}
}

bool is_ylist(List* l1,List* l2)//检查两个链表是否是Y型链表,并找出入口。
{
	Node* n1 = l1->head;
	while(NULL != n1->next) n1 = n1->next;
	Node* n2 = l2->head;
	while(NULL != n2->next) n2 = n2->next;

	return n2 == n1;
}

TYPE* in_ylist(List* l1,List* l2)
{
	if(!is_ylist(l1,l2)) return NULL;

	size_t len1 = 0 , len2 = 0;
	Node *n1 = l1->head , *n2 = l2->head;
	while(NULL != n1)
	{
		n1 = n1->next;
		len1++;
	}

	while(NULL != n2)
	{
		n2 = n2->next;
		len2++;
	}
	
	n1 = l1->head , n2 = l2->head;
	if(len1 > len2)
	{
		for(int i=0; i<len1-len2; i++)
		{
			n1 = n1->next;
		}
	}
	else
	{
		for(int i=0; i<len2-len1; i++)
		{
			n2 = n2->next;
		}
	}

	while(NULL!=n1 && NULL!=n2)//前面将n1 ,n2都定义处理成了一样的有效长度,当两个链在某点地址相同是则为入口
	{
		if(n1 == n2)
		{
			return &n1->data;
		}

		n1 = n1->next;
		n2 = n2->next;//是节点向后
	}
}

Node* _merge_list(Node* n1,Node* n2)//合并两并两个有序链表,结果依然保持有序。
{
	if(NULL == n1 && NULL == n2) return NULL;
	if(NULL != n1 && NULL  == n2 ) return n1;
	if(NULL == n1 && NULL != n2) return n2;
	if(n1->data < n2->data)
	{
		n1->next = _merge_list(n1->next,n2);
		return n1;
	}
	else
	{
		n2->next = _merge_list(n2->next,n1);
		return n2;
	}
}

List* merge_list(List* l1,List* l2)
{
	List* list = creat_list();
	
	Node *n1 = l1->head ,*n2 = l2->head;
	if(n1->data < n2->data)
	{
		list->head = n1;
		n1 = n1->next;
	}
	else
	{
		list->head = n2;
		n2 = n2->next;
	}

	Node *node = list->head;
	while(NULL != n1 && NULL != n2)
	{
		if(n1->data < n2->data)
		{
			node->next = n1;
			n1 = n1->next;
		}
		else
		{
			node->next = n2;
			n2 = n2->next;
		}
		node = node->next;
	}
	if(NULL != n1)
		node->next = n1;
	else
		node->next = n2;
	return list;
}

// 创建
List* creat_list(void)
{
	List* list = malloc(sizeof(List));
	list->head = NULL;
	list->tail = NULL;
	return list;
}

// 销毁
void destory_list(List* list)
{
	while(0 < list->size)
	{
		head_del_list(list);
	}

	free(list);
}

// 头添加
void head_add_list(List* list,TYPE data)
{
	Node* node = creat_node(data);

	if(0 == list->size)
	{
		list->head = node;
		list->tail = node;
	}
	else
	{
		node->next = list->head;
		list->head = node;
	}
	list->size++;
}

// 尾添加
void tail_add_list(List* list,TYPE data)
{
	Node* node = creat_node(data);

	if(0 == list->size)
	{
		list->head = node;
		list->tail = node;
	}
	else
	{
		list->tail->next = node;
		list->tail = node;
	}
	list->size++;
}

// 头删除
bool head_del_list(List* list)
{
	if(0 >= list->size) return false;

	Node* temp = list->head;
	list->head = temp->next;
	
	if(1 == list->size) 
	{
		list->tail = NULL;
	}

	free(temp);
	list->size--;
	return true;
}

// 尾删除
bool tail_del_list(List* list)
{
	if(0 >= list->size) return false;
	
	if(1 == list->size)
	{
		head_del_list(list);
		return true;
	}

	Node* prev = list->head;
	while(prev->next != list->tail)
	{
		prev = prev->next;
	}

	free(list->tail);
	list->tail = prev;
	prev->next = NULL;
	list->size--;
	
	return true;
}

// 插入
bool insert_list(List* list,int index,TYPE data)
{
	if(index < 0 || index >= list->size) return false;
	
	if(0 == index)
	{
		head_add_list(list,data);
		return true;
	}

	Node* prev = list->head;
	for(int i=0; i<index-1; i++)
	{
		prev = prev->next;
	}

	Node* node = creat_node(data);
	node->next = prev->next;
	prev->next = node;
	list->size++;
	
	return true;
}

// 位置删除
bool delete_index_list(List* list,int index)
{
	if(index < 0 || index >= list->size) return false;
	
	if(0 == index)
		head_del_list(list);
	else if(list->size == index+1)
		tail_del_list(list);
	else
	{
		Node* prev = list->head;
		for(int i=0; i<index-1; i++)
		{
			prev = prev->next;
		}

		Node* node = prev->next;
		prev->next = node->next;
		free(node);
		list->size--;
	}
	return  true;
}

// 值删除
bool delete_value_list(List* list,TYPE data)
{
	if(data == list->head->data)
	{
		head_del_list(list);
		return true;
	}

	Node* prev = list->head;
	while(NULL!=prev->next)
	{
		if(data == prev->next->data)
		{
			Node* node = prev->next;
			prev->next = node->next;
			free(node);
			list->size--;
			return true;
		}
		prev = prev->next;
	}
	return false;
}


// 排序
void sort_list(List* list)
{
	for(Node* i=list->head; NULL != i->next; i=i->next)
	{
		for(Node* j=i->next; NULL != j; j=j->next)
		{
			if(i->data > j->data)
			{
				TYPE temp = i->data;
				i->data = j->data;
				j->data = temp;
			}
		}
	}
}

// 遍历
void show_list(List* list)
{
	for(Node* node=list->head; NULL!=node; node=node->next)
	{
		printf("%d ",node->data);
	}
	printf("\n");
}

int main()
{
//自己选择添加运行
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值