Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javatechie.ds;
- public class Stack {
- int[] stack = new int[5];
- int top = 0;
- public void push(int element) {
- stack[top] = element;
- top++;
- }
- public int pop() {
- top--;
- int element = stack[top];
- stack[top] = 0;
- return element;
- }
- public int peek() {
- return stack[top - 1];
- }
- public int size() {
- return top;
- }
- public boolean isEmpty() {
- return top == 0 ? true : false;
- }
- public static void main(String[] args) {
- Stack stack = new Stack();
- stack.push(12);
- stack.push(5);
- stack.push(7);
- stack.push(8);
- stack.push(4);
- System.out.println("Element : " + stack.pop());
- System.out.println("peek() : " + stack.peek());
- System.out.println("Size : " + stack.size());
- System.out.println("isEmpty : " + stack.isEmpty());
- }
- }
Add Comment
Please, Sign In to add comment