-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
46 lines (45 loc) · 1.05 KB
/
Copy pathstack.java
File metadata and controls
46 lines (45 loc) · 1.05 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
import java.util.Scanner;
class stack {
static int top = 0;
int a[] = new int[5];
void display(){
if (top <= 0)
System.out.println("Stack underflow!!");
else
{
System.out.print("The stack is :");
for(int i = top-1; i >= 0; i--)
System.out.println(a[i]);
System.out.println();
}
}
void push()
{
if (top >= 5)
System.out.println("Stack overflow");
else {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value you want in the stack");
int value = sc.nextInt();
a[top++] = value;
}
}
void pop(){
if (top <= 0)
System.out.println("Stack underflow !");
else{
top--;
}
}
public static void main(String [] args){
stack n = new stack();
n.push();
n.push();
n.push();
n.push();
n.push();
n.display();
n.pop();
n.display();
}
}