Skip to content

Added java code for stack implementation using array please merge it #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Data Structures/StackImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
public class StackImpl {
int top = 0;
int MAX = 5;
int[] a = new int[MAX];
private boolean isFull() {
if(top == MAX) {
return true;
}
return false;
}
public void insert(int val) {
if(isFull()) {
System.out.println("Stack is full");
return;
}

a[top++] = val;
}

private boolean isEmpty() {
if(top == 0) {
return true;
}

return false;
}

public int remove() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return Integer.MIN_VALUE;
}

return a[--top];
}

public int top() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return Integer.MIN_VALUE;
}

return a[top - 1];
}
public static void main(String[] args) {
StackImpl a = new StackImpl();
a.insert(12);
a.insert(5);
a.insert(34);
a.insert(42);
a.insert(91);
a.insert(9);

System.out.println(a.remove());
a.insert(9);

System.out.println("Top:" + a.top());

System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
}
}