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;
    }
  • 相关阅读:
    计算机系统概述
    Qt学习--初学注意事项
    Qt实现一个简单的TextEditor
    Qt 用户登录界面
    C++ 模板
    多态与虚函数
    继承与派生
    C++ 运算符重载
    web安全-点击劫持
    web安全问题-cookie
  • 原文地址:https://www.cnblogs.com/to7str/p/2725292.html
Copyright © 2011-2022 走看看