一、链表节点的创建和访问
1.构建一个链表节点结构体
// 构建一个链表的节点
struct node
{
int data; // 有效数据
struct node *pNext; // 指向下一个节点的指针
};
2.定义头指针用于对链表节点的访问和修改
struct node *pHeader = NULL;// 定义头指针
3.创建一个新的节点与前一个节点关联起来
struct node *p = (struct node *)malloc(sizeof(struct node));
if (NULL == p)
{
return -1;
}
// 清理申请到的堆内存
memset(p, 0,sizeof(struct node));
// 填充节点
p->data = 1; // 修改数据
p->pNext = NULL;// 将来要指向下一个节点的首地址
// 实际操作时将下一个节点malloc返回的指针赋值给这个
pHeader = p; // 将本节点和它前面的头指针关联起来
4.注意事项
创建链表节点时必须将上一个节点的指针与本次创建的节点关联起来,由上一个节点指向下一个节点,构成一条链。
二、实例
1.创建节点
struct node *creat_node(int data)
{
struct node *p =(struct node *)malloc(sizeof(struct node));//申请堆内存
if(NULL == p)
{
printf("mallocd err\r\n");
return NULL;
}
memset(p,0,sizeof(struct node));//清除数据
p->data =data;//赋值
p->pNext=NULL;
return p;
}
2.尾部插入节点
void insert_tail(struct node* pH,struct node * new_data)
{
struct node * p =pH;
int cnt = 0;
while(NULL != p->pNext)//第一步,先找到链表中最后一个节点
{
p=p->pNext; //往后走一个节点
cnt ++;
}
pH->data = cnt+1;
p->pNext = new_data; //第二步,将新节点插入到最后一个节点尾部
}
void insert_node_tail(struct node* pH,int data)
{
int cnt = 0;
struct node * p =pH;
struct node *p_insert =(struct node *)malloc(sizeof(struct node));
if(NULL == p_insert)
{
return ;
}
memset(p_insert,0,sizeof(struct node));
p_insert->data = data;
p_insert->pNext = NULL;
while(NULL != p->pNext)
{
p=p->pNext;
cnt ++;
}
pH->data = cnt+1;
p->pNext = p_insert;
}
3.头部插入
void insert_head(struct node* pH,struct node * new_data)
{
new_data->pNext=pH->pNext;// 第1步: 新节点的next指向原来的第一个节点
pH->pNext = new_data; // 第2步: 头节点的next指向新节点的地址
pH->data +=1; // 第3步: 头节点中的计数要加1
}
void insert_node_head(struct node* pH,int data)
{
struct node *p_insert =(struct node *)malloc(sizeof(struct node));
if(NULL == p_insert)
{
return ;
}
memset(p_insert,0,sizeof(struct node));
p_insert->data = data;
p_insert->pNext = NULL;
p_insert->pNext=pH->pNext;
pH->pNext = p_insert;
pH->data +=1;
}
4.遍历链表
void loop_list(struct node* pH)
{
struct node * p =pH;
printf("开始遍历\r\n");
while(NULL != p->pNext)
{
p = p->pNext;
printf("node_data: %d\r\n",p->data);
}
if(pH->pNext == NULL)//头节点的下一个节点为NULL 则当前链表为空
printf("loop node is void\r\n");
printf("遍历结束\r\n");
}
5.删除节点
int delete_node(struct node *pH,int data)
{
struct node * Prev=NULL;
struct node * p = pH;
while(NULL != p->pNext)
{
Prev = p; //保存当前节点
p=p->pNext; //取当前节点的下一个节点判断
if(p->data == data) //找到了要删除的节点
{
if(p->pNext == NULL) //判断当前节点是最后一个节点
{
Prev->pNext=NULL; //原来尾节点的前一个节点变成新尾节点
free(p); //释放原来的尾节点的内存
}
else //判断当前节点不是最后一个节点
{
Prev->pNext=p->pNext;//要删除的节点的前一个节点和它的后一个节点相连
free(p); //释放原来的尾节点的内存
}
pH->data -=1; //删除统计的节点个数
return 0;
}
}
printf("没有找到这个点\r\n");
return -1;
}
int delete_node_all(struct node *head)
{
struct node *p;
if(head==NULL)
return 0;
while(head)
{
p=head->pNext;
free(head);
head=p;
}
return 1;
}
6.举例
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * pNext;
};
int main(void)
{
struct node * pHead = creat_node(0);//创建头指针
insert_tail(pHead,creat_node(1));
insert_tail(pHead,creat_node(2));
insert_tail(pHead,creat_node(3));
loop_list(pHead);
printf("\r\n");
delete_node(pHead,1);
delete_node(pHead,2);
delete_node(pHead,3);
loop_list(pHead);
printf("\r\n");
insert_head(pHead,creat_node(1));
insert_head(pHead,creat_node(2));
insert_head(pHead,creat_node(3));
loop_list(pHead);
printf("\r\n");
return 0;
}
7.打印结果