HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Three stacks in a single array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
threestacksarraysingle

Problem

I have implemented three stacks in a single array in Java. Any advice on object oriented design, coding structure, logical part or any sort of advice would be appreciated.

public class ThreeStack {
    private int[] stack;

    private int first, last, mid;
    private final int midPosition;

    public ThreeStack(int size) {
        stack = new int[size];
        first = -1; last = size; mid = (first + last) >> 1;
        midPosition = mid;
    }

    //push
    public boolean pushFirstStack(final int val) {
        if (first + 1  mid) {
            stack[--last] = val;
            return true;
        } else {
            return false;
        }
    }

    //pop
    public int popFirstStack() {
        if (first = stack.length) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[last];
            stack[last++] = 0;
            return val;
        }
    }

    //size
    public int firstStackSize() {
        return mid - midPosition;
    }

    public int secondStackSize() {
        return last - mid;
    }

    public int lastStackSize() {
        return secondStackSize();
    }

    public void printStack() {
        for (int i = 0; i < stack.length; i++) {
            System.out.print(stack[i]);
            System.out.print((i == first || i == mid || i == last) ? "*": "");
            System.out.print(" ");
        }
        System.out.println();
    }
}


Main Method

```
public class MainClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ThreeStack stack = new ThreeStack(12);

while (true) {
stack.printStack();
menu();
int c = in.nextInt();
switch (c) {
case 1:
stack.pushFirstStack(in.nextInt());
break;
case 2:
stack.pushSecondStack(in.nextInt());
break;
case 3:

Solution

Stack sizing

The 3 stacks have different size limits:

  • The 1st stack can have maximum size / 2 elements



  • The 2nd and 3rd stacks share their storage, they can have size / 2 elements



When there is a peculiar limitation like this, it's good to document it, and hopefully with an explanation of the reasoning.

An alternative approach could have been giving each stack size / 3 space.

Another alternative would be using an array of StackNode objects, where StackNode would store the payload, and a link to the StackNode above it. This way all stacks would have dynamic size, until their total use reaches the capacity of the StackNode array. However, in addition to the extra storage needed (for the links), the logic would be also more complicated to account for the freed spots in the array.

Make final what you can

This field can be final:

private int[] stack;


One statement per line

It's best when you can read code from top to bottom.
When there are multiple statements per line,
it's easy to overlook the right end.
So instead of putting multiple assignments on the same line like this:

first = -1; last = size; mid = (first + last) >> 1;


It would be better to break it up, to have one statement per line.

Clever code

This code is a bit clever:

mid = (first + last) >> 1;


It's best to keep things simple and natural:

mid = (first + last) / 2;


Sure, bit shifting is faster than division.
But it's reasonable to expect the compiler to automatically convert / 2 to use bit shifting instead of division, so that you can write simple, natural, unsurprising code. (I don't know if the compiler actually does that, but in any case, such micro-optimization is likely to be pointless pedantry anyway.)

Code Snippets

private int[] stack;
first = -1; last = size; mid = (first + last) >> 1;
mid = (first + last) >> 1;
mid = (first + last) / 2;

Context

StackExchange Code Review Q#127075, answer score: 3

Revisions (0)

No revisions yet.