• 单链表(带头节点)


    /////////////////////////////////定义节点结构体

    struct node
    {
    int data;
    struct node *pnext;
    };

    ////////////////////////////////////main函数

    int main()
    {

    struct node *pHeader = creatList(0);

    /*insert_tail(creatList(1),pHeader);
    insert_tail(creatList(2),pHeader);
    insert_tail(creatList(3),pHeader);*/

    insert_head(creatList(1),pHeader);
    insert_head(creatList(2),pHeader);
    //creatList(1);
    printf("p1的data为:%d 总的节点数为:%d ",pHeader->pnext->data,pHeader->data);
    printf("p2的data为:%d 总的节点数为:%d ",pHeader->pnext->pnext->data,pHeader->data);
    //printf("p3的data为:%d 总的节点数为:%d ",pHeader->pnext->pnext->pnext->data,pHeader->data);
    //creatList(2);
    system("pause");
    return 0;
    }

    //////////////////////////////////创建新节点

    struct node * creatList(int data)
    {
    struct node *p ;
    p =(struct node *)malloc(sizeof(struct node));
    //忘记判断了
    if(p == NULL)
    {
    printf("创建节点指针失败");
    return NULL;
    }
    //忘记清0了
    memset(p,0,sizeof(struct node));
    p->data = data;
    p->pnext = NULL;
    return p;
    }

    ///////////////////////////////尾插法

    void insert_tail(struct node * newNode, struct node *pHeader)
    {
    struct node * p = pHeader;
    int length = 0;
    while(p->pnext!= NULL)
    {
    p = p->pnext;
    length++;
    }
    pHeader->data = length+1;
    p->pnext = newNode;

    }

    //////////////////////////////////////头插法

    void insert_head(struct node *newNode,struct node *pheader)
    {
    struct node * p = newNode;
    p->pnext = pheader->pnext;
    pheader->pnext = p;

    pheader->data +=1;
    }

  • 相关阅读:
    nginx入门与实战
    python开发之virtualenv与virtualenvwrapper讲解
    Linux下的python3,virtualenv,Mysql、nginx、redis安装配置
    Linux系统基础优化及常用命令
    vim与程序员
    Shell基本命令
    Linux之文档与目录结构
    远程连接Linux
    oracle 根据时间戳查询date类型sql
    oracle 锁用户
  • 原文地址:https://www.cnblogs.com/tiange-137/p/11962938.html
Copyright © 2020-2023  润新知