Day4.

单链表

cs 复制代码
#include <head.h>

typedef struct List{
    int value;
    struct List *pointe;
}*list;
list create_space()
{
    list s=(struct List *)malloc(sizeof(struct List));   //向堆区申请空间
    s->pointe = NULL;//初始化
    s->value = 0;
    return s;
}
list inserhead_list(list head,int value)
{
    list s = create_space();
    if(s == NULL)
    {
        return head;
    }
    s->value = value;
    if(head == NULL)
    {
        head = s;
        return head;
    }else{
        s->pointe = head;
        head = s;
    }
    return head;
}
//头删
list delete_list(list head)
{
    list p = head;
    if(head == NULL)
    {
        free(head);
        return head;
    }
    head = p->pointe;
    free(p);
    return head;
}
void output(list head)
{
    if(head == NULL)
    {
        return; 
    }
    while(head)
    {
        printf("%d ",head->value);
        head = head->pointe;
    }
}


int main(int argc,const char *argv[])
{

    list head = NULL;
    int value;
    for (int i = 0; i < 5; i++)
    {
        printf("请输入插入的值:");
        scanf("%d",&value);
        head = inserhead_list(head,value);
    }
    output(head);
    head=delete_list(head);
    puts("");
    output(head);
    return 0;
}

双链表

cs 复制代码
#include <head.h>

typedef struct List{
    int value;
    struct List *next;
    struct List *priv;
}*list;
list create_space()
{
    list s=(struct List *)malloc(sizeof(struct List));   //向堆区申请空间
    s->next = NULL;//初始化
    s->priv = NULL;
    s->value = 0;
    return s;
}
头插
list inserhead_list(list head,int value)
{
    list s = create_space();
    if(s == NULL)
    {
        return head;
    }
    s->value = value;
    if(head == NULL)
    {
        head = s;
        return head;
    }else{
        s->next = head;
        head->priv = s;
        head = s;
    }
    return head;
}
//头删
list delete_list(list head)
{
    list p = head;
    if(head == NULL)
    {
        free(head);
        return head;
    }
    head = p->next;
    head->priv = NULL;
    free(p);
    return head;
}
int output(list head)
{
    //1,判断链表为空
    if(NULL ==head)
        return -1;
    //2.正向遍历
    puts("正向遍历");
    list p=head;
    while(p->next!=NULL)
    {
        printf("%d\t",p->value);
        p=p->next;
    }
    printf("%d\t",p->value);

    puts("\n逆向遍历");
    while(p!=NULL)
    {
        printf("%d\t",p->value);
        p=p->priv;    
    }
    puts("");
    return 0;
}

int main(int argc,const char *argv[])
{

    list head = NULL;
    int value;
    for (int i = 0; i < 5; i++)
    {
        printf("请输入插入的值:");
        scanf("%d",&value);
        head = inserhead_list(head,value);
    }
    output(head);
    head=delete_list(head);
    puts("------------");
    output(head);
    return 0;
}
相关推荐
利刃大大1 小时前
【高并发内存池】五、页缓存的设计
c++·缓存·项目·内存池
C语言小火车1 小时前
【C++八股文】基础知识篇
c++·tcp/ip·const·智能指针·多线程同步·static关键字·c++内存模型
liulilittle2 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
眠りたいです2 小时前
基于脚手架微服务的视频点播系统-播放控制部分
c++·qt·ui·微服务·云原生·架构·播放器
Want5952 小时前
C/C++圣诞树①
c语言·开发语言·c++
老赵的博客2 小时前
c++ 杂记
开发语言·c++
jimmy.hua2 小时前
[C++刷怪笼]:set/map--优质且易操作的容器
开发语言·c++
tan180°3 小时前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
bkspiderx3 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
郝学胜-神的一滴4 小时前
避免使用非const全局变量:C++中的最佳实践 (C++ Core Guidelines)
开发语言·c++·程序人生