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;
}
相关推荐
集成显卡7 小时前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
感哥7 小时前
C++ 面向对象
c++
沐怡旸9 小时前
【底层机制】std::shared_ptr解决的痛点?是什么?如何实现?如何正确用?
c++·面试
感哥15 小时前
C++ STL 常用算法
c++
saltymilk1 天前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥1 天前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
博笙困了2 天前
AcWing学习——双指针算法
c++·算法
感哥2 天前
C++ 指针和引用
c++