Skip to content

Instantly share code, notes, and snippets.

@naing-pyae-hlyan
Created June 4, 2023 07:21
Show Gist options
  • Save naing-pyae-hlyan/3c8a9c0f50828768bf341f38a0c8ef28 to your computer and use it in GitHub Desktop.
Save naing-pyae-hlyan/3c8a9c0f50828768bf341f38a0c8ef28 to your computer and use it in GitHub Desktop.
ensorcelled-clover-7829

ensorcelled-clover-7829

Created with <3 with dartpad.dev.

void main() {
// final node1 = Node(value: 1);
// final node2 = Node(value: 2);
// final node3 = Node(value: 3);
// node1.next = node2;
// node2.next = node3;
// print(node2);
final stack = Stack<int>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
print(stack);
stack.pop();
print(stack);
}
// Linked List
class Node<T> {
T value;
Node<T>? next;
Node({required this.value, this.next});
@override
String toString() {
if (next == null) return '$value';
return '$value -> ${next.toString()}';
}
}
// Stack
class Stack<E> {
Stack() : _storage = <E>[];
final List<E> _storage;
void push(E element) => _storage.add(element);
E pop() => _storage.removeLast();
@override
String toString() {
return ''
'${_storage.reversed.join('\n')}'
'\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment