-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayImplentationOfStack.java
More file actions
62 lines (55 loc) · 1.3 KB
/
ArrayImplentationOfStack.java
File metadata and controls
62 lines (55 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package Codes.Stack;
class MyStack{
int arr[];
int capacity;
int top;
MyStack(int c){
capacity=c;
top=-1;
arr= new int [capacity];
}
void push(int x){
if(top==capacity-1)
System.out.println("Error: Stack Overflow");
top++;
arr[top]=x;
}
int pop(){
if(top==-1)
System.out.println("Error: Stack Underflow");
int res=arr[top];
top--;
return res;
}
int peek(){
if(top==-1)
System.out.println("Error: Stack Underflow");
return arr[top];
}
boolean isEmpty(){
return (top==-1);
}
int size(){
return top;
}
}
public class ArrayImplentationOfStack {
public static void main(String[] args) {
MyStack stack= new MyStack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
// System.out.println(stack.peek());
stack.pop();
// System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
System.out.println(stack.size());
// System.out.println(stack.pop());
// stack.pop();
// stack.pop();
// System.out.println(stack.peek());
}
}