• 单链表 头插 尾插 遍历 删除


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

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

    void insertTail(struct node *pH,struct node *new)
    {
    struct node *p=pH;
    int counter=0;

    while(NULL != p->pNext)
    {
    p=p->pNext;
    counter++;
    }

    p->pNext=new;
    pH->data=counter+1;
    }

    void insertHeader(struct node *pH,struct node *new)
    {
    new->pNext=pH->pNext;
    pH->pNext=new;
    pH->data=+1;
    }

    void traversal(struct node *pH)
    {
    struct node *p=pH;

    while(NULL != p->pNext)
    {
    p=p->pNext;
    printf("data : %d ",p->data);

    }
    }

    void deleteNode(struct node *pH,int data)
    {
    struct node *p=pH;
    struct node *tmp;

    while(NULL != p->pNext)
    {
    tmp=p;
    p=p->pNext;
    printf("data : %d ",p->data);
    if(p->data == data)
    {
    if(NULL == p->pNext)
    {
    tmp->pNext=NULL;
    free(p);
    }
    else
    {
    tmp->pNext=p->pNext;
    free(p);
    }
    pH->data--;
    }

    }
    }

    struct node *createNode(int data)
    {
    struct node *p = (struct node *)malloc(sizeof(struct node));
    if(NULL == p)
    {
    printf("malloc error ! ");
    return NULL;
    }

    memset(p,'',sizeof(struct node));

    p->data=data;
    p->pNext=NULL;

    return p;
    }

    int main()
    {
    struct node *pHeader;
    struct node *p,*p1,*p2;

    pHeader=createNode(0);

    insertHeader(pHeader,createNode(8));
    insertTail(pHeader,createNode(1));
    insertTail(pHeader,createNode(2));
    insertTail(pHeader,createNode(3));
    insertTail(pHeader,createNode(4));

    deleteNode(pHeader,4);
    deleteNode(pHeader,8);

    printf("counter %d ",pHeader->data);

    traversal(pHeader);

    printf("data %d ",pHeader->pNext->data);
    printf("hello world ! ");
    return 0;
    }

  • 相关阅读:
    悲剧的程序员
    【C++】关于随机函数与概率设置
    布局管理器(一)
    敏捷开发,如何搜集故事
    【Visual C++】CDC与HDC的区别以及相互转换
    IFRAME without src attribute on HTTPS in Internet Explorer
    测量某断代码执行时间-代码
    数据一致性实现技术
    ffmpeg
    Log4j的使用方法
  • 原文地址:https://www.cnblogs.com/zhangjianrong/p/11625430.html
Copyright © 2020-2023  润新知