C数据结构与算法-链表

一、介绍
相比于数组,链表结构是离散的,容易操作,数组插入需要将整个后面的数据后移比较耗时
在这里插入图片描述

链表优点
n个节点离散分配
每一个节点之间通过指针相连
每一个节点有一个前驱节点和一个后继节点
首节点没有前驱节点,尾节点没有后继节点

注意:链表使用必须要支持malloc等动态内存分配函数

二、链表的定义

方式1
struct link{
       int data;          //定义数据域
       struct link *next; //定义指针域,存储直接后继的节点信息
};
方式2
typedef struct link{
       int data;          //定义数据域
       struct link *next; //定义指针域,存储直接后继的节点信息
}list_def;

简单理解就是结构体里面多了一个指向自身结构体类型的指针

三、链表操作
1、创建

//创建n个节点的链表 
list_def *list_create(int n)
{
	int i; 
	list_def *head,*node,*end;
	
	printf("创建链表:");
	if(n <= 0)
		return 0;
	else 
	{		
		node = (list_def *)malloc(sizeof(list_def));
		node->next = NULL; 
		printf("输入:");
		scanf("%d",&node->data); 
		head = node;
		end = node;
		for(i = 0; i < (n-1); i++)
		{
			end = (list_def *)malloc(sizeof(list_def));
			end->next = NULL;
			node->next = end;
			node = end;
			printf("输入:");
			scanf("%d", &node->data); 
		}
	}
	
	return head;
}

2、修改节点内容

//修改节点内容 
int chage_value(list_def *head, int n)
{
	int i = 0;
	list_def *node;
	printf("修改链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < n && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL)
	{
		printf("输入:");
		scanf("%d",&node->data);
	}
	else
	{printf("节点不存在\n");} 
	
	printf("修改完成\n");
	
	return 0;
}

3、插入节点
在这里插入图片描述

//插入节点 
int list_insert(list_def *head, int n)
{
 	int i = 0;
	list_def *node,*insert_node;
	printf("插入链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < n && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL)
	{
		insert_node = (list_def *)malloc(sizeof(list_def));
		printf("输入:");
		scanf("%d",&insert_node->data);
		insert_node->next = node->next;
		node->next = insert_node;
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

4、删除节点
在这里插入图片描述

//删除节点
int list_delet(list_def *head, int n)
{
	int i = 0;
	list_def *node,*delet_node;
	
	printf("删除链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < (n-1) && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL && node->next != NULL)
	{
		delet_node = node->next;
		node->next = delet_node->next;
		free(delet_node);
		printf("删除完成\n");
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

5、打印链表内容

//输出链表
int list_print(list_def *head)
{
	list_def *node,*delet_node;
	
	printf("打印链表 \n");
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(node != NULL)
	{
		printf("%d ",node->data);
		node = node->next;
	}

	return 0;
} 

6、查看节点

//查看节点内容 
int list_look(list_def *head, int n)
{
	int i = 0;
	list_def *node;
	
	printf("查看节点%d\n",n);
	
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < (n-1) && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL )
	{
		printf("节点值 %d\n",node->data);
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

四、示例代码

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

//定义链表 
typedef struct link{
       int data;          //定义数据域
       struct link *next; //定义指针域,存储直接后继的节点信息
}list_def;

//创建n个节点的链表 
list_def *list_create(int n)
{
	int i; 
	list_def *head,*node,*end;
	
	printf("创建链表:");
	if(n <= 0)
		return 0;
	else 
	{		
		node = (list_def *)malloc(sizeof(list_def));
		node->next = NULL; 
		printf("输入:");
		scanf("%d",&node->data); 
		head = node;
		end = node;
		for(i = 0; i < (n-1); i++)
		{
			end = (list_def *)malloc(sizeof(list_def));
			end->next = NULL;
			node->next = end;
			node = end;
			printf("输入:");
			scanf("%d", &node->data); 
		}
	}
	
	return head;
}

//修改节点内容 
int chage_value(list_def *head, int n)
{
	int i = 0;
	list_def *node;
	printf("修改链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < n && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL)
	{
		printf("输入:");
		scanf("%d",&node->data);
	}
	else
	{printf("节点不存在\n");} 
	
	printf("修改完成\n");
	
	return 0;
}

//插入节点 
int list_insert(list_def *head, int n)
{
 	int i = 0;
	list_def *node,*insert_node;
	printf("插入链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < n && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL)
	{
		insert_node = (list_def *)malloc(sizeof(list_def));
		printf("输入:");
		scanf("%d",&insert_node->data);
		insert_node->next = node->next;
		node->next = insert_node;
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

//删除节点
int list_delet(list_def *head, int n)
{
	int i = 0;
	list_def *node,*delet_node;
	
	printf("删除链表 节点%d\n",n);
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < (n-1) && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL && node->next != NULL)
	{
		delet_node = node->next;
		node->next = delet_node->next;
		free(delet_node);
		printf("删除完成\n");
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

//查看节点内容 
int list_look(list_def *head, int n)
{
	int i = 0;
	list_def *node;
	
	printf("查看节点%d\n",n);
	
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(i < (n-1) && node != NULL)
	{
		node = node->next;
		i++;
	}
	if(node != NULL )
	{
		printf("节点值 %d\n",node->data);
	}
	else
	{printf("节点不存在\n");} 
	
	return 0;
} 

//输出链表
int list_print(list_def *head)
{
	list_def *node,*delet_node;
	
	printf("打印链表 \n");
	if(head == NULL)
		return (-1);
		
	node = head;	
	while(node != NULL)
	{
		printf("%d ",node->data);
		node = node->next;
	}

	return 0;
} 

int main()
{
	list_def *list_head;
	
	list_head = list_create(10);
	list_print(list_head);
	chage_value(list_head, 3);
	list_print(list_head);
	list_insert(list_head, 5);
	list_print(list_head);
	list_delet(list_head, 6);
	list_print(list_head);
	list_look(list_head, 1);
	while(1)
	{
		
	}
}

五、结果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值