掌握结构体,指针后,链表作为两种形式的集合,将C语言的作用发挥到巨大。
链表知识123
链表是线性表,包括两个部分:数据域&指针域
数据域:存储需要保存的数据
指针域:各个节点之间的连接
连续性:链表在逻辑上是连续的,但物理上未必连续
链表主要有单向链表,双向链表,循环链表
链表操作:对于链表的操作一般包括增加,删除,修改,查找
下面对单向链表进行举例操作:
操作环境:keil v4,串口软件,有硬件支持
构建结构体:
typedef struct list {
int id; //元素id,方便数据查找
char data[20]; //数据域
struct list *next; //指向下一个链表头指针
}LIST;
//定义全局变量,实现每个元素的id不同
static LIST *list_head = NULL;
//定义链表头结点:
static int list_id = 0;
/*
*功能:增加链表元素
*输入:**head -> 链表头
* list -> 增加的链表
*输出:无
**/
static void List_Add(LIST **head, LIST *list)
{
LIST *temp; //临时变量,指向*head
if(NULL == *head) //如果链表头为空,则将list保存到list_head
{
*head = list;
(*head)->next = NULL;
}
else
{
temp = *head; //temp保存*head
while(temp) //轮询
{
if(NULL == temp->next) //尾插
{
temp->next = list;
list->next = NULL; //next清空,供下一个元素尾插
}
temp = temp->next;
}
}
}
/*
*功能:删除指定id位置的元素
*输入:**head -> 链表头
* id -> 指点id
*输出:0 -> 成功
* -1 -> 失败
**/
static int List_Del(LIST **head, int id)
{
LIST *temp, *p;
temp = *head;
if(NULL == temp) //链表为空
{
printf("list is empty \r\n");
return -1;
}
else
{
if(id == temp->id) //删除链表头
{
*head = temp->next;
return 0;
}
else
{
while(temp->next) //轮询
{
p = temp;
temp = temp->next;
if(id == temp->id)
{
p->next = temp->next;
return 0;
}
}
return -1;
}
}
}
/*
*功能:修改指定id位置的元素
*输入:**head -> 链表头
* id -> 指点id
* *content -> 修改内容
*输出:0 -> 成功
* -1 -> 失败
**/
static int List_Chg(LIST **head, int id, char *content)
{
LIST *temp;
temp = *head; //temp保存*head
while(temp) //轮询
{
if(id == temp->id)
{
memset(temp->data, 0, sizeof(temp->data));
sprintf(temp->data, "%s", content);
temp->data[strlen(content)] = '\0';
return 0;
}
temp = temp->next;
}
return -1;
}
/*
*功能:寻找指定id位置的元素
*输入:**head -> 链表头
* id -> 指点id
*输出:0 -> 成功
* -1 -> 失败
**/
static int List_Query(LIST **head, int id)
{
LIST *temp;
temp = *head;
if(NULL == temp) //链表为空
{
printf("list is empty\r\n");
return -1;
}
else
{
while(temp) //轮询
{
if(id == temp->id) //找到指定元素
{
printf("list %d : %s\r\n", temp->id, temp->data);
return 0;
}
temp = temp->next;
}
printf("no finding\r\n");
return -1;
}
}
/*
*功能:打印链表
*输入:**head -> 链表头
*输出:无
**/
static void List_Printf(LIST **head)
{
LIST *temp;
temp = *head;
printf("list information\r\n");
while(temp) //轮询
{
printf("list %d : %s\r\n", temp->id, temp->data);
temp = temp->next;
}
}
int main(void)
{
int i;
LIST *lists = NULL;
LIST temp_list;
lists = malloc(sizeof(LIST)*10);
/*******************************单向链表操作************************************/
//执行一:循环加入10个链表元素
for(i = 0; i < 10; i++)
{
lists[i].id = list_id++; //id循环赋值
sprintf(lists[i].data, "TECH-PRO - %d", i); //data循环赋值
List_Add(&list_head, &lists[i]); //将数据加入链表
}
List_Printf(&list_head); //打印链表数据
//执行二:增加一个结构体到链表
temp_list.id = list_id++; //id赋值
sprintf(temp_list.data, "temp_list"); //data赋值
List_Add(&list_head, &temp_list); //将结构体数据加入链表中
List_Printf(&list_head); //打印链表数据
//执行三:删除指定元素
List_Del(&list_head, 0); //删除指定元素链表
List_Del(&list_head, 2); //删除指定元素链表
List_Del(&list_head, 5); //删除指定元素链表
List_Del(&list_head, 9); //删除指定元素链表
List_Printf(&list_head); //打印链表数据
//执行四:改变指定元素数据
List_Chg(&list_head, 4, "bug"); //改变id为4的元素数据
List_Printf(&list_head); //打印链表数据
//执行五:寻找指定元素
List_Query(&list_head, 3); //寻找id为3的元素数据
List_Printf(&list_head); //打印链表数据
/*****************************************************************************/
return 0;
}
执行每一步功能操作,可单独处理
执行一效果:
执行二效果:
执行三效果:
执行四效果:
执行五效果: