C语言实现单链表的相关操作

#include <stdio.h>
#include <stdlib.h>
// C语言实现单链表的相关操作
typedef int datatype_t;
typedef struct node
{
    datatype_t data;
    struct node *next;
} linklist_t;

// 创建一个空的单链表 本质为创建一个空的链表头
linklist_t *linklist_create() {
    linklist_t *h = (linklist_t *)malloc(sizeof(linklist_t));
    h->next = NULL;
    return h;
}

// 头插法
int linklist_head_insert(linklist_t *h, datatype_t value) {
    linklist_t *temp;
    temp = (linklist_t *)malloc(sizeof(linklist_t));
    temp->data = value;
    temp->next = h->next;
    h->next = temp;
    return 0;
}

// 尾插法
int linklist_tail_insert(linklist_t *h, datatype_t value) {
    linklist_t * temp;
    temp = (linklist_t *)malloc(sizeof(linklist_t));
    temp->data = value;

    // 遍历整个链表 找到最后一个位置进行插入
    while (h->next != NULL)
    {
        h = h->next;
    }

    h->next = temp;
    temp->next = NULL;
    return 0;
}

// 顺序插入
int linklist_sort_insert(linklist_t *h, datatype_t value) {
    linklist_t * temp;
    temp = (linklist_t *)malloc(sizeof(linklist_t));
    temp->data = value;

    // 遍历所有节点并判断是否插入 在这里改变了插入的位置
    while (h->next != NULL && h->next->data < value)
    {
        h = h->next;// 改变插入的位置
    }

    temp->next = h->next;
    h->next = temp;
    return 0;
}

// 指定位置插入数据节点
int linklist_pos_insert(linklist_t *h, datatype_t value, int pos) {
    int i = 0;
    // 为插入的数据节点申请内存空间
    linklist_t * temp = (linklist_t *)malloc(sizeof(linklist_t));
    // 为新插入的数据节点赋值
    temp->data = value;

    // 判断位置 找到需要插入数据节点的位置
    while (i < pos && h->next != NULL)
    {
        h = h->next;
        i++;
    }

    // 使用头插法的思想插入数据节点
    temp->next = h->next;
    h->next = temp;
    return 0;
}

// 单链表的显示
int linklist_show(linklist_t *h) {
    while (h->next != NULL)
    {
        h = h->next;
        printf("%d ", h->data);
    }
    
    printf("\n");
    return 0;
}

// 判断链表是否为空
int linklist_empty(linklist_t *h) {
    return h->next == NULL ? 1 : 0;
}

// 头删法删除数据节点
int linklist_delete_head(linklist_t *h) {
    linklist_t *temp;
    datatype_t value;
    temp = h->next;
    value = temp->data;

    h->next = temp->next;
    free(temp);
    temp = NULL;
    return value;
}

// 删除节点中的指定数据
int linklist_delete_value(linklist_t *h, datatype_t value) {

    linklist_t *p = NULL;
    while (h->next != NULL)
    {
        if (h->next->data == value)
        {
            p = h->next;// 取出下一个值为value的
            h->next = p->next;// 将下一个的下一个拼接到链表上
            free(p);// 释放该值
            p = NULL;
            return 0;
        } else {
            h = h->next;// 继续向后移动
        }
    }
    
    printf("no value\n");
    return -1;
}

// 修改节点数据
int linklist_change(linklist_t *h, datatype_t old_value, datatype_t new_value) {
    // 循环遍历整个单链表 判断是否有匹配的数据
    while (h->next != NULL)
    {
        if (h->next->data == old_value) {
            h->next->data = new_value;
            return 0;
        } else {
            h = h->next;
        }
    }

    printf("no value\n");
    return -1;
}

// 查找指定数据节点的位置
int linklist_search(linklist_t *h, datatype_t value) {
    int pos = 0;
    while (h->next != NULL)
    {
        if (h->next->data == value)
        {
            return pos;
        } else {
            h = h->next;
            pos++;
        }
    }
    
    printf("no value\n");
    return -1;
}

// 链表数据的反转
int linklist_recover(linklist_t *h) {
    linklist_t *q = NULL;
    // 将单链表中头结点与其它节点断开 并将指针p指向首个带数据的节点
    linklist_t *p = h->next;
    h->next = NULL;
    // 判断下一节点是否存在
    while (p != NULL)
    {
        // 核心操作
        q = p;// 将q和p指向相同的节点
        p = p->next;// 移动指针p 将p指向下一个节点

        // 头插法 重新开始插入数据节点
        q->next = h->next;
        h->next = q;// 将q指向的节点重新插入
    }
    return 0;
}

// 单链表的合并 这里使用归并排序
/* 归并排序 将已有子序合并 得到完全有序的新序列 归并排序的前提是 子序列原来就是有序的*/
void linklist_merge(linklist_t *h1, linklist_t *h2) {
    linklist_t *p = h1->next;// 使用指针p指向的单链表1的首个含有数据的节点
    linklist_t *q = h2->next;// 使用指针1指向的单链表2的首个含有数据的节点
    // 使用指针i指向单链表1的头节点,该头结点将作为新单链表的头结点
    linklist_t *i = h1;
    free(h2); // 释放单链表2头结点使用的内存空间
    // 循环判断单链表是否还有节点
    while (p && q)
    {
        // 对比两个单链表中节点的值 从第一个节点开始
        if (p->data != q->data)
        {
            i->next = p; // 将节点连接到i指向的新单链表
            i = p; // 移动指针i指向新连入的节点
            p = p->next;// 移动指针p指向单链表i的下一个节点 用于后续对比
        } else {
            i->next = q; // 将节点连接到i指向的新单链表
            i = q; // 移动指针i指向新连入的节点
            q = q->next;// 移动指针q指向单链表2的下一个节点 用于后续对比
        }
    }

    if (p == NULL) // 如果单链表1中的节点对比完
    {
        i->next = q;// 直接将单链表2剩余节点连接到新单链表
    }

    if (q == NULL) // 如果单链表2中的节点对比完
    {
       i->next = p;// 直接将单链表1剩余节点连接到新单链表
    }
}

// 单链表的销毁
void linklist_destroy(linklist_t *h) {
    linklist_t *p;
    while (h->next)// 如果元素不是空的就继续执行
    {
        p = h->next;// p为h的第一个有效节点
        h->next = p->next;// 第一个有效节点的下一个节点给到h->next 相当于跳过了第一个有效节点 开头直接指向第二个节点
        free(p);//释放第一个节点
    }
    
    free(h);
}

int main() {
    linklist_t *h;
    h = linklist_create();
    // 按顺序插入数据节点
    linklist_sort_insert(h, 80);
    linklist_sort_insert(h, 60);
    linklist_sort_insert(h, 40);

    linklist_show(h);

    linklist_tail_insert(h, 800);
    linklist_tail_insert(h, 600);
    linklist_tail_insert(h, 400);

    // 显示节点数据 判断是否插入成功
    linklist_show(h);

    // 头删法删除数据节点
    linklist_delete_head(h);
    linklist_show(h);
    linklist_tail_insert(h, 300);
    linklist_show(h);

    // 删除指定的值
    linklist_delete_value(h, 80);
    linklist_show(h);
    

    // 头插法插入数据
    linklist_head_insert(h, 30);
    linklist_head_insert(h, 10);
    linklist_show(h);

    // 向指定位置插入数据
    linklist_pos_insert(h, 5, 0);
    linklist_pos_insert(h, 6, 1);
    linklist_show(h);

    linklist_destroy(h);
    linklist_show(h);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值