-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
80 lines (75 loc) · 1.81 KB
/
Stack.java
File metadata and controls
80 lines (75 loc) · 1.81 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
import java.util.*;
class Stack
{
Node top;
int size;
Stack()
{
top = null;
size = 0;
}
void Push(int n)
{
Node N = new Node(n);
size++;
N.next = top;
top = N;
}
void Pop()
{
if (top == null)
{
System.out.println("Stack empty");
return;
}
System.out.println("Popped element is: " + top.n);
top = top.next;
size--;
}
void Display()
{
Node head = top;
if (head == null)
System.out.print("Stack empty");
while (head != null)
{
System.out.print(head.n + "\t");
head = head.next;
}
System.out.println();
}
public static void main()
{
Scanner sc = new Scanner(System.in);
Stack S = new Stack();
int ch;
System.out.println("1. Push\n 2. Pop\n 3. Display\n 4. Display Size\n 5. Quit");
do
{
System.out.println("Make choice:");
ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter item");
int x = sc.nextInt();
S.Push(x);
break;
case 2:
S.Pop();
break;
case 3:
S.Display();
break;
case 4:
System.out.println("Size of stack is " + S.size);
break;
case 5:
System.out.println("Quitting program");
break;
default:
System.out.println("Invalid: Please choose again");
}
}while (ch != 5);
}
}