def sort_stack(stack):
    if stack.size() == 0:
        return stack
    temp_stack = Stack.new()
    while not stack.is_empty():
        element = stack.pop()
        while not temp_stack.is_empty() and temp_stack.peek() > element:
            temp_stack_element = temp_stack.pop()
            stack.push(temp_stack_element)
        temp_stack.push(element)
    return temp_stack
def is_palindrome(list):
    if not list.head():
        return True  # Empty lists are empty strings, and as such they are palindromes.
    fast_runner = list.head()
    slow_runner = list.head()
    reversed_half = Stack.new()
    i = 0
    # Position slow runner in the middle of the linked list.
    while fast_runner is not None:
        fast_runner = fast_runner.next_element()
        i += 1
        if i % 2 == 0:
            reversed_half.push(slow_runner)
            slow_runner = slow_runner.next_element()
    if i % 2 != 0:  # Edge case for odd length linked lists.
        reversed_half.push(slow_runner)
    while slow_runner is not None:
        elem = reversed_half.pop()
        if slow_runner.data() != elem.data():
            return False
        slow_runner = slow_runner.next_element()
    return True
 def __init__(self, size_limit):
     if size_limit <= 0:
         raise ValueError("Stack size limit needs to be positive.")
     self._size_limit = size_limit
     self._stack = Stack.new()
Example #4
0
 def __init__(self):
     self._newest_elements = Stack.new()
     self._oldest_elements = Stack.new()
Example #5
0
 def __init__(self, index):
     self._disks = Stack.new()
     self._index = index