search method
Searches for an element in the stack and returns its 1-based position from the top.
Returns -1 if the element is not found. The top element is at position 1, the second element is at position 2, and so on.
Example:
final stack = LinkedStack<String>();
stack.push('bottom');
stack.push('middle');
stack.push('top');
print(stack.search('top')); // 1
print(stack.search('middle')); // 2
print(stack.search('bottom')); // 3
print(stack.search('missing')); // -1
Implementation
int search(Object? element) {
var current = _top;
var position = 1;
while (current != null) {
if (current.data == element) {
return position;
}
current = current.next;
position++;
}
return -1;
}