Skip to content

Commit 0e6b09b

Browse files
Create ImplementStackUsingArray
A program to implement a Stack using Array. The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
1 parent 85b3b1d commit 0e6b09b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class MyStack {
2+
private int[] arr;
3+
private int top;
4+
5+
public MyStack() {
6+
arr = new int[1000];
7+
top = -1;
8+
9+
}
10+
11+
public void push(int x) {
12+
// Your Code
13+
top++;
14+
if(top<arr.length)
15+
arr[top]=x;
16+
17+
18+
}
19+
20+
public int pop() {
21+
// Your Code
22+
int ans=-1;
23+
if(top==-1)
24+
return ans;
25+
else{
26+
ans=arr[top];
27+
top--;
28+
}
29+
return ans;
30+
31+
}
32+
}

0 commit comments

Comments
 (0)