zoukankan      html  css  js  c++  java
  • Doubly Linked List Aizu

    Your task is to implement a double linked list.

    Write a program which performs the following operations:

    insert x: insert an element with key x into the front of the list.
    delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.
    deleteFirst: delete the first element from the list.
    deleteLast: delete the last element from the list.

    Input

    The input is given in the following format:

    n
    command1
    command2

    commandn
    In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

    insert x
    delete x
    deleteFirst
    deleteLast

    Output

    Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

    Constraints

    The number of operations ≤ 2,000,000
    The number of delete operations ≤ 20
    0 ≤ value of a key ≤ 109
    The number of elements in the list does not exceed 106
    For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

    Sample Input 1

    7
    insert 5
    insert 2
    insert 3
    insert 1
    delete 3
    insert 6
    delete 5

    Sample Output 1

    6 1 2

    Sample Input 2

    9
    insert 5
    insert 2
    insert 3
    insert 1
    delete 3
    insert 6
    delete 5
    deleteFirst
    deleteLast

    Sample Output 2

    1

    code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00             ^   11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                ^ 00.                                     ^^0.^
                                 1 ^ 0                                     ^^110.^
                               0.   0 ^                                    ^^^10.01
                       ^^     010^   1 1                                   ^^^1110.1
                       0001  10 0   ^ 1.1                                   ^^^1111110
                       0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                       10  10^ 0^                                             ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Doubly Linked List Aizu - ALDS1_3_C.cpp created by VB_KoKing on 2019-04-29:12.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first hurricane that passed through your ear is you,
    The first star you see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct Node {
        int key;
        Node *next, *prev;
    };
    
    Node *nil;
    
    void init() {
        nil = (Node *) malloc(sizeof(Node));
        nil->next = nil;
        nil->prev = nil;
    }
    
    Node *list_search(int key) {
        Node *cur = nil->next;
        while (cur != nil && cur->key != key)
            cur = cur->next;
        return cur;
    }
    
    void print_list() {
        Node *cur = nil->next;
        int isf = 0;
        while (true) {
            if (cur == nil) break;
            if (isf++ > 0) cout << ' ';
            printf("%d", cur->key);
            cur = cur->next;
        }
        cout << endl;
    }
    
    void delete_Node(Node *t) {
        if (t == nil) return;
        t->prev->next = t->next;
        t->next->prev = t->prev;
        free(t);
    }
    
    void delete_first() { delete_Node(nil->next); }
    
    void delete_last() { delete_Node(nil->prev); }
    
    void delete_key(int key) { delete_Node(list_search(key)); }
    
    void insert(int key) {
        Node *x = (Node *) malloc(sizeof(Node));
        x->key = key;
        x->next = nil->next;
        nil->next->prev = x;
        nil->next = x;
        x->prev = nil;
    }
    
    int main() {
        init();
        string com;
        int key, n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            cin >> com;
            switch (com[0]) {
                case 'i':
                    cin >> key;
                    insert(key);
                    break;
                case 'd':
                    if (com.length() > 6) {
                        switch (com[6]) {
                            case 'F':
                                delete_first();
                                break;
                            case 'L':
                                delete_last();
                                break;
                        }
                    } else {
                        cin >> key;
                        delete_key(key);
                    }
                    break;
            }
        }
        print_list();
    
        return 0;
    }
    
  • 相关阅读:
    未来 3-5 年信息安全行业会如何变化,目前可以观察到的变化有哪些?
    信息安全经典书籍
    Apparmor——Linux内核中的强制访问控制系统
    深入理解SELinux
    FreebuF黑客专访系列之吴翰清(刺):接下来几年,有两样东西必定会火
    Security Checklist (路由器安全checklist)
    CPU MPU MCU SOC SOPC关系及区别
    军火库(第一期):无线电硬件安全大牛都用哪些利器?
    华为V-ISA信誉安全体系:对付新型DDoS攻击的利器
    物联网兴起 嵌入式系统安全日益受关注
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338386.html
Copyright © 2011-2022 走看看