• PapaMelon #5 设计单向链表


    题目链接

    题解

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <cassert>
    
    using namespace std;
    
    const vector<string> OP = {
        "PUSH_FRONT", "POP_FRONT", "INSERT",  "CONTAINS",
        "REMOVE",     "REVERSE",   "FOR_EACH"};
    
    struct Node {
        int val;
        Node* next;
        Node(): val(-1), next(nullptr) {}
        Node(int x): val(x), next(nullptr) {}
    };
    
    struct List {
        Node* head;
    
        List() {
            head = new Node();
        }
    
        ~List() {
            clear();
            delete head;
        }
    
        void clear() {
            while (head->next) {
                Node* ptr = head->next->next;
                head->next->next = nullptr;
                delete head->next;
                head->next = ptr;
            }
        }
    
        void push_front(int x) {
            Node* ptr = head->next;
            Node* node = new Node(x);
            node->next = ptr;
            head->next = node;
        }
    
        void pop_front() {
            Node* node = head->next;
            if (!node) return;
            head->next = node->next;
            node->next = nullptr;
            delete node;
        }
    
        void insert(int index, int x) {
            Node* ptr = head->next;
            Node* pre = head;
            for (int i = 0; i < index; i++) {
                pre = ptr;
                ptr = ptr->next;
            }
            Node* node = new Node(x);
            pre->next = node;
            node->next = ptr;
        }
    
        bool contains(int x) {
            Node* ptr = head->next;
            while (ptr && ptr->val != x) ptr = ptr->next;
            return ptr != nullptr;
        }
    
        void remove(int x) {
            Node* pre = head;
            Node* ptr = head->next;
    
            while (ptr) {
                if (ptr->val == x) {
                    pre->next = ptr->next;
                    ptr->next = nullptr;
                    delete ptr;
                    ptr = pre->next;
                } else {
                    pre = ptr;
                    ptr = ptr->next;
                }
            }
        }
    
        void reverse() {
            Node* pre = nullptr;
            Node* ptr = head->next;
            while (ptr) {
                Node* node = ptr->next;
                ptr->next = pre;
                pre = ptr;
                ptr = node;
            }
            head->next = pre;
        }
    
        void foreach() {
            Node* ptr = head->next;
            for (int i = 0; ptr; i++, ptr = ptr->next) {
                if (i) cout << " ";
                cout << ptr->val;
            }
            cout << endl;
        }
    
    };
    
    List data;
    
    void lpushfront() {
        int x;
        cin >> x;
        data.push_front(x);
    }
    
    void lpopfront() {
        data.pop_front();
    }
    
    void linsert() {
        int index, x;
        cin >> index >> x;
        // auto it = data.begin();
        // for (int i = 0; i < index; i++) it++;
        // data.insert(it, x);
        data.insert(index, x);
    }
    
    void lcontains() {
        int x;
        cin >> x;
        bool ret = data.contains(x);
        if (ret) cout << "true" << endl;
        else cout << "false" << endl;
    }
    
    void lremove() {
        int x;
        cin >> x;
        data.remove(x);
    }
    
    void lreverse() {
        data.reverse();
    }
    
    void lforeach() {
        data.foreach();
    }
    
    void solve() {
        int M;
        cin >> M;
        
        data.clear();
        while (M--) {
            string op;
            cin >> op;
            int id = find(OP.begin(), OP.end(), op) - OP.begin();
    
            switch (id) {
                case 0:
                    lpushfront();
                    break;
                case 1:
                    lpopfront();
                    break;
                case 2:
                    linsert();
                    break;
                case 3:
                    lcontains();
                    break;
                case 4:
                    lremove();
                    break;
                case 5:
                    lreverse();
                    break;
                case 6:
                    lforeach();
                    break;
                default:
                    assert(0);
            }
        }
    }
    
    int main() {
        solve();
        return 0;
    }
    
    
  • 相关阅读:
    List装form
    《Java设计模式》之调停者模式(Mediator)
    android 4.0 禁用系统home键
    最大权二分匹配
    hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流
    【python】filter()
    【python】linux将python改为默认3.4版本
    【linux】VMware12.0安装
    【python】lxml-The E-factory
    【xml】python的lxml库使用
  • 原文地址:https://www.cnblogs.com/scau20110726/p/15002810.html
Copyright © 2020-2023  润新知