Stack Getting the topic of data structures and algorithms

Stack topic of data structures and algorithms


table of Contents

  1. Implemented using fixed-size array of stacks and queues

  2. Achieve a particular stack, the stack implemented on the basis of the function, then the return stack to achieve the minimum operating element

  3. If only stack structure queue structure and how only queue structure to achieve stack structure


1. To achieve a fixed size queue and stack arrays with

(A) an array of fixed size stack implemented ideas
  1. Creating a variable size to an array position 0
  2. When performing push operation applied to the added size refers num position and size ++
  3. When the peek operation performed on only need to return the number of size-1 position.
  4. When the pop operation performed, returns the number to the --size.
(B) fixing the array to realize the idea of ​​the size of the queue
  1. Create three variable size, start, end points to an array 0 position. Wherein the recording elements of the queue size, start implementation of the first element of the queue, the next end point to a position of elements may be added
  2. When performing push operation, size ++, adding elements to the position of the end point. End is then judged whether the overflow, i.e., the length of the array 3, end gets executed when the fourth position, the end is set to 0, i.e.,end = end == arr.length - 1 ? 0 : end + 1;
  3. When performing pop operation, size-, temporary variables stored ready queue head element to be returned and determines whether or not a position beyond the last start + 1, is set to more than 0, i.e. start = start == arr.length - 1 ? 0 : start + 1;, returns a temporary variable.
  4. When performing peek operation returns to start point of the element.
(C) code for
package class_03;


public class Array_To_Stack_Queue {

    public static class ArrayStack {
        private Integer[] arr;
        private Integer size;

        public ArrayStack(int initSize) {
            if (initSize < 0) {
                throw new IllegalArgumentException("The init size is less than 0");
            }
            arr = new Integer[initSize];
            size = 0;
        }


        public void push(int obj) {
            if (size == arr.length) {
                throw new ArrayIndexOutOfBoundsException("The queue is full");
            }
            arr[size++] = obj;
        }

        public Integer peek() {
            if (size == 0) {
                throw new ArrayIndexOutOfBoundsException("The queue is null");
            }
            return arr[size - 1];
        }

        public Integer pop() {
            if (size == 0) {
                throw new ArrayIndexOutOfBoundsException("The queue is null");
            }
            return arr[--size];
        }


    }

    public static class ArrayQueue {
        private Integer start;
        private Integer end;
        private Integer size;
        private Integer[] arr;

        public ArrayQueue(int initSize) {
            if (initSize < 0) {
                System.out.println("initSize不能小于0");
                return;
            }
            start = 0;
            end = 0;
            size = 0;
            arr = new Integer[initSize];
        }

        public void push(int num) {
            if (size == arr.length) {
                System.out.println("队列满");
            }
            size++;
            arr[end] = num;
            end = end == arr.length - 1 ? 0 : end + 1;
        }

        public Integer pop() {
            if (size == 0) {
                return null;
            }
            size--;
            int temp = arr[start];
            start = start == arr.length - 1 ? 0 : start + 1;
            return temp;
        }

        public Integer peek() {
            if (size == 0) {
                return null;
            }
            return arr[start];
        }
    }

    public static void main(String[] args) {

    }
}

2. Implement a special stack, the stack implemented on the basis of the function, then the return stack to achieve the minimum operating element

(A) analysis of ideas

For example to myStack2

  1. Preparing two stacks, one for adding data stackData, further used to store a minimum stackMin
  2. When performing push operation, if the minimum stack is null, it is added directly to the minimum stack element is added, or peek the minimum stack elements and additive elements as compared to the hours when the additive element added to stackMin, or added peek out element.
  3. When the pop operation performed directly stackMin pop the top element and stackData
(B) code for
public class GetMinStack {
    public static class MyStack1 {
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack1() {
            this.stackData = new Stack<Integer>();
            this.stackMin = new Stack<Integer>();
        }

        public void push(int newNum) {
            if (this.stackMin.isEmpty()) {
                this.stackMin.push(newNum);
            } else if (newNum <= this.getmin()) {
                this.stackMin.push(newNum);
            }
            this.stackData.push(newNum);
        }

        public int pop() {
            if (this.stackData.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            int value = this.stackData.pop();
            if (value == this.getmin()) {
                this.stackMin.pop();
            }
            return value;
        }

        public int getmin() {
            if (this.stackMin.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            return this.stackMin.peek();
        }
    }

    public static class MyStack2 {
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack2() {
            this.stackData = new Stack<Integer>();
            this.stackMin = new Stack<Integer>();
        }

        public void push(int newNum) {
            if (this.stackMin.isEmpty()) {
                this.stackMin.push(newNum);
            } else if (newNum < this.getmin()) {
                this.stackMin.push(newNum);
            } else {
                int newMin = this.stackMin.peek();
                this.stackMin.push(newMin);
            }
            this.stackData.push(newNum);
        }

        public int pop() {
            if (this.stackData.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            this.stackMin.pop();
            return this.stackData.pop();
        }

        public int getmin() {
            if (this.stackMin.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            return this.stackMin.peek();
        }
    }

    public static void main(String[] args) {
        MyStack1 stack1 = new MyStack1();
        stack1.push(3);
        System.out.println(stack1.getmin());
        stack1.push(4);
        System.out.println(stack1.getmin());
        stack1.push(1);
        System.out.println(stack1.getmin());
        System.out.println(stack1.pop());
        System.out.println(stack1.getmin());

        System.out.println("=============");

        MyStack1 stack2 = new MyStack1();
        stack2.push(3);
        System.out.println(stack2.getmin());
        stack2.push(4);
        System.out.println(stack2.getmin());
        stack2.push(1);
        System.out.println(stack2.getmin());
        System.out.println(stack2.pop());
        System.out.println(stack2.getmin());
    }
}


3. If only stack structure queue structure and how only queue structure to achieve stack structure

Ideas
(a) queue structure with a stack structure

  1. By the queue FIFO, LIFO stack know. We can implement a queue using two stacks. Create two stacks, respectively stackPush (used to add elements), stackPop (for pop elements)
  2. When performing push operation, add the elements directly to the stack stackPush.
  3. When performing poll, peek operation, when stackPop is null, and stackPush is not null, all the elements added to the stackPush stackPop, and then returns to the top element.

(B) to achieve a stack structure with the queue structure
4 by a queue FIFO, LIFO stack know. We can achieve stacks with two queues. Creating two queues are queue, help, queue used to store queue elements, help to assist.
5. When performing push operation, the queue can be directly added to the queue.
6. When performing pop, when the peek operation, the queue element and to help in the queue stack, a queue is the only data to be returned.

(C) code for


import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Code_03_StackAndQueueConvert {

	public static class TwoStacksQueue {
		private Stack<Integer> stackPush;
		private Stack<Integer> stackPop;

		public TwoStacksQueue() {
			stackPush = new Stack<Integer>();
			stackPop = new Stack<Integer>();
		}

		public void push(int pushInt) {
			stackPush.push(pushInt);
		}

		public int poll() {
			if (stackPop.empty() && stackPush.empty()) {
				throw new RuntimeException("Queue is empty!");
			} else if (stackPop.empty()) {
				while (!stackPush.empty()) {
					stackPop.push(stackPush.pop());
				}
			}
			return stackPop.pop();
		}

		public int peek() {
			if (stackPop.empty() && stackPush.empty()) {
				throw new RuntimeException("Queue is empty!");
			} else if (stackPop.empty()) {
				while (!stackPush.empty()) {
					stackPop.push(stackPush.pop());
				}
			}
			return stackPop.peek();
		}
	}

	public static class TwoQueuesStack {
		private Queue<Integer> queue;
		private Queue<Integer> help;

		public TwoQueuesStack() {
			queue = new LinkedList<Integer>();
			help = new LinkedList<Integer>();
		}

		public void push(int pushInt) {
			queue.add(pushInt);
		}

		public int peek() {
			if (queue.isEmpty()) {
				throw new RuntimeException("Stack is empty!");
			}
			while (queue.size() != 1) {
				help.add(queue.poll());
			}
			int res = queue.poll();
			help.add(res);
			swap();
			return res;
		}

		public int pop() {
			if (queue.isEmpty()) {
				throw new RuntimeException("Stack is empty!");
			}
			while (queue.size() > 1) {
				help.add(queue.poll());
			}
			int res = queue.poll();
			swap();
			return res;
		}

		private void swap() {
			Queue<Integer> tmp = help;
			help = queue;
			queue = tmp;
		}

	}

}

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/93717864