-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
73 lines (73 loc) · 1.7 KB
/
Stack.java
File metadata and controls
73 lines (73 loc) · 1.7 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
import java.util.Scanner;
class Stack
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the stack");
int s=sc.nextInt();
Stack obj=new Stack(s);
System.out.println("\n 1.push \t 2. pop \t 3.display \t 4. Exit");
boolean flag=true;
while(flag)
{
System.out.println("\nEnter your choice:");
int c=sc.nextInt();
switch(c)
{
case 1:
System.out.println("Enter an element for insertion");
int x=sc.nextInt();
obj.push(x);
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
case 4:
flag=false;
break;
default:
System.out.println("Please enter a valid choice");
break;
}
}
}
int a[],top,size;
Stack()
{
size=0;
}
Stack(int i)
{
size=i;
a=new int[size];
top=-1;
}
void push(int j)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
a[++top]=j;
}
void pop()
{
int k;
if(top==-1)
System.out.println("Stack Underflow");
else
{
k=a[top--];
System.out.println("Element deleted is="+k);
}
}
void display()
{
System.out.println("Elements of the stack are as follows:\n");
for(int i=0;i<=top;i++)
System.out.print(a[i]+" ");
}
}