C_单链表的使用(增、删、改、查)

一、链表节点的创建和访问

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.打印结果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用结构体来实现单链表,每个结构体包含一个数据域和一个指向下一个结构体的指针。具体实现如下: 1. 加节点: 首先定义一个结构体: struct Node { int data; struct Node* next; }; 然后定义一个头节点: struct Node* head = NULL; 接着,可以编写一个函数来加节点: void addNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct Node* current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } 2. 除节点: 可以编写一个函数来除节点: void deleteNode(int data) { struct Node* current = head; struct Node* previous = NULL; while (current != NULL && current->data != data) { previous = current; current = current->next; } if (current == NULL) { return; } if (previous == NULL) { head = current->next; } else { previous->next = current->next; } free(current); } 3. 修节点: 可以编写一个函数来修节点: void modifyNode(int oldData, int newData) { struct Node* current = head; while (current != NULL && current->data != oldData) { current = current->next; } if (current == NULL) { return; } current->data = newData; } 4. 找节点: 可以编写一个函数来找节点: void searchNode(int data) { struct Node* current = head; while (current != NULL && current->data != data) { current = current->next; } if (current == NULL) { printf("节点不存在\n"); } else { printf("节点存在,数据为:%d\n", current->data); } } 以上就是用 C 语言编写单链表增删改查的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值