C/C++ - 数据结构(链表、栈、队列、树(二叉树))、动态数组

这篇博客介绍了C/C++中数据结构的基本概念和实现,包括链表的两种实现思路、动态数组的C语言实现、栈的顺序存储和链式存储以及队列的基本概念和存储方式。此外,还涉及到了树的表示法和二叉树的遍历方法。
摘要由CSDN通过智能技术生成


链表(思路一)

普通单向链表

初始化

在这里插入图片描述

2020年10月08日
缺点:

  1. 结构体设计没有通用性
    int val 换成 void *data

优化参考:【最新c、c++全栈培训第24期(上半部分)】单向链表(版本一)

#include <stdio.h>
#include <stdlib.h>

struct LinkNode
{
   
    int val;
    struct LinkNode *next;
} l;

void destroy(struct LinkNode *node)
{
   
    if (node == NULL)
    {
   
        return;
    }
    struct LinkNode *next = node->next;
    free(node);
    printf("(destroy)%p\n", node);
    if (next != NULL)
    {
   
        destroy(next);
    }
}

void show(struct LinkNode *first)
{
   
    if (first == NULL)
    {
   
        return;
    }
    printf("%p %d\n", first, first->val);
    if (first->next != NULL)
    {
   
        show(first->next);
    }
}

void build(struct LinkNode **first, int *vals, int len)
{
   
    if (len > 0)
    {
   
        *first = malloc(sizeof(struct LinkNode));
        (*first)->val = vals[0];
        build(&(*first)->next, vals + 1, len - 1);
    }
    else
    {
   
        *first = NULL;
    }
}

void test()
{
   
    struct LinkNode *node1;
    int vals[] = {
   1, 2, 3, 4, 5, 6};
    int len = sizeof(vals) / sizeof(vals[0]);
    build(&node1, vals, len);
    show(node1);
    destroy(node1);
}

int main(int argc, char *argv[])
{
   
    test();
    return 0;
}

在这里插入图片描述

插入、删除(add_next、remove_next)

void add_next(struct LinkNode *first, struct LinkNode *node)
{
   
    if (first == NULL || node == NULL)
    {
   
        return;
    }
    // 合并尾部
    struct LinkNode *end = node;
    while (end->next != NULL)
    {
   
        end = end->next;
    }
    end->next = first->next;
    // 合并前面
    first->next = node;
}

void remove_next(struct LinkNode *node)
{
   
    if (node == NULL || node->next == NULL)
    {
   
        return;
    }
    struct LinkNode *next = node->next->next;
    free(node->next);
    node->next = next;
}

void test()
{
   
    struct LinkNode *node1;
    int vals[] = {
   1, 2, 3, 4, 5, 6};
    int len = sizeof(vals) / sizeof(vals[0]);
    printf("\n-------原始-------\n");
    build(&node1, vals, len);
    show(node1);
    printf("\n-------删除-------\n");
    remove_next(node1);
    //             1    3    4     5
    remove_next(node1->next->next->next);
    show(node1);
    printf("\n-------增加-------\n");
    struct LinkNode *nodel2;
    int vals2[] = {
   11, 22, 33, 44};
    int len2 = sizeof(vals2) / sizeof(vals2[0]);
    build(&nodel2, vals2, len2);
    add_next(node1->next, nodel2);
    show(node1);
    printf("\n-------清理-------\n");
    destroy(node1);
}

在这里插入图片描述

链表(思路二)

https://www.bilibili.com/video/BV13b411V73v?p=284

在这里插入图片描述

首地址可以表示用户数据,也可以表示node

#include <stdio.h>
#include <stdlib.h>

struct LinkNode
{
   
    struct LinkNode *next;
};

struct LinkList
{
   
    struct LinkNode header;
    int size;
};

typedef void *List;

// 初始化链表
List init_LinkList()
{
   
    struct LinkList *list = malloc(sizeof(List));
    if (NULL == list)
    {
   
        // log...
        return NULL;
    }
    list->header.next = NULL;
    list->size = 0;
    return list;
}
//
void insert_LinkList(List list, int offset, void *data)
{
   
    if (list == NULL)
    {
   
        // log...
        return;
    }
    struct LinkList *myList = (struct LinkList *)list;
    struct LinkNode *newNode = (struct LinkNode *)data;
    // 下标
    if (0 > offset)
    {
   
        offset = 0;
    }
    else if (offset >= myList->size)
    {
   
        offset = myList->size;
    }
    //
    struct LinkNode *previousNode = &myList->header;
    for (int i = 0; i < offset; i++)
    {
   
        previousNode = previousNode->next;
    }
    if (previousNode->next == NULL)
    {
   
        previousNode->next = newNode;
        (myList->size)++;
    }
    else
    {
   
        if (newNode != NULL)
        {
   
            newNode->next 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

骆言

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

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

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

打赏作者

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

抵扣说明:

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

余额充值