-
-
Notifications
You must be signed in to change notification settings - Fork 447
/
Linked_List_Stack.dart
76 lines (64 loc) · 1.37 KB
/
Linked_List_Stack.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
//Author: Shawn
//Email: stepfencurryxiao@gmail.com
class Node<T> {
//the data of the Node
T? data;
Node<T>? next;
Node(T? data) {
this.data = data;
this.next = null;
}
}
class LinkedListStack<T> {
//Top of stack
Node<T>? head;
//Size of stack
int size = 0;
LinkedListStack() {
this.head = null;
this.size = 0;
}
//Add element at top of the stack
void push(T element) {
Node<T> newNode = new Node<T>(element);
newNode.next = this.head;
this.head = newNode;
this.size++;
}
//Pop element from top at the stack
T? pop() {
T? returnData = null;
if (size == 0) {
print("The stack is empty!!!");
} else {
Node<T>? destroy = this.head;
this.head = this.head?.next;
returnData = destroy?.data;
this.size--;
}
return returnData;
}
bool isEmpty() {
return this.size == 0;
}
int getSize() {
return this.size;
}
}
int main() {
LinkedListStack<String> Stack = new LinkedListStack<String>();
var returnData;
print("Push 2 5 9 7 to the stack\n");
Stack.push("2");
Stack.push("5");
Stack.push("9");
Stack.push("7");
print("Successful push!\n");
returnData = Stack.pop();
print("Pop a data: $returnData\n");
returnData = Stack.pop();
print("Pop a data: $returnData\n");
returnData = Stack.pop();
print("Pop a data: $returnData\n");
return 0;
}