2

What is the difference between these two ways of dealing with stacks and queues? What are the both called?

First way:

import java.util.Arrays;

public class StackMethods {
   private int top;
   int size;
   int[] stack ;

    public StackMethods(int arraySize){
       size=arraySize;
       stack= new int[size];
       top=-1;
     }

    public void push(int value){
        if(top==size-1){
            System.out.println("Stack is full, can't push a value");
        }
        else{
            top=top+1;
            stack[top]=value;
           }
    }

    public void pop(){
        if(!isEmpty())
            top=top-1;
        else{
            System.out.println("Can't pop...stack is empty");
            }
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public void display(){
        for(int i=0;i<=top;i++){
            System.out.print(stack[i]+ " ");
        }
        System.out.println();
   }
}

Second way:

public class StackReviseDemo {

    public static void main(String[] args) {
        StackMethods newStack = new StackMethods(5);
        newStack.push(10);
        newStack.push(1);
        newStack.push(50);
        newStack.push(20);
        newStack.push(90);

        newStack.display();
        newStack.pop();
        newStack.pop();
        newStack.pop();
        newStack.pop();
        newStack.display();
    }
}

Also are they correct? trying to learn these well, but explanations across the internet are vague about these..

dsh
  • 12,037
  • 3
  • 33
  • 51
jim layhey
  • 91
  • 7

1 Answers1

0

I'm not 100% sure what you mean with two ways. Looking at your first code snippet, we can see that you are declaring the class StackMethods. In the second one you are instantiating an object of the class StackMethods. So all you do in the main-method of your second code snippet is to create an object which is calling the methods push(), pop() and display() you declared in the class above. You didn't actually implement two datastructures, but just a basic stack.

The good news is, over all you have grasped the concepts of stacks, since your implementation of the class 'StackMethods' is correct overall.

In regards to what the difference between a Queue and a Stack is, this question might help you:

In case this didn't answer your question and I simply misunderstood it, please just comment and let me know so I can try to help you out a little better.

Community
  • 1
  • 1
Benjamin
  • 99
  • 8
  • The question got messed-up by a formatting error, what I was trying to understand is the difference between stacks in arrays and stacks that start like this and just seem to work without creating a load of methods to push and pop... Stack s = new Stack(); // followed by Methods – jim layhey Jul 01 '15 at 19:44
  • Ah okay ! So when you call something like Stack, you use the Java Collections Framework. So some dude at Oracle already implemented the Stack for you. The Stack in that implementation is most likely based on a List, so it's not limited in size since it uses a pointer structure. It also is generic. So you can use it for basically any possible base class, not only ints like in your implementation. It supports a few more operations than your Stack. Here is the link to the Java Documentation: http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html – Benjamin Jul 01 '15 at 19:52
  • Now that isn't such an easy question to answer. In general you will be able to use the Stack from the Java Collections Framework in most cases. However, you should of course know how stacks and queues function, and implementing them yourself is the best way to learn. So I would say that there is a need if you wish to learn about the underlying principles of the datastructures. If you don't care about that and just want to get a programm running quickly, just using the implementation of the Collections Framework probably is the way to go. – Benjamin Jul 01 '15 at 20:03
  • 1
    Thanks Benjamin, This helped a lot – jim layhey Jul 01 '15 at 20:27