一、介绍
相比于数组,链表结构是离散的,容易操作,数组插入需要将整个后面的数据后移比较耗时
链表优点
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)
{
}
}
五、结果