-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
62 lines (57 loc) · 1.51 KB
/
Stack.java
File metadata and controls
62 lines (57 loc) · 1.51 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 com.company.Algorithm;
import java.util.Scanner;
public class Stack {
final int max=10;
int s[]=new int[max];
int top =-1;
void push(int element){
if (top>=max-1)
System.out.println("Stack Overflow");
else
s[++top]=element;
}
int pop(){
int z=0;
if (top == -1)
System.out.println("Underflow");
else
z=s[top--];
return z;
}
void display(){
if (top==-1)
System.out.println("Stack is empty");
else {
for (int i=top;i>=0;i--)
System.out.println("Stack is --> " + s[i]);
}
}
public static void main(String[] args) {
Stack stack=new Stack();
Scanner sc=new Scanner(System.in);
int a=1;
while (a!=0)
{
System.out.println("Enter 1 for push,2 for Pop,3 for Display, 4 for Exist");
int ch=sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter the element to be inserted-->");
int e=sc.nextInt();
stack.push(e);
break;
case 2:
int p=stack.pop();
System.out.println("Element poped out --->"+p);
break;
case 3:
stack.display();
break;
case 4:
a=0;
break;
}
}
}
}