Algorithms and Data Structures (3) stacks and queues

Implement stack and queue

(1) Stack

basic concepts

  1. Stack: limit the insertion end of the linear form and delete, first in last out - FILO linear form;
  2. Top of the stack (top): allows one end of the insertion and deletion, also known as the end of the table;
  3. Bottom of stack (bottom): header;
  4. Empty stack: There are no elements in the stack;
  5. PUSH: insert elements
  6. Popped: Removing elements

Basic operations: whether the air, into the stack, the stack, the top element is read, the destruction of the stack;

Stack order structure:

public class Stack {
    private int size;
    private Object[] elements;
    private int ptr = -1;

    public Stack() {
        size = 100;
        elements = new Object[100];
    }

    public Stack(int size) {
        elements = new Object[size];
        this.size = size;
    }

    public void push(Object element) throws Exception {
        if (ptr > size - 1)
            throw new Exception("stack over flow");
        elements[++ptr] = element;
    }

    public Object pop() throws Exception {
        if (ptr == -1)
            throw new Exception("empty stack");
        return elements[ptr--];
    }

    public Boolean isEmpty() {
        if (ptr == -1)
            return true;
        return false;
    }
}

Chain to achieve

public class LinkStack<T> {

    private static class Node<T>{
        private T data;
        private Node<T> nextNode;

        public Node() {
        }

        public Node(T data) {
            this.data = data;
        }
    }

    private Node<T> top;
    private int ptr = -1;

    public LinkStack() {
    }

    public LinkStack(T element) {
        top = new Node<T>(element);
    }

    public void push(T element) {
        Node<T> node = new Node<>(element);
        node.nextNode=top;
        top = node;
        ptr++;
    }

    public T pop() throws Exception {
        if (ptr == -1)
            throw new Exception("empty stack");
        ptr --;
        Node<T> popNode = top;
        top = top.nextNode;
        popNode.nextNode = null; //release ram
        return popNode.data;
    }

    public Boolean isEmpty() {
        if (ptr == -1)
            return true;
        return false;
    }
}

(2) Queue

basic concepts

  1. Queue (queue): linear limited operation table, first in first out (FIFO) linear form, allowing only one end of the table to delete, insert the other end of the table;
  2. The first team (front): Delete the end;
  3. The tail (rear): insertion end;
  4. Empty queue: elemental;

Circular queue

The queue allocated memory space as a round end to end, using head and tail pointers (called front, rear) to maintain it;

points to a front position of the first element, rear position points to the next queue tail element;

Queue is empty front = rear;

public class SequenceQueue<T> {
    private Object[] elements;
    private int size;
    private int front = 0;
    private int rear = 0;

    public SequenceQueue(int size) {
        this.size = size + 1;//因rear指向队尾的下一个位置(用于判空和判满),需要数组长度需+1;
        elements = new Object[size + 1];
    }

    public void push(T t) throws Exception {
        if (isFull())
            throw new Exception("out of size ");
        elements[rear] = t;
        rear = (rear + 1) % size;
    }

    public T pop() throws Exception {
        if (isEmpty())
            throw new Exception("empty queue");
        T reValue = (T) elements[front];
        front = (front + 1) % size;
        return reValue;
    }

    public boolean isFull() {
        return (rear + 1) % size == front ? true : false;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public int size() {
        return this.size;
    }
}

Chain to achieve

Referring stack may be slightly modified to achieve chain; to achieve a single linked list;

Published 17 original articles · won praise 0 · Views 371

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/103947914