• 单链表尾插法,头结点作为结点个数进行计数


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

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

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

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

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

  • 相关阅读:
    Android第四次作业
    Android第三次作业
    android 第一次作业
    团队作业-项目答辩
    团队作业2
    软工作业--团队作业2
    软件工程—团队作业1
    软件工程第一次作业
    Android第四次作业
    Android 第三次作业
  • 原文地址:https://www.cnblogs.com/zhangjianrong/p/11624181.html
Copyright © 2020-2023  润新知