-
-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathArray_Stack.dart
More file actions
94 lines (75 loc) · 1.89 KB
/
Array_Stack.dart
File metadata and controls
94 lines (75 loc) · 1.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
class ArrayStack<T> {
/// [stack]
List<T?> _stack = [];
/// [_count] is the number of element in the stack
int _count = 0;
/// [_size] of stack
int _size = 0;
//Init the array stack
ArrayStack(int size) {
this._size = size;
this._stack = List<T?>.filled(_size, null);
this._count = 0;
}
/// Push a item to the stack of type [T] to the [_stack]
/// if the size is exceeded the element wont be added.
void push(T item) {
if (_count == _size) {
return null;
}
_stack[_count] = item;
_count++;
}
/// Pop the last element inserted from the [_stack].
T? pop() {
if (_count == 0) {
return null;
}
T? pop_data = _stack[_count - 1];
_stack[_count - 1] = null;
_count--;
return pop_data;
}
List<T?> get stack {
return _stack;
}
}
void main() {
ArrayStack<String> arrayStack = new ArrayStack<String>(6);
arrayStack.push('1');
arrayStack.push("2");
arrayStack.push('3');
arrayStack.push("4");
arrayStack.push('5');
arrayStack.push("6");
test('test case 1', () {
expect(arrayStack.stack, ['1', '2', '3', '4', '5', '6']);
});
test('test case 2: pop stack', () {
expect('6', arrayStack.pop());
});
test('test case 3: pop stack', () {
expect('5', arrayStack.pop());
});
test('test case 4: pop stack', () {
expect('4', arrayStack.pop());
});
test('test case 5: pop stack', () {
expect('3', arrayStack.pop());
});
test('test case 6: pop stack', () {
expect('2', arrayStack.pop());
});
test('test case 7: pop stack', () {
expect('1', arrayStack.pop());
});
test('test case 8: pop stack', () {
expect(null, arrayStack.pop());
});
ArrayStack<String> arrayStack2 = new ArrayStack<String>(3);
test('test case 9', () {
expect(arrayStack2.stack, [null, null, null]);
});
}