Stack Full
shinta0x01
python
a year ago
3.3 kB
9
Indexable
class Node: def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): self.top = None def push(self, data): node = Node(data) node.next = self.top self.top = node def pop(self): popped_data = self.top.data self.top = self.top.next return popped_data def peek(self): if self.top is None: return None else: return self.top.data def is_palindrome(self, str1): for i in str1: self.push(i) for i in str1: if i == self.peek(): self.pop() else: self.pop() return False return True def backtrack(self): current = self.top while current: if current.data < 0: for i in range(5): self.pop() current = current.next def concatenate(self, other_stack): if other_stack.top is None: return # Traverse to the end of the current stack current = self.top while current.next: current = current.next # Concatenate the other stack to the end of the current stack current.next = other_stack.top # Clear the other stack after concatenation other_stack.top = None def display(self): current = self.top while current: print(current.data, '', end='') current = current.next def min(self): current = self.top minimum = self.top.data while current: if minimum < current.data: minimum = minimum else: minimum = current.data current = current.next return minimum def max(self): current = self.top maximum = self.top.data while current: if maximum > current.data: maximum = maximum else: maximum = current.data current = current.next return maximum def is_empty(self): return self.top is None def size(self): count = 0 current = self.top while current: count += 1 current = current.next return count def clear(self): self.top = None def search(self, data): current = self.top position = 0 while current: if current.data == data: return position position += 1 current = current.next return None def reverse(self): temp_stack = Stack() while self.top: temp_stack.push(self.pop()) self.top = temp_stack.top def copy(self): copied_stack = Stack() current = self.top while current is not None: copied_stack.push(current.data) current = current.next return copied_stack stack = Stack() stack1 = Stack() stack.push(0) stack.push(2) stack.push(4) stack.push(8) stack.display() print() print('Minimum', stack.min()) print('Maximum', stack.max())
Editor is loading...
Leave a Comment