• 【数据结构复习】链表的倒置(头插法倒置)


    #include <bits/stdc++.h>
    using namespace std;
    typedef int ElemType;
    struct LNode{
        ElemType data;
        LNode *next;
    };
    LNode *head,*tail;
    
    void init(){
        head = (LNode*)malloc(sizeof(LNode));
        head->next = NULL;
        tail = head;
    }
    
    void input_data(){
        int x;
        cin >> x;
        while (x!=-1){
            LNode *temp = (LNode*)malloc(sizeof(LNode));
            temp->data = x;
            temp->next = NULL;
            tail->next = temp;
            tail = temp;
            cin >> x;
        }
    }
    
    //关键函数
    void _reverse(){
        LNode *p = head->next;
        head->next = NULL;
        while (p){
            LNode *temp = p->next;//记录下当前遍历到的这个节点的下一个
            p->next = head->next;//这个几点的下一个节点接在头结点后面的那个节点
            head->next = p;//头结点的后一个节点指向该节点,从而完成插入过程
            p = temp;
        }
    }
    
    void print(){
        LNode *temp = head->next;
        while (temp){
            cout<<temp->data<<" ";
            temp = temp->next;
        }
    }
    
    int main(){
        init();
        //freopen("D://rush.txt","r",stdin);
        input_data();
        _reverse();
        print();
        fclose(stdin);
        return 0;
    }
    

      

  • 相关阅读:
    mac安装搜狗
    idea的阿里代码规范检查
    记录windows10闪屏
    github访问不了
    线程安全问题例子
    简单负载均衡工具类
    git将远程分支回归到指定版本
    minio的使用
    php禁止浏览器使用缓存页面的方法
    百度seo
  • 原文地址:https://www.cnblogs.com/AWCXV/p/11568644.html
Copyright © 2020-2023  润新知