Created
February 11, 2021 00:51
-
-
Save laxmankumar2000/da65bf6f81f0ab09da8bce4d423c476a to your computer and use it in GitHub Desktop.
STACK USint 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 Stack; | |
import java.nio.file.StandardOpenOption; | |
class Array_Stcak { | |
int arr[]; | |
int TopOfStack; | |
public Array_Stcak(int size) | |
{ | |
arr = new int[size]; | |
TopOfStack = -1; | |
} | |
public boolean isEmpty() | |
{ | |
if (TopOfStack == -1) | |
return true; | |
else | |
return false; | |
} | |
public boolean isStackfull() | |
{ | |
if (TopOfStack==arr.length-1) | |
return true; | |
else | |
return false; | |
} | |
public void push(int value) | |
{ | |
if (isStackfull()) | |
System.out.println("Stack is fuul"); | |
else | |
{ | |
arr[++TopOfStack] = value; | |
} | |
} | |
// public void pop() | |
// { | |
// if (isEmpty()) | |
// { | |
// System.out.println("stack is Empty"); | |
// } | |
// else | |
// { | |
// TopOfStack--; | |
// } | |
// } | |
public int pop() | |
{ | |
if (isEmpty()) | |
{ | |
return -1; | |
} | |
else | |
{ | |
return arr[TopOfStack--]; | |
} | |
} | |
public void peek() | |
{ | |
if (isEmpty()) | |
{ | |
System.out.println("Stack is Empty"); | |
} | |
else | |
System.out.println(arr[TopOfStack]); | |
} | |
public void DeleteStack() | |
{ | |
arr = null; | |
} | |
public void Display() | |
{ | |
for ( int i = 0 ; i<arr.length; i++) | |
{ | |
System.out.println(arr[i]); | |
} | |
} | |
} | |
class Main | |
{ | |
public static void main(String[] args) { | |
Array_Stcak obj = new Array_Stcak(5); | |
obj.push(11); | |
obj.push(12); | |
obj.push(13); | |
obj.push(14); | |
obj.push(15); | |
System.out.println("peek"); | |
obj.peek(); | |
System.out.println("After peek"); | |
obj.Display(); | |
System.out.println("pop"); | |
System.out.println("poppp" + obj.pop()); | |
obj.Display(); | |
System.out.println("dis"); | |
obj.Display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment