zoukankan      html  css  js  c++  java
  • 单链表(带头节点)

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

    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;
    }

  • 相关阅读:
    innobackupex备份命令输出
    Percona XtraBackup原理详解
    MongoDB性能分析工具mongostat
    MongoDB查看当前连接数
    事务、拦截器
    HttpServletResponse和HttpServletRequest的简单实用
    Maven环境配置
    SQL Server 时间戳与时间格式互相转换
    虚拟机、云主机、VPS 三者之间的区别
    Elasticsearch 空值过滤
  • 原文地址:https://www.cnblogs.com/tiange-137/p/11962938.html
Copyright © 2011-2022 走看看