Created
June 19, 2015 10:38
-
-
Save AnimeshShaw/b94527fc5b5350b52bbb to your computer and use it in GitHub Desktop.
JStack Implementation in Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.codehackersblog.stack; | |
| import java.util.Scanner; | |
| /** | |
| * | |
| * @author Psycho_Coder | |
| */ | |
| public class JStackArray implements StackInterface { | |
| private final static int DEFAULT_SIZE = 5; | |
| private final int[] stack; | |
| private int top; | |
| private final int SIZE; | |
| private static JStack st; | |
| private static int NO_OF_ELEMENTS; | |
| public JStackArray() { | |
| SIZE = DEFAULT_SIZE; | |
| stack = new int; | |
| top = -1; | |
| NO_OF_ELEMENTS = 0; | |
| } | |
| public JStackArray(int SIZE) { | |
| this.SIZE = SIZE; | |
| stack = new int; | |
| top=-1; | |
| NO_OF_ELEMENTS = 0; | |
| } | |
| @Override | |
| public void push(int item) { | |
| if (!isFull()) { | |
| stack[++top] = item; | |
| ++NO_OF_ELEMENTS; | |
| } else { | |
| out("Stack Overflow! Please remove some elements from stack."); | |
| } | |
| } | |
| @Override | |
| public int pop() { | |
| int k = -1; | |
| if (!isEmpty()) { | |
| k = stack[top]; | |
| --top; | |
| --NO_OF_ELEMENTS; | |
| } | |
| return k; | |
| } | |
| @Override | |
| public boolean isEmpty() { | |
| return top == -1; | |
| } | |
| @Override | |
| public boolean isFull() { | |
| return top == SIZE; | |
| } | |
| @Override | |
| public int size() { | |
| return stack.length; | |
| } | |
| @Override | |
| public int peek() { | |
| return stack[top]; | |
| } | |
| private void display() { | |
| out("The elements in the stack are : "); | |
| for (int i = NO_OF_ELEMENTS-1; i >= 0; i--) { | |
| System.out.print(stack[i] + " "); | |
| } | |
| out("\n"); | |
| } | |
| public static void out(String data) { | |
| System.out.println(data); | |
| } | |
| public static void main(String[] args) { | |
| int item, choice; | |
| boolean conti = true; | |
| Scanner sc = new Scanner(System.in); | |
| if (args.length == 1) { | |
| st = new JStackArray(Integer.parseInt(args[0])); | |
| } else { | |
| st = new JStackArray(); | |
| } | |
| while (conti) { | |
| out("\nMenu"); | |
| out("1. Push"); | |
| out("2. Pop"); | |
| out("3. Peek"); | |
| out("4. Size"); | |
| out("5. Display"); | |
| out("6. Exit"); | |
| out("Enter your choice : "); | |
| choice = sc.nextInt(); | |
| switch (choice) { | |
| case 1: | |
| out("Enter the item :"); | |
| item = sc.nextInt(); | |
| st.push(item); | |
| break; | |
| case 2: | |
| int k = st.pop(); | |
| out(k == -1 ? "Stack Underflow! Please insert some elements into stack." : ("Element Poped" + k)); | |
| break; | |
| case 3: | |
| out("Top element :" + st.peek()); | |
| break; | |
| case 4: | |
| out("Size of the Stack : " + st.size()); | |
| break; | |
| case 5: | |
| st.display(); | |
| break; | |
| case 6: | |
| conti = false; | |
| break; | |
| default: | |
| out("Wrong Choice. Please try agian!"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment