简单的数组模拟链表、栈和队列

70 阅读2分钟

1. 数组模拟单链表

题目:www.acwing.com/problem/con…

public class num826 {

    private static int N = 100010;

    private static int head;
    private static int[] e = new int[N];
    private static int[] ne = new int[N];
    private static int idx;

    private static void init() {
        head = -1;
        idx = 0;
    }

    private static void addHead(int x) {
        ne[idx] = head;
        head = idx;
        e[idx++] = x;
    }

    private static void addElement(int k, int x) {
        e[idx] = x;
        ne[idx] = ne[k - 1];
        ne[k - 1] = idx;
        ++idx;
    }

    private static void delete(int k) {
        if (k == 0) {
            head = ne[head];
        } else {
            ne[k - 1] = ne[ne[k - 1]];
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(reader.readLine());
        init();
        while (m-- > 0) {
            String[] s = reader.readLine().split(" ");
            int k = Integer.parseInt(s[1]);
            if (s[0].equals("H")) {
                addHead(k);
            } else if (s[0].equals("I")) {
                int x = Integer.parseInt(s[2]);
                addElement(k, x);
            } else {
                delete(k);
            }
        }
        for (int i = head; i != -1; i = ne[i]) {
            System.out.print(e[i] + " ");
        }
    }
}

2. 数组模拟双链表

题目:www.acwing.com/problem/con…

public class num827 {

    private static int N = 100010;

    private static int head;
    private static int tail;
    private static int idx;
    private static int[] e = new int[N];
    private static int[] l = new int[N];
    private static int[] r = new int[N];

    public static void init() {
        head = -1;
        tail = -1;
        idx = 0;
    }

    public static void addRight(int x) {
        e[idx] = x;
        if (tail == -1) {
            head = tail = idx;
            r[idx] = -1;
            l[idx] = -1;
        } else {
            r[tail] = idx;
            l[idx] = tail;
            r[idx] = -1;
            tail = idx;
        }
        ++idx;
    }

    public static void addLeft(int x) {
        e[idx] = x;
        if (head == -1) {
            head = tail = idx;
            r[idx] = -1;
            l[idx] = -1;
        } else {
            l[head] = idx;
            r[idx] = head;
            l[idx] = -1;
            head = idx;
        }
        ++idx;
    }

    public static void addIRight(int k, int x) {
        e[idx] = x;
        if (r[k] != -1) {
            l[r[k]] = idx;
        }
        r[idx] = r[k];
        r[k] = idx;
        l[idx] = k;
        if (r[idx] == -1) {
            tail = idx;
        }
        ++idx;
    }

    public static void addILeft(int k, int x) {
        e[idx] = x;
        if (l[k] != -1) {
            r[l[k]] = idx;
        }
        l[idx] = l[k];
        l[k] = idx;
        r[idx] = k;
        if (l[idx] == -1) {
            head = idx;
        }
        ++idx;
    }

    public static void delete(int k) {
        if (head == k) {
            head = r[k];
            if (head == -1) {
                tail = -1;
                return;
            }
            l[r[k]] = -1;
        } else if (tail == k) {
            tail = l[k];
            if (tail == -1) {
                head = -1;
                return;
            }
            r[l[k]] = -1;
        } else {
            r[l[k]] = r[k];
            l[r[k]] = l[k];
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(reader.readLine());
        init();
        while (m-- > 0) {
            String[] s = reader.readLine().split(" ");
            int k = Integer.parseInt(s[1]);
            switch (s[0]) {
                case "R":
                    addRight(k);
                    break;
                case "L":
                    addLeft(k);
                    break;
                case "D":
                    delete(k - 1);
                    break;
                case "IL": {
                    int x = Integer.parseInt(s[2]);
                    addILeft(k - 1, x);
                    break;
                }
                default: {
                    int x = Integer.parseInt(s[2]);
                    addIRight(k - 1, x);
                    break;
                }
            }
        }
        for (int i = head; i != -1; i = r[i]) {
            System.out.print(e[i] + " ");
        }
    }
}

3. 数组模拟栈

题目:www.acwing.com/problem/con…

public class num828 {

    private static int N = 100010;

    private static int[] stack = new int[N];
    private static int head;

    private static void init() {
        head = -1;
    }

    private static void push(int x) {
        ++head;
        stack[head] = x;
    }

    private static int query() {
        return stack[head];
    }

    private static boolean empty() {
        return head == -1;
    }

    private static int pop() {
        return stack[head--];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(reader.readLine());
        init();
        while (m-- > 0) {
            String[] s = reader.readLine().split(" ");
            switch (s[0]) {
                case "push":
                    push(Integer.parseInt(s[1]));
                    break;
                case "query":
                    System.out.println(query());
                    break;
                case "empty":
                    System.out.println(empty() ? "YES" : "NO");
                    break;
                default:
                    pop();
            }
        }

    }
}

4. 数组模拟队列

题目:www.acwing.com/problem/con…

public class num829 {

    private static int N = 100010;

    private static int[] queue = new int[N];
    private static int head;
    private static int tail;

    private static void init() {
        head = 0;
        tail = 0;
    }

    private static void push(int x) {
        queue[tail++] = x;
    }

    private static int query() {
        return queue[head];
    }

    private static boolean empty() {
        return head == tail;
    }

    private static int pop() {
        return queue[head++];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(reader.readLine());
        init();
        while (m-- > 0) {
            String[] s = reader.readLine().split(" ");
            switch (s[0]) {
                case "push":
                    push(Integer.parseInt(s[1]));
                    break;
                case "query":
                    System.out.println(query());
                    break;
                case "empty":
                    System.out.println(empty() ? "YES" : "NO");
                    break;
                default:
                    pop();
            }
        }

    }
}