-1

Create a class called inverse_Stack where our 'stack' is organized in such a way where the first/"bottom" element is located at index (-1). Each element pushed is placed into the array slot before the [current] top-most element. if(size=0)store at index: (length -1); if(size=1), store at index:(length -2);if(size=2), store at index: (length-3);

This is all i have so far. Lost with the push pop peek methods for an inverse stack. I know how to make them work for a regular stack

public class Inverse_Stack<T> implements StackADT<T>{

private T[] stack; 
private int top;
//private int bot;

public Inverse_Stack(){

 this(100);

}

public Inverse_Stack(int capacity){

 top = 0; 

 stack = (T[] new Object[capacity];

} 

public int size(){
 //returns size of array
 return stack.length;

}

public void push(T element){
    //fill in code
}

 private void expandCapacity(){

T[] newStack = (T[] new Object[stack.length*2];

for(int i = 0; i < stack.length;i++)

newStack[i] = stack[i];

stack = newStack;
}

public T pop(){

if(isEmpty())

throw new RuntimeException("Empty Stack");

//fill in code

}

public T peek(){

if(isEmpty())

throw new RuntimeException("Empty Stack");

 //fill in code

 }
Ricky
  • 11
  • 5

3 Answers3

0

length tells you the capacity: the number of items the stack can hold. You also need to keep a count variable so you know how many items are currently in the stack.

I won't write the Java code, but I can give you the general idea:

In your constructor, set count to 0.

isEmpty returns true if count is greater than 0.

push
    if the stack is full, expand the capacity
    add element at stack[count]
    increment count

pop
    if the stack is empty, throw empty stack exception
    decrement count
    return the value at stack[count]

peek is like pop, but you don't actually decrement count.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

Is not ArrayDeque doing exactly what you want?

public class InverseStack<T> extends ArrayDeque<T> {
…
}

and toArray( T[] a ) would get Your inverse array

Kaplan
  • 2,572
  • 13
  • 14
0

this is my answer. I think this is more help you.

class StackX {

    private int maxSize; //size of stack array
    private char[] stackData;
    private int top; //top of stack
//-------------------------------------------------------------------------

    public StackX(int s) {
        this.stackData = new char[s];
        this.maxSize = s;
        this.top = -1;
    }

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

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

    public void push(char item) {
        if (isFull()) {
            System.out.println("Stack is full");
            return;
        }

        stackData[++top] = item;
    }

    public char pop() throws Exception {
        if (isEmpty()) {
            throw new Exception("Stack is empty");
        }
        return stackData[top--];

    }

    public char peek() throws Exception {
        if (isEmpty()) {
            throw new Exception("Stack is empty");

        }
        char peekValue = this.pop();
        this.push(peekValue);
        return peekValue;

    }

    public void display() {
        if (isEmpty()) {
            System.out.println("Stack is empry");
        }
        System.out.println("Start printing stack data");
        for (int i = top; i >= 0; i--) {
            System.out.println(stackData[i]);
        }
        System.out.println("End printing stack data");
    }
}
Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51