本文用一个相同的主函数和结构体来讲述链表的14种常见函数
主函数和结构体
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node* p_next;
}node;
typedef struct
{
node head;
node tail;
}link;
int main()
{
link lnk = { 0 };
link_init(&lnk);
link_add_head(&lnk, 20);
link_add_head(&lnk, 10);
link_append(&lnk, 90);
link_append(&lnk, 100);
link_insert(&lnk, 70);
link_insert(&lnk, 40);
link_insert(&lnk, 60);
link_insert(&lnk, 30);
link_insert(&lnk, 50);
link_insert(&lnk, 80);
link_remove_head(&lnk);
link_remove_tail(&lnk);
int val = 0;
link_get_head(&lnk, &val);
printf("最前面数字是%d\n", val);
link_get_tail(&lnk, &val);
printf("最后面数字是%d\n", val);
int size = link_size(&lnk);
for (int num = 0; num < size; num++)
{
link_get(&lnk, &val, num);
printf("%d ", val);
}
link_deinit(&lnk);
return 0;
}
链表的初始化函数
//链表的初始化函数
void link_init(link* p_link)
{
p_link->head.p_next = &(p_link->tail);
p_link->tail.p_next = NULL;
}
链表的清理函数
//链表的清理函数
void link_deinit(link* p_link)
{
while (p_link->head.p_next != &(p_link->tail))
{
node* p_first = &(p_link->head);
node* p_mid = p_first->p_next;
node* p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
}
统计有效数字个数的函数
//统计有效数字个数的函数
int link_size(const link* p_link)
{
int cnt = 0;
const node* p_node = NULL;
for (p_node = &(p_link->head); p_node != &(p_link->tail); p_node =p_node->p_next)
{
const node* p_first = p_node;
const node* p_mid = p_first->p_next;
const node* p_last = p_mid->p_next;
if (p_mid != &(p_link->tail))
{
cnt++;
}
}
return cnt;
// }
// return cnt-1;
}
判断链表是否为空的函数
//判断链表是否为空的函数
int link_empty(const link* p_link)
{
return p_link->head.p_next == &(p_link->tail);
}
判断链表是否为满的函数
//判断链表是否为满的函数
int link_full(const link* p_link)
{
return 0;
}
在最前面插入新节点的函数
//在最前面插入新节点的函数
int link_add_head(link* p_link, int num)
{
node* p_first = NULL, * p_mid = NULL, * p_last = NULL;
node* p_node = (node*)malloc(sizeof(node));
if (!p_node)
{
return 0;
}
p_node->num = num;
p_node->p_next = NULL;
p_first = &(p_link->head);
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_node;
p_node->p_next = p_mid;
return 1;
}
在最后加入新节点的函数
//在最后加入新节点的函数
int link_append(link* p_link, int num)
{
node* p_tmp = NULL;
node* p_node = (node*)malloc(sizeof(node));
if (!p_node)
{
return 0;
}
p_node->num = num;
p_node->p_next = NULL;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
node* p_first = p_tmp;
node* p_mid = p_first->p_next;
node* p_last = p_mid->p_next;
if (p_mid == &(p_link->tail))
{
p_first->p_next = p_node;
p_node->p_next = p_mid;
break;
}
}
return 1;
}
按照从小到大的顺序把新节点插入到链式物理结构中
//按照从小到大的顺序把新节点插入到链式物理结构中
int link_insert(link* p_link, int num) {
node* p_tmp = NULL;
node* p_node = (node*)malloc(sizeof(node));
if (!p_node) {
return 0;
}
p_node->num = num;
p_node->p_next = NULL;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
node* p_first = p_tmp;
node* p_mid = p_first->p_next;
node* p_last = p_mid->p_next;
if (p_mid == &(p_link->tail) || p_mid->num > p_node->num)
{
p_first->p_next = p_node;
p_node->p_next = p_mid;
break;
}
}
return 1;
}
删除第一个有效节点的函数
//删除第一个有效节点的函数
int link_remove_head(link* p_link)
{
node* p_first = NULL, * p_mid = NULL, * p_last = NULL;
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
p_first = &(p_link->head);
p_mid = p_first->p_next;
p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
删除最后一个有效节点的函数
//删除最后一个有效节点的函数
int link_remove_tail(link* p_link)
{
node* p_tmp = NULL;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
node* p_first = p_tmp;
node* p_mid = p_first->p_next;
node* p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
删除某个数字所在节点的函数
//删除某个数字所在节点的函数
int link_remove(link* p_link, int num)
{
node* p_tmp = NULL;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
node* p_first = p_tmp;
node* p_mid = p_first->p_next;
node* p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && p_mid->num == num)
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
获得第一个有效数字的函数
//获得第一个有效数字的函数
int link_get_head(const link* p_link, int* p_num)
{
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
const node* p_first = &(p_link->head);
const node* p_mid = p_first->p_next;
const node* p_last = p_mid->p_next;
*p_num = p_mid->num;
return 1;
}
获得最后一个有效数字的函数
//获得最后一个有效数字的函数
int link_get_tail(const link* p_link, int* p_num)
{
const node* p_tmp = NULL;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
const node* p_first = p_tmp;
const node* p_mid = p_first->p_next;
const node* p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
*p_num = p_mid->num;
return 1;
}
}
return 0;
}
获得编号对应数字的函数
//获得编号对应数字的函数
int link_get(const link* p_link, int* p_num, int num)
{
const node* p_tmp = NULL;
int cnt = 0;
for (p_tmp = &(p_link->head); p_tmp != &(p_link->tail); p_tmp = p_tmp->p_next)
{
const node* p_first = p_tmp;
const node* p_mid = p_first->p_next;
const node* p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && cnt == num)
{
*p_num = p_mid->num;
return 1;
}
cnt++;
}
return 0;
}
标签:node,tmp,常用,函数,mid,next,链表,link,first
From: https://blog.csdn.net/A_hard_August/article/details/141427163