-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
88 lines (84 loc) · 1.63 KB
/
stack.java
File metadata and controls
88 lines (84 loc) · 1.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
class Stack
{
private int arr[];
private int top;
private int capacity;
Stack(int size)
{
capacity=size;
top=-1;
arr=new int[size];
}
void push(int x)
{
if(top==capacity-1)
{
System.out.println("Stack is Full");
}
else
{
System.out.println("Push Element: "+x);
arr[++top]=x;}
}
int pop()
{
if(top==-1)
{
System.out.println("Stack is Empty");
//exit();
return 0;
}
return arr[top--];
}
int getSize()
{
return top+1;
}
void peek()
{
if(top==-1)
{
System.out.println("Stack is Empty");
}
else
System.out.println("Peek Element"+arr[top]);
}
void display()
{
for(int i=top;i>=0;i--)
{
System.out.println("Stack: "+arr[i]);
}
}
}
public class Main {
public static void main(String args[])
{
Stack obj= new Stack(5);
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
//obj.display();
obj.pop();
obj.pop();
obj.display();
obj.peek();
System.out.println("Stack Size: "+obj.getSize());
obj.push(6);
obj.push(7);
obj.push(8);
obj.display();
obj.pop();
obj.pop();
obj.pop();
obj.pop();
obj.display();
obj.pop();
obj.display();
obj.pop();
obj.peek();
}
}