forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
113 lines (82 loc) · 1.18 KB
/
stack.java
File metadata and controls
113 lines (82 loc) · 1.18 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.*;
import java.util.*;
class Stack
{
public int capacity;
public int top;
public int arr[];
public Stack(int size)
{capacity=size;
arr=new int[capacity];
top=-1;
}
void push(int n )
{
if(top<capacity-1)
{
top=top+1;
arr[top]=n;
System.out.println("array aftr push");
int i;
for(i=0;i<=top;i++)
{
System.out.println(arr[i]+"");
}
System.out.println();
}
else
{
System.out.println("stack is full");
}
}
void pop()
{
if(top>=0)
{top--;
System.out.println("array after pop");
int i;
for(i=0;i<=top;i++)
{
System.out.println(arr[i]+"");
}
System.out.println();
}
else
{
System.out.println("stack is empty");
}
}
void extreme()
{
System.out.println(arr[top]);
}
public static void main(String args[])throws Exception
{Scanner sc=new Scanner(System.in);
int x;
System.out.println("enter the size of stack");
x=sc.nextInt();
Stack obj=new Stack(x);
int q;
q=0;
while(q==0)
{
System.out.println("enter 1 for push 2 for pop and 3 for extreme and 4 for exit");
int a,b;
a=sc.nextInt();
switch(a)
{
case 1:
System.out.println("enter element to be pushed");
b=sc.nextInt();
obj.push(b);
break;
case 2:obj.pop();
break;
case 3:obj.extreme();
break;
case 4:q=1;
break;
}
}
}
}