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;
}
相关推荐
位东风21 分钟前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
雷羿 LexChien2 小时前
C++内存泄漏排查
开发语言·c++
嘉小华3 小时前
CMake 完全指南:第一章 - 构建的烦恼 - 为什么需要CMake?
c++
Feliz Da Vida3 小时前
[代码学习] c++ 通过H矩阵快速生成图像对应的mask
c++·学习
无聊的小坏坏4 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
Bruce_Liuxiaowei4 小时前
Netstat高级分析工具:Windows与Linux双系统兼容的精准筛查利器
linux·运维·网络·windows·安全
YOLO大师5 小时前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷
Par@ish5 小时前
【网络安全】恶意 Python 包“psslib”仿冒 passlib,可导致 Windows 系统关闭
windows·python·web安全
看到我,请让我去学习6 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
emplace_back14 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#