Created
October 24, 2019 08:42
-
-
Save hocyadav/b3fc4912989f7ea09766e7e54eee1372 to your computer and use it in GitHub Desktop.
Stack implementation using array
This file contains 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 datastructure; | |
/** | |
* | |
* @author yadha02 | |
* | |
*/ | |
public class Stack_ArrayImplementation { | |
//declare global variable | |
static int stackSize = 11; | |
static int[] stack = new int[stackSize]; | |
static int top = -1; | |
static void push(int x){//increment TOP and add && check overflow condition | |
if(top == stackSize-1) { | |
System.out.println("STACK : overflow - no space left"); | |
return; | |
} | |
stack[++top] = x; | |
} | |
static void pop(){//here pop implementaion : only decrement top | |
if(top == -1) { | |
System.out.println("STACK : underflow - no element to pop"); | |
return; | |
} | |
top--; | |
} | |
int top(){//get top element && return only if not empty | |
return stack[top]; | |
} | |
static void print(){ | |
System.out.print("STACK : "); | |
for(int i=0; i<=top; i++) { | |
System.out.print(" "+stack[i]); | |
} | |
System.out.println(""); | |
} | |
static boolean isEmpty(){ | |
return (top == -1) ? true : false; | |
} | |
public static void main(String[] args) { | |
System.out.println(isEmpty()); | |
pop(); | |
push(1); print(); | |
push(3); print(); | |
push(6); print(); | |
pop(); print(); | |
push(11); print(); | |
push(14); print(); | |
push(155); print(); | |
push(13); print(); | |
System.out.println(isEmpty()); | |
push(17); print(); | |
push(123); print(); | |
push(19); print(); | |
push(144); print(); | |
push(15); print();//overflow - only work we we change value stackSize to 11 | |
System.out.println(isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment