/
stack.dart
105 lines (88 loc) · 2.51 KB
/
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
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
102
103
104
105
import 'dart:collection';
import 'dart:core' as core;
import 'dart:core';
import 'package:stack/illegal_operation_exception.dart';
class Stack<T> {
final ListQueue<T> _list = ListQueue();
final int noLimit = -1;
/// the maximum number of entries allowed on the stack. -1 = no limit.
int _sizeMax = 0;
/// Default constructor sets the maximum stack size to 'no limit.'
Stack() {
_sizeMax = noLimit;
}
/// Returns a list of T elements contained in the Stack
List<T> toList() => _list.toList();
/// Constructor in which you can specify maximum number of entries.
/// This maximum is a limit that is enforced as entries are pushed on to the stack
/// to prevent stack growth beyond a maximum size. There is no pre-allocation of
/// slots for entries at any time in this library.
Stack.sized(int sizeMax) {
if(sizeMax < 2) {
throw IllegalOperationException(
'Error: stack size must be 2 entries or more '
);
}
else {
_sizeMax = sizeMax;
}
}
/// check if the stack is empty.
bool get isEmpty => _list.isEmpty;
/// check if the stack is not empty.
bool get isNotEmpty => _list.isNotEmpty;
/// push element in top of the stack.
void push(T e) {
if(_sizeMax == noLimit || _list.length < _sizeMax) {
_list.addLast(e);
}
else {
throw IllegalOperationException(
'Error: cannot add element. Stack already at maximum size of: ${_sizeMax} elements');
}
}
/// get the top of the stack and delete it.
T pop() {
if (isEmpty) {
throw IllegalOperationException(
'Can\'t use pop with empty stack\n consider '
'checking for size or isEmpty before calling pop',
);
}
T res = _list.last;
_list.removeLast();
return res;
}
/// get the top of the stack without deleting it.
T top() {
if (isEmpty) {
throw IllegalOperationException(
'Can\'t use top with empty stack\n consider '
'checking for size or isEmpty before calling top',
);
}
return _list.last;
}
/// get the size of the stack.
int size() {
return _list.length;
}
/// get the length of the stack.
int get length => size();
/// returns true if element is found in the stack
bool contains(T x) {
return _list.contains(x);
}
/// removes all elements from the stack
void clear() {
while (isNotEmpty) {
_list.removeLast();
}
}
/// print stack
void print() {
for (var item in List<T>.from(_list).reversed) {
core.print(item);
}
}
}