从头撸一个单链表

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

  最近在学习单链表时,对于单链表的头节点指针的创建,以及如何给一个链表存入数据,这块理解不是很到位。虽然明白链表的基本操作,创建、插入、删除、索引,但用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, [email protected]
* @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 的节点…
链表为空

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

发布了52 篇原创文章 · 获赞 81 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xiaoma_2018/article/details/88938366