#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int id;
int age;
char name[20];
} nodes;
typedef struct Linknode
{
void* value;
struct Linknode *next;
}Linknodes;
typedef struct LinkList
{
Linknodes *p_head;
int length;
}LinkLists;
typedef void* LList;//向用户封装数据
LList LinkList_Init();
void Insert_Linklist(LList p_node, LList value_in);
void Insert_pos_Linklist(LList p_node, int pos, LList value_in);
void Foreach_LinkList(LList p_node, void(*my_printf)(void*));
void my_printf1(void* node_in);
void Remove_pos_LinkList(LList p_node, int pos);
void Remove_value_LinkList(LList p_node, void* value, int(*my_compare)(void*, void*));
int my_compare1(void* value_in, void* node_in);
void Length_LinkList(LList p_node);
void Clear_LinkList(LList p_node);
void Destory_LinkList(LList p_node);
int main() {
printf("---开始创建链表---\n");
LList Link_head = LinkList_Init();
printf("---链表创建完毕---\n");
nodes n1 = {1, 21, "花木兰"};
nodes n2 = {2, 21, "凯皇"};
nodes n3 = {3, 21, "百里守约"};
nodes n4 = {4, 19, "百里玄策"};
nodes n5 = {5, 22, "苏烈"};
nodes n6 = {6, 20, "李信"};
printf("---开始后插数据--\n");
Insert_Linklist(Link_head, &n1);
Insert_Linklist(Link_head, &n2);
Insert_Linklist(Link_head, &n3);
Insert_Linklist(Link_head, &n4);
Insert_Linklist(Link_head, &n5);
printf("---后插数据完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
Length_LinkList(Link_head);
printf("---开始在特定位置插入数据--\n");
Insert_pos_Linklist(Link_head, 1, &n6);
printf("---在特定位置插入数据完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
Length_LinkList(Link_head);
printf("---开始在特定位置删除数据--\n");
Remove_pos_LinkList(Link_head, 0);
printf("---在特定位置删除数据完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
Length_LinkList(Link_head);
printf("---开始在特定数值删除数据---\n");
int id_temp = 3;
Remove_value_LinkList(Link_head, &id_temp, my_compare1);
printf("---在特定数值删除数据完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
Length_LinkList(Link_head);
printf("---开始清空链表---\n");
Clear_LinkList(Link_head);
printf("---清空链表完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
Length_LinkList(Link_head);
printf("---开始销毁链表---\n");
Destory_LinkList(Link_head);
Link_head = NULL;
printf("---销毁链表完毕---\n");
Foreach_LinkList(Link_head, my_printf1);
system("pause");
return 0;
}
LList LinkList_Init() {
LinkLists *Link_head = (LinkLists*)malloc(sizeof(LinkLists));
Link_head -> p_head = (Linknodes *)malloc(sizeof(Linknodes));
Link_head -> p_head -> value = NULL;
Link_head -> p_head -> next = NULL;
Link_head -> length = 0;
return Link_head;
}
void Insert_Linklist(LList p_node, LList value_in) {
if(p_node == NULL)
return;
LinkLists *node_head = (LinkLists *)p_node;
Linknodes *p_current = node_head -> p_head;
while(p_current -> next != NULL) {
p_current = p_current -> next;
}
Linknodes *new_node = (Linknodes*)malloc(sizeof(Linknodes));
new_node -> value = value_in;
new_node -> next = NULL;
p_current -> next = new_node;
node_head -> length ++;
}
void Insert_pos_Linklist(LList p_node, int pos, LList value_in) {
if(p_node == NULL)
return;
LinkLists *node_head = (LinkLists *)p_node;
if(pos < 0 || pos >= node_head -> length) {
Insert_Linklist(p_node, value_in);
return;
}
Linknodes *new_node = (Linknodes*)malloc(sizeof(Linknodes));
new_node -> value = value_in;
new_node -> next = NULL;
Linknodes *p_current = node_head -> p_head -> next;
Linknodes *p_privious = node_head -> p_head;
int temp = 0;
while(p_current != NULL) {
if (temp == pos) {
p_privious -> next = new_node;
new_node -> next = p_current;
break;
} else {
p_privious = p_current;
p_current = p_current -> next;
}
temp ++;
}
node_head -> length ++;
}
void Foreach_LinkList(LList p_node, void(*my_printf)(void*)) {
if(p_node == NULL)
return;
LinkLists *node_head = (LinkLists *)p_node;
Linknodes *p_currut = node_head -> p_head -> next;
while(p_currut != NULL) {
my_printf(p_currut -> value);
p_currut = p_currut -> next;
}
}
void my_printf1(void* node_in) {
if(node_in == NULL)
return;
nodes* s_node = (nodes* )node_in;
printf("ID:%d 姓名:%s, 年龄:%d\n", s_node -> id, s_node -> name, s_node -> age);
}
void Remove_pos_LinkList(LList p_node, int pos) {
if(p_node == NULL)
return;
LinkLists *node_head= (LinkLists *)p_node;
Linknodes *p_current = node_head -> p_head -> next;
Linknodes *p_privious = node_head -> p_head;
int temp = 0;
if(pos < 0 || pos >= node_head -> length)
return;
while(p_current != NULL) {
if(pos == temp) {
p_privious -> next = p_current -> next;
free(p_current);
node_head -> length --;
break;
} else {
p_privious = p_current;
p_current = p_current -> next;
}
}
}
void Remove_value_LinkList(LList p_node, void* value, int(*my_compare)(void*, void*)) {
if(p_node == NULL)
return;
LinkLists *node_head= (LinkLists *)p_node;
Linknodes *p_current = node_head -> p_head -> next;
Linknodes *p_privious = node_head -> p_head;
while(p_current != NULL) {
if(my_compare(value, p_current -> value)) {
p_privious -> next = p_current -> next;
free(p_current);
node_head -> length --;
break;
} else {
p_privious = p_current;
p_current = p_current -> next;
}
}
}
int my_compare1(void *value_in, void *node_in) {
int *p1 = (int *)value_in;
nodes *p2 = (nodes* )node_in;
if(*p1 == p2 -> id)
return 1;
else
return 0;
}
void Length_LinkList(void *p_node) {
if(p_node == NULL)
return;
LinkLists *node_head = (LinkLists *)p_node;
int length = node_head -> length;
printf("链表的长度为%d\n", length);
}
void Clear_LinkList(void *p_node) {
if(p_node == NULL)
return;
LinkLists *node_head = (LinkLists *)p_node;
Linknodes *p_current = node_head ->p_head -> next;
while(p_current != NULL) {
Linknodes *p_save = p_current -> next;
free(p_current);
p_current = p_save;
}
node_head -> p_head -> next = NULL;
node_head -> length = 0;
}
void Destory_LinkList(void *p_node) {
if(p_node == NULL)
return;
Clear_LinkList(p_node);
LinkLists *node_head = (LinkLists *)p_node;
free(node_head -> p_head);
free(node_head);
p_node = NULL;
}
C语言单向链表的数据结构实现——创建、插入、删除、销毁
最新推荐文章于 2021-05-12 23:31:04 发布