- Java
- Collections Data Structure
- Stack
Stack data structure

publicclass MyStack { privateint maxSize; privatelong[] stackArray; privateint top; public MyStack(int s) { maxSize = s; stackArray = newlong[maxSize]; top = -1; } publicvoid push(long j) { stackArray[++top] = j; } publiclong pop() { return stackArray[top--]; } publiclong peek() { return stackArray[top]; } publicboolean isEmpty() { return (top == -1); } publicboolean isFull() { return (top == maxSize - 1); } publicstaticvoid main(String[] args) { MyStack theStack = new MyStack(10); // make new stack theStack.push(20); theStack.push(40); theStack.push(60); theStack.push(80); while (!theStack.isEmpty()) { long value = theStack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } }
Related examples in the same category