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;
}
相关推荐
whoarethenext23 分钟前
C++ OpenCV 学习路线图
c++·opencv·学习
黄交大彭于晏27 分钟前
发送文件脚本源码版本
java·linux·windows
闻缺陷则喜何志丹33 分钟前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
hutaotaotao1 小时前
c++中的输入输出流(标准IO,文件IO,字符串IO)
c++·io·fstream·sstream·iostream
AL流云。1 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法
qq_429879672 小时前
省略号和可变参数模板
开发语言·c++·算法
CodeWithMe3 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi3 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
zh_xuan8 小时前
c++ 单例模式
开发语言·c++·单例模式
利刃大大10 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目