Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Please review the code

package com.gmail.practice;

import java.util.Arrays;

public class StacksForTwo {

    int size;
    int[] stack;
    int top1;
    int top2;

    public StacksForTwo(int arraysize)
    {
        size = arraysize;
        stack = new int[size];
        top1 = -1;
        top2 = size;        
    }

    public void push1(int x)
    {
        if(top1 < top2-1)
        {
        top1++;
        stack[top1] = x;        
        }else{
            System.out.println("stackoverflow");
        }
    }

    public void push2(int y)
    {
        if(top1 < top2-1)
        {
            top2--;
            stack[top2] = y;
        }else{
            System.out.println("stack overflow");
        }
    }

    public void pop1()
    {
        if(top1 >= 0)
        {
            top1--;
            System.out.println("The popped out number is"+" "+stack[top1+1]);
        }else{
            System.out.println("stack underflow");
        }
    }

    public void pop2()
    {
        if(top2 < size)
        {
            top2++;
            System.out.println("The popped out number is"+" "+stack[top2+1]);
        }else{
            System.out.println("stack underflow");
        }
    }

    public void display()
    {
        System.out.println(Arrays.toString(stack));
    }

    public static void main(String[] args)
    {
        StacksForTwo sft = new StacksForTwo(10);
        sft.push1(4);
        sft.push1(5);
        sft.push1(3);
        sft.push1(2);
        sft.push2(6);
        sft.push2(4);
        sft.display();
        sft.push2(8);
        sft.push1(2);
        sft.push2(6);
        sft.push2(4);
        sft.push2(8);
        sft.display();
    }
}
share|improve this question
    
Could you please elaborate. Depending on your inputs i will try more things. –  dataEnthusiast yesterday

2 Answers 2

This code is a WOM implementation of a stack - Write Only Memory. Normally it's done as a joke. The only way to get the data out of the stack is to parse the standard output waiting for println statements with the values in them.

I can understand that your code is here for you to watch the process happening, but beyond that there's not much real functionality in here. I encourage you to use proper code where the pop() methods actually return their result, and the calling code is the code that prints the output. Alternatively, I strongly recommend you use the IDE's debugger interface to step through your code so you can watch things happen that way.

Having said all that, here are some general comments:

  1. your instance variables are not private, and should be.
  2. the size and stack variables should also be final
  3. instead of having a display() method, just override the toString()
  4. push1, and pop1 et all should probably be renamed to pushLeft and pushRight, or really anythong other than 1 and 2. You used the variables x and y so why not pushX and pushY?
share|improve this answer
    
Fields are default scope and can be accessed from classes in same package, anyway its pretty WOM. If he follow your suggestions its perfectly WOM. –  Peter Rader 20 hours ago
    
thanks for the inputs i will redo the code. –  dataEnthusiast 18 hours ago

I would go for

interface Stack {
    boolean isEmpty();
    int pop();
    void push(int x);
}

And then make a class providing two Stacks.

Also create a counter to detect when both stacks are full. This can be done with an AtomicInteger (thread-safeness) counting the free array slots.

public class StackPair {
    public final Stack firstStack = new Stack { ... };
    public final Stack secondStack = new Stack { ... };
    public StackPair(int capacity) { ... }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.