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;
}
相关推荐
OTWOL15 分钟前
【C++编程入门基础(一)】
c++·算法
宇寒风暖1 小时前
侯捷 C++ 课程学习笔记:内存管理与工具应用
c++·笔记·学习
开开心心就好1 小时前
娱乐使用,可以生成转账、图片、聊天等对话内容
windows·python·智能手机·软件工程·娱乐·软件需求
Smile丶凉轩1 小时前
数据库面试知识点总结
数据库·c++·mysql
Want5952 小时前
C/C++跳动的爱心
c语言·开发语言·c++
laimaxgg2 小时前
Qt常用控件之数字显示控件QLCDNumber
开发语言·c++·qt·qt5·qt6.3
蓝天扶光2 小时前
c++贪心系列
开发语言·c++
Alidme2 小时前
cs106x-lecture14(Autumn 2017)-SPL实现
c++·学习·算法·codestepbystep·cs106x
小王努力学编程2 小时前
【算法与数据结构】单调队列
数据结构·c++·学习·算法·leetcode
~kiss~3 小时前
python的thrift2pyi学习
windows·python·学习