Dahua Data Structure 7: Two Stacks Shared Storage Space (Two-way Stack)

Posted by cwiddowson on Mon, 14 Oct 2019 16:51:55 +0200

Links to the original text: http://www.cnblogs.com/riasky/p/3360826.html

1. Why use a two-way stack?

Through the last blog- Special Linear Tables (Stacks) It is not difficult to know that the stack's sequential storage (array implementation) performance is relatively high, because it does not have the problem of moving elements when inserting and deleting, but it has one drawback: in order to determine the size of array storage capacity, in case it is not enough, it needs to expand capacity. At this point, the two-way stack will come in handy, it can maximize the use of pre-opened storage space.


 

2. What are the characteristics of the two-way stack?

The array has two endpoints, two stacks have two stack bottoms, so that the bottom of one stack is the beginning of the array, that is, the subscript is 0, and the other stack is the end of the array, that is, the subscript is the length of the array m-1. In this way, if two stacks add elements, the two endpoints extend to the middle (as shown below).



3. Java implements a two-way stack

 

// Array Implementation of Bidirectional Stack
public class ShareStack<T> {
	private Object[] arr; // Array implementation stack operation
	private int top1; // Stack 1: Stack top pointer
	private int top2; // Stack 2: Stack top pointer
	private static Integer DEFAULT_SIZE = 16; // Array default length
	private static final Integer STACK_1 = 1; // Stack 1
	private static final Integer STACK_2 = 2; // Stack 2

	public ShareStack() {
		init();
	}

	public ShareStack(int size) {
		DEFAULT_SIZE = size;
		init();
	}

	// Initialization stack
	public void init() {
		arr = new Object[DEFAULT_SIZE];
		this.top1 = -1;
		this.top2 = DEFAULT_SIZE;
	}

	// Pressing stack
	public boolean push(int stackNum, T data) {
		if (top1 + 1 == top2) { // The stack is full.
			System.out.println("The stack is full.,No more push Element..");
			return false;
		}
		if (stackNum == STACK_1) { // The operation is stack 1
			arr[++top1] = data; // Stack 1 Forward Bit
			return true;
		} else if (stackNum == STACK_2) { // The operation is stack 2
			arr[--top2] = data; // Stack 2 Back One
			return true;
		} else {
			System.out.println("Please enter the correct stack number: 1 Or 2");
			return false;
		}
	}

	// Bomb stack
	@SuppressWarnings("unchecked")
	public T pop(int stackNum) {
		if (stackNum == STACK_1) { // The operation is stack 1
			if (top1 == -1) {
				System.out.println("Stack 1 is already empty...");
				return null;
			}
			return (T) arr[top1--]; // Stack 1 Back One
		} else if (stackNum == STACK_2) { // The operation is stack 2
			if (top2 == DEFAULT_SIZE) {
				System.out.println("Stack 2 is already empty...");
				return null;
			}
			return (T) arr[top2++]; // Stack 2 Forward Bit
		} else {
			System.out.println("Please enter the correct stack number: 1 Or 2");
			return null;
		}
	}

	// Get the top element of the stack
	@SuppressWarnings("unchecked")
	public T getTop(int stackNum) {
		if (stackNum == STACK_1) {
			if (this.top1 != -1) {
				return (T) arr[top1];
			}
		} else if (stackNum == STACK_2) {
			if (stackNum != DEFAULT_SIZE) {
				return (T) arr[top2];
			}
		} else {
			System.out.println("Please enter the correct stack number: 1 Or 2");
		}
		return null;
	}

	// Get the length of the array
	public int size() {
		return DEFAULT_SIZE + top1 + 1 - top2;
	}

	// Determine whether it is empty stack
	public boolean isEmpty() {
		return this.top1 == -1 && this.top2 == DEFAULT_SIZE;
	}

	// Set to empty stack
	public boolean clear() {
		this.top1 = -1;
		this.top2 = DEFAULT_SIZE;
		return true;
	}
	
	// test method
	public static void main(String[] args) throws Exception {
		ShareStack<Integer> stack = new ShareStack<Integer>();
		stack.push(1, 1);
		stack.push(2, 2);
		stack.pop(1);
		stack.pop(2);
	}
}

4. When will the two-way stack be used?

 

 

1. When the space requirements of two stacks are inversely related, that is, when one stack grows, the other stack is shortening.

2. The two stacks need to have the same data type, which will complicate the problem if the data types are different.



Reprinted at: https://www.cnblogs.com/riasky/p/3360826.html

Topics: Java