论:单链表

@醉里挑灯看剑,梦回吹角连营

前言:本篇总结对于单链表的基本操作,创建、插入、删除、索引。用C语言实现,加深对于单链表的使用

定义链表结构

//本文链表存储int型数据为例
typedef struct Signal_Node {
	int data;
	struct Signal_Node *next;
}Node;

节点的建立

通过malloc函数在堆中开辟节点空间,用来存放数据及指向下一个节点空间的结构体指针变量,结构如下:
单链表节点的建立

头节点的建立

建立头节点,必须先定义一个指向头节点的结构体指针变量Node *hp = NULL,便于之后对链表进行操作
单链表头节点的建立

插入新的节点

从链表头插入节点

先生成新的节点空间 Node *newnd,作为要插入的新节点元素
从链表头插入新节点

从链表尾部插入

与从头节点插入一样,只是指向下一个节点的指针为空
1、遍历链表,直到 p -> next = NULL   // p为最后一个节点指针
2、p -> next = newnd;
3、newnd -> next = NULL;        //已插入节点

从指定位置插入

从指定位置插入节点
1、遍历链表,找到要插入的链表位置p,if(p -> data == index) return p;
2、newnd -> next = p -> next -> next;
3、p -> next = newnd;

删除节点分析

  删除节点的过程,即与插入节点的过程相反。需要注意的是,删除某个节点,必须知道上一个节点的指针,即上一个节点中存放将要删除节点中的指针。每删除一个节点,需要将该节点空间赋空NULL,并且释放该空间free()

找到删除节点的上一个节点指针 *p

1、int temp_data = p -> next -> data ,临时存放被删除节点中的数据
2、遍历链表直到 temp_data == index,对比索引数据,找到上一个节点指针 p
3、返回 p

删除节点 p -> next

1、Node *temp = p -> next // 将需要删除的节点指针暂存,供之后释放该空间所用
2、p -> next = p -> next -> next   // 删除该节点 p -> next
3、temp = NULL && free(temp) // 将该节点空间赋空,并且释放该空间

代码实现

  本代码使用基本数据类型int,从main函数开始,一步步实现链表的建立,链表中节点的插入,以及向节点中输入数据,最后一个个删除释放节点空间在等操作,方便加深对链表创建及操作的理解。完整代码如下:

/**
* @file Signal_node_base.c
* @author mahuiming, 807741077@qq.com
* @date 2019-04-01
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LINK_NODE_NUM 5

typedef struct SignalNode {

    int data;
    struct SignalNode *next;
}Node;

/* 创建节点 */
Node *Sig_CreateNode(int data, int dataSize) {

    Node *p = (Node *)malloc(sizeof(Node));
    static int i = 0;
    if(NULL == p)
        printf("开辟节点空间失败!\n");
    p -> next = NULL;
    p -> data = data;
    printf("成功创建节点 %d\n",i);
    i++;
    return p;
}
/* 插入一个新的节点 */
void InsertNode(Node *p, Node *newnd) {

    newnd -> next = p -> next;
    p -> next = newnd;
}
/* 将节点从链表头部插入 */
void HeadInsertNode(Node *hp, Node *newnd) {

    InsertNode(hp,newnd);
}
/* 将节点从链表尾部插入 */
void TailInsertNode(Node *hp, Node *newnd) {

    Node *p = hp; 
    while(1) {

        if(p -> next == NULL){
            InsertNode(p, newnd);
            break;
        }
        p = p -> next;
    }   
}
/* 创建一个链表 */
void createList(Node **hpp) {

    //创建一个数据为int类型的头节点,传入参数0 和 sizeof(int)
    *hpp = Sig_CreateNode(0, 4);
    (*hpp) -> next = NULL;
}
/* 向链表中存值,建立链表 */
void setList(Node *hp, int data) {

        Node *newnd = Sig_CreateNode(data, sizeof(data));
    //  HeadInsertNode(hp, newnd);     
        TailInsertNode(hp, newnd);
}
/* 打印链表中的数据 */
void ShowList(Node *hp) {

    Node *p = hp -> next;
    if(NULL == p)
        printf("链表为空\n");
    else {
        while(1) {
            if(NULL == p)
                break;
            printf("链表中的数据为:%d \n",p -> data);
            p = p -> next;
        }
    }
}
/* 找到被删除节点的上一个节点 */
Node *Find_Delnode_Prev(Node *hp, int index) {

    Node *p = hp;
    static int temp_data = 0;
    while(1) {
        temp_data = p -> next -> data;
        if(temp_data == index) {
            printf("将要删除数据为 %d 的节点...\n",temp_data);
            printf("\n");
            break;
        }
        p = p -> next;
    }
    return p;
}
/* 删除节点 */
void Delete_Node(Node *del_node) {
    Node *temp = del_node -> next;
    del_node -> next = del_node -> next -> next;
    temp = NULL;
    free(temp);
}

int main() {
    int a = 0, i = 0, index = 0;
    Node *hp = NULL;
    Node *prev_node = NULL;
    /* 创建空链表*/
    createList(&hp);
    /* 向链表中写入数据 */
    for(i = 0; i < LINK_NODE_NUM; i++) {

        printf("Insert data:\n");
        scanf("%d",&a);
        setList(hp, a);
    }
    /* 显示所有链表信息 */
    ShowList(hp);
    printf("显示所有链表数据结束!\n");
    printf("\n");
    printf("开始删除链表中的节点~\n");
    for(i = 0; i < LINK_NODE_NUM; i++) {
        printf("输入要删除节点中的数据:");
        scanf("%d",&index);
        prev_node = Find_Delnode_Prev(hp, index);
        Delete_Node(prev_node);
        ShowList(hp);
    }
    /* 释放头节点指针 */
    free(hp);
    return 0;
}

程序运行过程:

成功创建节点 0
Insert data:
100
成功创建节点 1
Insert data:
200
成功创建节点 2
Insert data:
300
成功创建节点 3
Insert data:
400
成功创建节点 4
Insert data:
500
成功创建节点 5
链表中的数据为:100
链表中的数据为:200
链表中的数据为:300
链表中的数据为:400
链表中的数据为:500
显示所有链表数据结束!
开始删除链表中的节点~
输入要删除节点中的数据:300
将要删除数据为 300 的节点…
链表中的数据为:100
链表中的数据为:200
链表中的数据为:400
链表中的数据为:500
输入要删除节点中的数据:200
将要删除数据为 200 的节点…
链表中的数据为:100
链表中的数据为:400
链表中的数据为:500
输入要删除节点中的数据:100
将要删除数据为 100 的节点…
链表中的数据为:400
链表中的数据为:500
输入要删除节点中的数据:500
将要删除数据为 500 的节点…
链表中的数据为:400
输入要删除节点中的数据:400
将要删除数据为 400 的节点…
链表为空

  通过代码实现及画图分析,会对单链表的一些操作更容易理解一些

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Caso_卡索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值