zoukankan      html  css  js  c++  java
  • 单链表+链表反转

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    struct node *pHead = NULL;
    
    void display(void )
    {
        struct node *p;
        p = pHead;
        while(NULL != p)
        {
            printf("%d  ", p->data);
            p = p->next;
        }
        printf("\n\r");
    }
    
    void insert(struct node *pNode)
    {
        struct node *p;
    #if 1
        if(NULL == pHead)
        {
            pHead = pNode;
            pHead->next = NULL;
        }
        else
        {
            p = pHead;
            while(NULL != p->next)
            {
                p = p->next;
            }
            p->next = pNode;
            pNode->next = NULL;
        }
    #else
        if(NULL == pHead)
        {
            pHead = pNode;
            pHead->next = NULL;
        }
        else
        {
            pNode->next = pHead;
            pHead = pNode;
        }
    #endif    
        return;
    }
    
    void list_Reversion(void )
    {
        struct node *pre, *cur, *ne;
        pre = pHead;
        cur = pHead->next;
        
        while(NULL != cur)
        {
            ne = cur->next;
            cur->next = pre;
            pre = cur;
            cur = ne;
        }
        pHead->next = NULL;
        pHead = pre;
    }
    
    int main(void )
    {
        int i;
        struct node *p;
        for(i = 0; i < 11; i++)
        {
            p = malloc(sizeof(struct node));
            p->data = i;
            insert(p);
        }
        display();
        list_Reversion();
        display();
        return 0;
    }
  • 相关阅读:
    List集合
    ArrayList_toArray
    Collection集合基础知识
    Array类的使用
    16.10
    16.9
    16.8
    16.7
    16.6
    16.5
  • 原文地址:https://www.cnblogs.com/to7str/p/2725292.html
Copyright © 2011-2022 走看看