#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node * next;
};
typedef struct node nodes;
nodes *LinkList_Init();
void Insert_value(int value_in, nodes *P_node);
void Insert_pos_value(int value_in, int pos, nodes *p_node);
void Foreach_Linklist(nodes * p_node);
void Remove_LinkList(int pos, nodes *p_node);
void Remove_value_LinkList(int value_in, nodes *p_node);
void Clear_LinkList(nodes *p_node);
void Destory_LinkList(nodes *p_node);
int main() {
printf("---开始创建链表---\n");
nodes* p_head = LinkList_Init();
printf("---链表创建完毕---\n");
printf("---开始后插数据--\n");
Insert_value(1, p_head);
Insert_value(2, p_head);
Insert_value(3, p_head);
Insert_value(4, p_head);
Insert_value(5, p_head);
Insert_value(6, p_head);
printf("---后插数据完毕---\n");
Foreach_Linklist(p_head);
printf("---开始在特定位置插入数据--\n");
Insert_pos_value(7, 1, p_head);
printf("---在特定位置插入数据完毕---\n");
Foreach_Linklist(p_head);
printf("---开始在特定位置删除数据--\n");
Remove_LinkList(0, p_head);
printf("---在特定位置删除数据完毕---\n");
Foreach_Linklist(p_head);
printf("---开始在特定数值删除数据---\n");
Remove_value_LinkList(6, p_head);
printf("---在特定数值删除数据完毕---\n");
Foreach_Linklist(p_head);
printf("---开始清空链表---\n");
Clear_LinkList(p_head);
printf("---清空链表完毕---\n");
Foreach_Linklist(p_head);
printf("---开始销毁链表---\n");
Destory_LinkList(p_head);
p_head = NULL;
printf("---销毁链表完毕---\n");
Foreach_Linklist(p_head);
system("pause");
return 0;
}
//链表初始化
nodes *LinkList_Init() {
nodes *p_node = (nodes*)malloc(sizeof(nodes));
p_node -> next = NULL;
return p_node;
}
//往后插入数据
void Insert_value(int value_in, nodes *P_node) {
if(P_node == NULL)
return;
nodes *p_current = P_node;
nodes *new_node = (nodes*)malloc(sizeof(nodes));
new_node -> value = value_in;
new_node -> next = NULL;
while(p_current -> next != NULL) {
p_current = p_current -> next;
}
p_current -> next = new_node;
}
//往指定位置插入数据
void Insert_pos_value(int value_in, int pos, nodes *p_node) {
if(p_node == NULL)
return;
nodes *p_current = p_node -> next;
nodes *p_privious = p_node;
int temp = 0;
while(p_current != NULL){
if(temp == pos) {
nodes *new_node = (nodes *)malloc(sizeof(nodes));
new_node -> value = value_in;
p_privious -> next = new_node;
new_node -> next = p_current;
break;
} else {
p_privious = p_current;
p_current = p_current -> next;
temp ++;
}
}
}
//遍历链表
void Foreach_Linklist(nodes * p_node) {
if(p_node == NULL)
return;
nodes * p_current = p_node -> next;
while(p_current != NULL) {
printf("%d\n", p_current -> value);
p_current = p_current -> next;
}
}
//删除指定位置的元素 零为起始位置
void Remove_LinkList(int pos, nodes *p_node) {
if(p_node == NULL)
return;
int temp = 0;
nodes *p_current = p_node -> next;
nodes *p_privious = p_node;
while(p_current != NULL) {
if(temp == pos) {
p_privious -> next = p_current -> next;
free(p_current);
p_current = NULL;
} else {
p_privious = p_current;
p_current = p_current -> next;
temp ++;
}
}
}
//删除特定值的元素
void Remove_value_LinkList(int value_in, nodes *p_node) {
if(p_node == NULL)
return;
nodes *p_current = p_node -> next;
nodes *p_privious = p_node;
while(p_current != NULL) {
if(p_current -> value == value_in) {
p_privious -> next = p_current -> next;
free(p_current);
p_current = NULL;
} else {
p_privious = p_current;
p_current = p_current -> next;
}
}
}
//清空链表
void Clear_LinkList(nodes *p_node) {
if (p_node == NULL) {
return;
}
nodes *p_current = p_node -> next;
while(p_current != NULL) {
nodes *p_save = p_current;
p_current = p_current -> next;
free(p_save);
p_save = NULL;
}
p_node -> next = NULL;
}
//销毁链表
void Destory_LinkList(nodes *p_node) {
if (p_node == NULL) {
return;
}
Clear_LinkList(p_node);
free(p_node);
p_node = NULL;
}
C语言创建链表的基本操作——建立、插入、删除、清空、销毁
最新推荐文章于 2024-02-14 11:27:13 发布