zoukankan      html  css  js  c++  java
  • 链表

    全部代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    typedef struct node
    {
        int nValue;
        struct node * pNext;
    }MyList;
    
    void CreateList(MyList **ppHead)
    {
        MyList *pTemp = NULL;
        MyList *pTail = NULL;
    
        //the number of node
        int number;
    
        int i;
    
        //the value of node
        int value;
    
        assert(ppHead != NULL);
    
        printf("please input a number of the node :");
        scanf("%d", &number);
    
        for(i=0; i<number; ++i)
        {
            printf("please input the %d value of node :", i+1);
            scanf("%d", &value);
    
            pTemp = (MyList *)malloc(sizeof(MyList));
            if(NULL == pTemp)
            {
                printf("allocate space failed !
    ");
                exit(-1);
            }
            pTemp->nValue = value;
            pTemp->pNext = NULL;
    
            //add node
            if(NULL == *ppHead)
            {
                *ppHead = pTemp;
            }
            else
            {
                pTail->pNext = pTemp;
            }
            pTail = pTemp;
        }
    }
    
    void Traverse(MyList *pHead)
    {
        if(NULL == pHead)
        {
            printf("list is null
    ");
            return;
        }
    
        while(pHead != NULL)
        {
            printf("%d ", pHead->nValue);
            pHead = pHead->pNext;
        }
    }
    
    //reverse traverse
    void RecTraverse(MyList *pHead)
    {
        if(NULL == pHead)
        {
            return;
        }
    
        RecTraverse(pHead->pNext);
        printf("%d ", pHead->nValue);
    }
    
    //the inversion of list
    void Reverse(MyList **ppHead)
    {
        MyList *p1 = NULL;
        MyList *p2 = NULL;
        MyList *p3 = NULL;
    
        assert(ppHead != NULL);
    
        if(NULL==*ppHead || NULL==(*ppHead)->pNext)
        {
            return;
        }
    
        p2 = *ppHead;
        p3 = p2->pNext;
    
        while(p3 != NULL)
        {
            //change direction
            p2->pNext = p1;
    
            //handle the next node
            p1 = p2;
            p2 = p3;
            p3 = p2->pNext;
        }
        p2->pNext = p1;
    
        *ppHead = p2;
    }
    
    int main(void)
    {
        MyList *pHead = NULL;
    
        CreateList(&pHead);
        Traverse(pHead);
        printf("
    ");
    
        RecTraverse(pHead);
        printf("
    ");
    
        Reverse(&pHead);
        Traverse(pHead);
        printf("
    ");
    
        RecTraverse(pHead);
        printf("
    ");
    
        return 0;
    }
  • 相关阅读:
    ad_imh
    pc send instructor pc ad
    数据、模型、IT系统认知
    量化投资认知
    LinAlgError: Last 2 dimensions of the array must be square
    转:Hadoop大数据开发基础系列:七、Hive基础
    Run-Time Check Failure #2
    0x00007FFC8C5325E7 (ucrtbased.dll)处(位于 DataStructure.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
    栈与后缀表达式C实现
    Jupyter使用
  • 原文地址:https://www.cnblogs.com/chen-cai/p/7760486.html
Copyright © 2011-2022 走看看