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;
    }
  • 相关阅读:
    用IIS做宿主的WCF服务
    Apache+mono+xsp搭建Linux下的asp.net平台
    web页面中的卡片布局代码
    GridView内的数据循环滚动
    adb 无法启动问题
    User interface
    动态生成linearLayout
    跳转到下一个activity
    android studio 快捷键
    [转]项目失败的经验
  • 原文地址:https://www.cnblogs.com/chen-cai/p/7760486.html
Copyright © 2011-2022 走看看