Big talk data structure seven: Two stacks of shared storage space (bidirectional stack)

Source: Internet
Author: User
Tags array length final int size integer

1. Why use bidirectional stacks?

Through the previous blog-a special linear table (stack), it is not difficult to know the stack's sequential storage (array implementation) performance is relatively good, because it does not exist when inserting and deleting the problem of moving elements, but it has a bit of a flaw: to determine the size of the array storage capacity in advance, if not enough, you need to expand the array capacity. Then the bidirectional stack comes in handy, it can maximize the use of the storage space beforehand.

2. What are the characteristics of bidirectional stacks?

The array has two endpoints, two stacks have two stack bottom, let one stack bottom of the stack to the beginning of the array, that is, subscript 0, another stack of the end of the array, that is, subscript the length of the M-1 place. In this way, two stacks, if added, extend from both ends to the middle (as shown below).

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

3. Java implementation bidirectional stack

An array of bidirectional stacks implements the 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;  
            }//Press Stack public boolean push (int stacknum, T data) {if (Top1 + 1 = top2) {//Stack full  
            System.out.println ("Stack is full, no more push elements ...");  
        return false;  
        } if (Stacknum = = Stack_1) {//operation is stack 1 ARR[++TOP1] = data;//stack 1 forward one return true; else if (stacknum = = Stack_2) {//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; }//Stack @SuppressWarnings ("unchecked") public T pop (int stacknum) {if STACKNU  
                m = = Stack_1) {//operation is stack 1 if (Top1 = = 1) {System.out.println ("Stack 1 is already empty stack ...");  
            return null; Return (T) arr[top1--];  
                Stack 1 Back one} else if (Stacknum = = stack_2) {//operation is stack 2 if (top2 = = default_size) {  
                System.out.println ("Stack 2 is already empty stack ...");  
            return null; Return (T) arr[top2++];  
            Stack 2 forward one} else {System.out.println ("Please enter the correct stack number: 1 or 2");  
        return null;  
   }//Get stack top element @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 array length public int size () {return default_size + Top1 + 1-top2; //Determine if empty stack public boolean isempty () {return THIS.TOP1 = = 1 && this.top2 = = Defaul  
    T_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 do I use two-way stacks?

1.) The two-stack space requirement has the opposite relationship when another stack is shortened when one stack grows.

2.) Two stacks need to have the same data type, which can complicate the problem if the data type is different.

Author: csdn Blog zdp072

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.