-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_1.java
More file actions
98 lines (98 loc) · 3.03 KB
/
Stack_1.java
File metadata and controls
98 lines (98 loc) · 3.03 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
import java.util.Scanner;
class StackArray
{
private final int maxsize;
private final long[] stackarray;
private int top;
public StackArray(int s)
{
maxsize = s;
stackarray = new long[maxsize];
top = -1;
}
public void push(long j) //push methond
{
stackarray[++top] = j;
}
public long pop() //pop method
{
return stackarray[top-1];
}
public void display() //displaymethod
{
System.out.println("\nStack =");
if(isempty())
{
System.out.println("Stack is Empty");
}
else
{
for(int i = top; i >= 0; i--)
System.out.println(stackarray[i]+"");
System.out.println();
}
}
public boolean isempty() //isempty method
{
return (top == -1);
}
public boolean isfull() //isfull method
{
return (top == maxsize -1 );
}
}
public class Stack_1
{
public static void main(String[] args)
{
try
{
System.out.println("Size of the Stack:"); //First ask Outputstaement
Scanner s=new Scanner(System.in);
int n = s.nextInt();
stack newStack = new stack(5);
for(;;)
{
System.out.println("Menu\n1-Push\n2-Pop\n3-Display\n4-Exit\nEnter Your Choices:"); //Menus
int ch = s.nextInt();
switch(ch)
{
case 1: //option-1
if(newStack.isfull())
{
System.out.println("Stack Is Full");
}
else
{
System.out.println("Item To be pushed");
int datastack = s.nextInt();
newStack.push(datastack);
}
break;
case 2: //option-2
if(newStack.isempty())
{
System.out.println("Stack is empty");
}
else
{
long popeddata = newStack.pop();
System.out.println(popeddata);
}
break;
case 3: //option-3
newStack.display();
break;
case 4: //option-4
break;
}
if(ch == 4)
break;
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}