-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
101 lines (92 loc) · 1.89 KB
/
main.dart
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
99
100
101
import 'dart:io';
// function to read input
dynamic readFunc(datatype) {
String? _input = stdin.readLineSync();
if (_input != null) {
switch (datatype) {
case int:
int result = int.parse(_input);
return result;
case double:
double result = double.parse(_input);
return result;
case String:
String result = _input;
return result;
default:
break;
}
}
}
// class for the stack
class Stack {
List<dynamic> stack = []; // initialised empty stack
int? capacity;
int top = -1;
bool isFull() {
bool result;
if (capacity == top) {
result = true;
} else {
result = false;
}
return result;
}
bool isEmpty() {
bool result;
if (top == -1) {
result = true;
} else {
result = false;
}
return result;
}
void push(dynamic data) {
if (isFull()) {
print("Stack Overflow");
} else {
top++;
stack.add(data);
print("$data added at the top of the stack");
}
}
void pop() {
if (isEmpty()) {
print("Stack Underflow");
} else {
print("The popped element is ${stack[top]}");
stack.removeAt(top);
top--;
}
}
void printStack() {
for (var i in stack) {
stdout.write("$i<-");
}
}
}
void main() {
Stack node = new Stack();
while (true) {
print(
"\n**Menu\n\n1. To push into the stack\n2. To pop out of the stack\n3. To display the items of the stack\n4. Exit the program");
int choice = readFunc(int);
switch (choice) {
case 1:
print("Enter the data to push into the stack");
var data = readFunc(int);
node.push(data);
break;
case 2:
node.pop();
break;
case 3:
node.printStack();
break;
case 4:
exit(0);
default:
print("Invalid option");
}
}
}