forked from AlbinoB/Data_Structures_With_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackApplication.java
More file actions
81 lines (60 loc) · 1.67 KB
/
Copy pathStackApplication.java
File metadata and controls
81 lines (60 loc) · 1.67 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
package stack;
import java.util.Scanner;
/*
*
* The stack is implemented to be as generic as possible
* One can create the stack to be of any type.
* Follow the examples given below to match your use case.
*
* */
public class StackApplication {
public static void main(String[] args) {
StackOperations<String> s=new StackOperations<String>();//<Specifiy the data type of the stack >
Scanner scanner=new Scanner(System.in);
int choice=-1;
String data=new String("");//data type must match the stack data type
//Integer data=new Integer(0);//eg 1.
//Float data=new Float(0.0);//eg 2.
do {
System.out.println("\n\n\n1.Peek");
System.out.println("2.Pop");
System.out.println("3.Push");
System.out.println("4.Display full stack \n:");
choice=scanner.nextInt();
switch(choice) {
case 1:{
data=s.peek();
if(data==null)//stack is empty
System.out.println("Stack is empty");
else
System.out.println("Top element is: "+data);
break;
}
case 2:{
data=s.pop();
if(data==null)//stack is empty
System.out.println("Stack is empty");
else
System.out.println("Element popped: "+data);
break;
}
case 3:{
System.out.print("Enter element to be pushed:");
data=scanner.next();//change this depending on the type accepted for the stack type
//data=scanner.next() ->String
//data=scanner.nextInt() ->Integer
//data=scanner.nextFloat() ->Float
s.push(data);
break;
}
case 4:{
s.displayStack();
break;
}
default:{
System.out.println("Invalid choice!");
}
}
}while(choice!=0);
}
}