# 3 stacks of fixed partitioning using arrays (Java)

Source: Internet
Author: User

From the programmer's interview of the Golden Code

The first thing we can think of is the fixed segmentation method: Divide the array into 3 points, each of which can be viewed as a stack, and then perform the operation of the stack on each stack.

The advantages of this method are relatively straightforward and simple to implement. Of course, the disadvantage is not flexible enough, there may be a stack of space overflow situation, and the other stack is empty state.

The following is the implementation of the idea, first for the length of the array of n, we divide the standard as follows (assuming array subscript starting from 0):

1) Stack 1, using [0, N/3)

1) Stack 2, using [N/3, 2N/3)

1) Stack 3, using [2N/3, N)

To control the range of activity for each stack in the array, we set the constant stacksize to represent the size of each stack and set 3 constants (Stackpointer, which are inappropriate with stacktop naming) to record the position of the top of the stack,

Assuming n = 300, then stacksize = 100, and each stackpointer element has a range of 1 to 99 (where-1 indicates that the stack is empty and the 0~99 represents the stack's effective interval). Due to

The operation of the stack into the stack is on an array, so we must be able to get a different stack of stack tops in the array position. Suppose we use stacknum (Stacknum = 0,1,2) to indicate which stack

, the stack top position of the stacknum stack should be stacksize * stacknum + stackpointer[stacknum]. Due to the need to use this operation frequently, we put it in a separate

Method of Topofstack. The buffer in the code represents the array.

`int stackSize = +; int [] Stackpointer = {-1,-1,-1};int[] buffer = new int[stacksize * 3];`
`/* returns the top element of the stack "Stacknum", indexed in the array */ int topofstack (int  stacknum) {     return stackSize * stacknum +  Stackpointer[stacknum];  }`

Next is the operation of the stack, where we provide access to the stack, the implementation of the top element of the stack (assuming that the array is an array of integers).

1) into the stack operation, enter the elements into the stack and the stack number with the operation

`void push (intint  value) {    if(Stackpointer[stacknum] + 1 >=  stackSize)          return;       // stack top pointer self-increment, then update stack top element    stackpointer[stacknum]++;     = value;}`

2) out of stack operation

`int pop (intthrows  exception{     if(stackpointer[stacknum] = =-1)          Throw New Exception ("Stack is empty, cannot be out of stack");      // gets the element at the top of the stack and zeros the top position of the stack while the pointer is self-reducing     int value = buffer[topofstack (stacknum)];      = 0;      --;      return value;}`

3) get stack top element

`int Top (intthrows  exception{     if(stackpointer[stacknum] = =-1)          Throw New Exception ("Stack is empty, unable to get stack top element")     ; return Buffer[topofstack (Stacknum)];}`

The complete code looks like this:

` Public classStackbyarray {intstackSize = 100; int[] Stackpointer = {-1,-1,-1}; int[] buffer =New int[StackSize * 3]; voidPushintStacknum,intvalue) {        if(Stackpointer[stacknum] + 1 >=stackSize)return; //stack top pointer self-increment, then update stack top elementstackpointer[stacknum]++; Buffer[topofstack (stacknum)]=value; }        intPopintStacknum)throwsexception{if(Stackpointer[stacknum] = =-1)             Throw NewException ("Stack is empty, cannot be out of stack"); //gets the element at the top of the stack and zeros the top position of the stack while the pointer is self-reducing         intValue =Buffer[topofstack (Stacknum)]; Buffer[topofstack (stacknum)]= 0; Stackpointer[stacknum]--; returnvalue; }        intTopintStacknum)throwsexception{if(Stackpointer[stacknum] = =-1)             Throw NewException ("Stack is empty, unable to get stack top element"); returnBuffer[topofstack (Stacknum)]; }        /*returns the top element of the stack "Stacknum", indexed in the array*/    intTopofstack (intstacknum) {         returnStackSize * Stacknum +Stackpointer[stacknum]; }     Public Static voidMain (string[] args) {Stackbyarray stack=NewStackbyarray (); Stack.push (0, 1); Stack.push (0, 2); Stack.push (1, 3); Stack.push (1, 4); Stack.push (2, 11); Try{System.out.println (Stack.top (2)); Stack.pop (2); Stack.top (2); } Catch(Exception e) {e.printstacktrace (); }    }}`
View Code

3 stacks of fixed partitioning using arrays (Java)

Related Keywords:
Related Article

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.