Skip to content

服务器托管,北京服务器托管,服务器租用-价格及机房咨询

Menu
  • 首页
  • 关于我们
  • 新闻资讯
  • 数据中心
  • 服务器托管
  • 服务器租用
  • 机房租用
  • 支持中心
  • 解决方案
  • 联系我们
Menu

单链表知识点

Posted on 2023年9月19日 by hackdl

单链表

1.顺序表

优点:物理空间连续,支持随机访问

缺点:空间不够就需要扩容,花费时间和空间;插入删除效率低下

2.单链表

优点:按需申请释放空间;插入删除常数时间

缺点:不支持随机访问

3.注意点

(1)在修改指针本身的内容时,也就是改变指针本身存储的地址,我们需要的是二级指针

void list_push_back(struct node** head, type x)
{
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;

    //这里想改变头结点的地址,让它指向一块新的空间,也就是改变一个*类型的值->于是我们需要**作为形式参数!!
    if (*head == NULL)
    {
        *head = newnode;
    }

    else 
    {
        struct node* cur = *head;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = newnode;
    }
}

4.实现

//forward_list s;里面的插入删除:insert_after erase_after
void list_print(struct node* head)
{
    struct node* cur = head;
    while (cur != NULL)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
    printf("n");
}
struct node* buy_newnode(type x)
{
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}
void list_push_back(struct node** phead, type x)
{
    assert(phead);
    struct node*newnode =  buy_newnode(x);

    //这里想改变头结点的地址,让它指向一块新的空间,也就是改变一个*类型的值->于是我们需要**作为形式参数!!
    if (*phead == NULL)
    {
        *phead = newnode;
    }

    else 
    {
        struct node* cur = *phead;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = newnode;
    }
}
//也需要传入二级指针:不管有没有元素,都需要改变head的值,因为head必须是指向第一个结点的!!! 
void list_push_front(struct node** phead, type x)
{
    assert(phead);
    struct node* newnode = buy_newnode(x);
    newnode->next = *phead;
    *phead = newnode;

    //验证上下都可以但上面的更加简洁
    /*if (*phead == NULL)
    {
        *phead = newnode;
    }
    else
    {
        newnode->next = *phead;
        *phead = newnode;
    }*/
}
//一级指针不用检查:它可能就是一个空链表
//二级指针需要检查:head是存在的,head有这个变量,那就一定有个地址。phead里面存放的就是head的地址
void list_pop_front(struct node**phead)
{
    assert(phead);
    assert(*phead);//这里头删,head一定需要有值才可以删除
    struct node* pre_head = *phead;
    *phead = (*phead)->next;
    free(pre_head);//释放它指向的空间
}
void list_pop_back(struct node**phead)
{
    assert(phead);
    assert(*phead);
    struct node* t = *phead;

    if (t->next == NULL)
    {
        free(*phead);
        *phead = NULL;
    }
    else
    {
        while (t->next->next != NULL)
        {
            t = t->next;
        }
        t->next = NULL;
        free(t->next);
    }     
}
struct node* list_find(struct node* head, type x)
{
    assert(head);
    struct node* t = head;
    while (t != NULL)
    {
        if (t->data == x)
        {
            return t;
        }
        t = t->next;
    }
    return NULL;
}
void list_insert(struct node** phead, struct node* pos, type x)
{ 
    assert(pos);
    assert(phead);
    if (pos == *phead)
    {
        list_push_front(phead, x);
    }
    else
    {
        struct node* t = *phead;
        while (t->next != pos)
        {
            t = t->next;
        }
        struct node*newnode = buy_newnode(x);
        newnode->next = pos;
        t->next = newnode;
    }
}
void list_erase(struct node**phead, struct node *pos)
{
    assert(phead);
    assert(pos);//如果链表为空,pos不可能有效
    if (pos == *phead)
    {
        list_pop_front(phead);
    }
    else
    {
        struct node* t = *phead;
        while (t->next != pos)
        {
            t = t->next;
        }
        t->next = pos->next;
        free(pos);
        //pos = NULL;//几乎无效?因为pos是个形参,在这里置空无用,交给外面处理
    }
}
void list_insert_after(struct node* pos, type x)
{
    assert(pos);
    struct node * newnode = buy_newnode(x);

    newnode->next = pos->next;
    pos->next = newnode;
}
void list_erase_after(struct node* pos)
{
    assert(pos->next);
    struct node* t = pos->next;
    pos->next = pos->next->next;
    free(t);
}

服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net

相关推荐: .Net7基础类型的优化和循环克隆优化

前言 .Net7里面对于基础类型的优化,是必不可少的。因为这些基础类型基本上都会经常用到,本篇除了基础类型的优化介绍之外,还有一个循环克隆的优化特性,也一并看下。 概括 1.基础类型优化 基础类型的优化有些不会涉及ASM,主要是记忆。 一:double.Par…

Related posts:

  1. 三沙市服务器托管报价网站模板:完美解决企业服务器管理问题
  2. 河北服务器托管:2000元高性能云空间
  3. 无需收银!亚马逊推出革命性的线下便利店!
  4. 云服务器托管与管理:优化效率与安全性
  5. 江西远程服务器托管费用解析

服务器托管,北京服务器托管,服务器租用,机房机柜带宽租用

服务器托管

咨询:董先生

电话13051898268 QQ/微信93663045!

上一篇: GaussDB云数据库SQL应用系列-定时任务管理
下一篇: 见识一下SQL Server隐式转换处理的不同

最新更新

  • 一文带你从了解到搭建 HTTP/3 Web 服务
  • Autocad C#二次开发煤矿数据处理
  • vue3探索——5分钟快速上手大菠萝pinia
  • vCenter Server7出现no healthy upstream的排查方法
  • 单文件组件形式

随机推荐

  • 北京双线服务器托管价格调查报告
  • 辽宁中文版服务器托管云空间
  • 1U服务器托管教程:详细指南
  • 什么是反射?它有什么用?
  • 做个清醒的程序员之打造核心竞争力

客服咨询

  • 董先生
  • 微信/QQ:93663045
  • 电话:13051898268
  • 邮箱:dongli@hhisp.com
  • 地址:北京市石景山区重聚园甲18号2层

友情链接

  • 服务器托管
  • 机房租用托管
  • 服务器租用托管
©2023 服务器托管,北京服务器托管,服务器租用-价格及机房咨询 京ICP备13047091号-8