C语言,链表:_交换两节点_释放链表_新建节点_插入节点_遍历链表_查找节点_修改节点_删除节点_排序节点_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN 6

typedef struct mylist
{
	int data;
	struct mylist* next;
}mylist;


/*
函数名:create_node
函数功能:新建节点
返回值:struct *
参数:int data
*/
struct mylist* create_node(int data);


/*
函数名:insert_head
函数功能:头插法插入节点
返回值:void
参数:struct mylist *head,struct mylist *node
*/
void insert_head(struct mylist* head, struct mylist* node);


/*
函数名:insert_tail
函数功能:尾插法插入节点
返回值:void
参数:struct mylist *head,struct mylist *node
*/
void insert_tail(struct mylist* head, struct mylist* node);


/*
函数名:show_list
函数功能:遍历链表
返回值:void
参数:struct mylist *head
*/
void show_list(struct mylist* head);


/*
函数名:find_list
函数功能:用数据查找节点
返回值:struct mylist *
参数:struct mylist *head,const int data
*/
struct mylist* find_list(struct mylist* head, const int data);


/*
函数名:change_list
函数功能:修改节点信息
返回值:int
参数:struct mylist *head,const int data,int data_new
*/
int  change_list(struct mylist* head, const int data, int data_new);


/*
函数名:sort_list
函数功能:按data对链表排序
返回值:void
参数:struct mylist *head
*/
void sort_list(struct mylist* head);


/*
函数名:delete_list
函数功能:删除指定节点
返回值:void
参数:struct mylist *head,int del_data
*/
void delete_list(struct mylist* head, int del_data);


/*
函数名:exchange_list
函数功能: 根据数据交换节点
返回值:void
参数:struct mylist* head, int data1, int data2
*/
void exchange_list(struct mylist* head, int data1, int data2);


/*
函数名: free_list
函数功能: 释放链表空间
返回值:void
参数:struct mylist* head
*/
void free_list(struct mylist* head);


int main(int argc, char** argv)
{
	int i;
	//int data;//用于查找
	//int data_new;//用于更新
	struct mylist* node = NULL;//接收新建节点
	struct mylist* head = create_node(-1);//头节点数据域无所谓

	//简单起见,用循环创造含有LEN个节点的链表
	for (i = 0; i < LEN; i++)
	{
		node = create_node(i+1);
		//insert_head(head, node);
		insert_tail(head, node);
	}

	//printf("请输入要删除的数据\n");
	//scanf("%d", &data);
	//delete_list(head, data);
	//sort_list(head);
	exchange_list(head, 1,3);
	//sort_list(head);	
	show_list(head);
	free_list(head);
	return 0;
}


/*
函数功能:新建节点
返回值:struct *node
参数:int data
*/
struct mylist* create_node(int data)
{
	//申请空间
	struct mylist* node = (struct mylist*)malloc(sizeof(struct mylist));
	if (node == NULL)
	{
		printf("新建失败\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return NULL;
	}
	//初始化空间
	memset(node, 0, sizeof(struct mylist));
	node->data = data;
	//返回
	return node;
}


/*
函数名:insert_head
函数功能:头插法插入节点
返回值:void
参数:struct mylist *head,struct mylist *node
*/
void insert_head(struct mylist* head, struct mylist* node)
{
	if (head == NULL || node == NULL)
	{
		printf("插入失败\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	node->next = head->next;
	head->next = node;
}


/*
函数名:insert_tail
函数功能:尾插法插入节点
返回值:void
参数:struct mylist *head,struct mylist *node
*/
void insert_tail (struct mylist* head, struct mylist* node)
{
	if (head == NULL || node == NULL)
	{
		printf("插入失败\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	struct mylist* p = head;
	while (p ->next!= NULL)
	{
		p = p->next;
	}
	p->next= node;
	node->next = NULL;
}


/*
函数名:show_list
函数功能:遍历链表
返回值:void
参数:struct mylist *head
*/
void show_list(struct mylist* head)
{
	if (head == NULL)
	{
		printf("无法遍历\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	printf("链表中所有数据如下:\n");
	struct mylist* p = head->next;
	while (p != NULL)
	{
		printf("%d\n", p->data);
		p = p->next;
	}
}


/*
函数名:find_list
函数功能:用数据查找节点
返回值:struct mylist *
参数:struct mylist *head,const int data
*/
struct mylist* find_list(struct mylist* head, const int data)
{
	if (head == NULL)
	{
		printf("无法查找\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return NULL;
	}
	struct mylist* p = head->next;
	while (p != NULL)
	{
		if (p->data == data)
		{
			break;
		}
		p = p->next;
	}
	return p;
}


/*
函数名:change_list
函数功能:修改节点信息
返回值:int
参数:struct mylist *head,const int data,int data_new
*/
int  change_list(struct mylist* head, const int data, int data_new)
{
	if (head == NULL)
	{
		printf("无法查找\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return -1;
	}
	struct mylist* node = find_list(head, data);
	if(node==NULL)
	{
		printf("未找到指定修改的数据\n");
		return -1;
	}
	else
	{
		node->data = data_new;
		return 0;
	}
	return -1;
}


/*
函数名:sort_list
函数功能:按data对链表排序
返回值:void 
参数:struct mylist *head
*/
void sort_list(struct mylist* head)
{
	if (head == NULL)
	{
		printf("无法排序\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	struct mylist *p = head->next;
	struct mylist *q = NULL;
	while (p!=NULL)
	{
		q = p->next;
		while (q != NULL)
		{
			if (p->data < q->data)
			{
				int temp= p->data;
				p->data = q->data;
				q->data = temp;
			}
			q = q->next;
		}
		p = p->next;
	}
}


/*
函数名:delete_list
函数功能:删除指定节点
返回值:void
参数:struct mylist *head,int del_data
*/
void delete_list(struct mylist* head, int del_data)
{
	if (head == NULL)
	{
		printf("无法删除\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	struct mylist* p = head->next;
	struct mylist* pr = head;
	while (p != NULL)
	{
		if (p->data == del_data)
		{
			pr->next = p->next;
			free(p);
			p = pr;
		}
		pr = p;
		p = p->next;
	}
}


/*
函数名:exchange_list
函数功能: 根据数据交换节点
返回值:void
参数:struct mylist* head, int data1, int data2
*/
void exchange_list(struct mylist* head, int data1, int data2)
{
	if (head == NULL)
	{
		printf("无法交换\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	struct mylist* p = head->next;
	struct mylist* pr = head;
	struct mylist* q = head->next;
	struct mylist* qr = head;
	//找第一个数
	while (p != NULL)
	{
		if (p->data == data1)
		{
			break;
		}
		p = p->next;
		pr = pr->next;
	}
	//找第二个数
	while (q != NULL)
	{
		if (q->data == data2)
		{
			break;
		}
		q = q->next;
		qr = qr->next;
	}
	if (q == NULL||p==NULL)
	{
		printf("未找到可交换的数据\n");
		return;
	}
//交换开始
	struct mylist* temp = q->next;
	//先排除两个节点相邻情况
	if (p == qr)
	{
		pr->next = q;
		q->next = p;
		p->next = temp;
		return;
	}
	else if (q == pr)
	{
		temp = p->next;
		qr->next = p;
		p->next = q;
		q->next = temp;
		return;
	}
	//一般情况时的交换
	pr->next = q;
	q->next = p->next;
	qr->next = p;
	p->next = temp;
}


/*
函数名: free_list
函数功能: 释放链表空间
返回值:void
参数:struct mylist* head
*/
void free_list(struct mylist* head)
{
	if (head == NULL)
	{
		printf("释放失败\n%s %d %s\n", __FILE__, __LINE__, __func__);
		return;
	}
	while (head != NULL)
	{
		struct mylist* p = head;
		head = head->next;
		free(p);
		p = NULL;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值