Example #1
0
    def push(self, data):
        if len(self.stacks) == 0:
            self.stacks[len(self.stacks) + 1] = Stack_Node(5)
            self.stacks[len(self.stacks)].push(data)

        elif self.stacks[len(self.stacks)].is_full():
            self.stacks[len(self.stacks) + 1] = Stack_Node(5)
            self.stacks[len(self.stacks)].push(data)

        else:
            self.stacks[len(self.stacks)].push(data)
        return self
def sort_stack(stack):

    spare_stack = Stack_Node()
    while not stack.is_empty():
        if spare_stack.is_empty():
            spare_stack.push(stack.pop())

        else:
            element = stack.pop()
            while not spare_stack.is_empty() and element > spare_stack.peek():
                stack.push(spare_stack.pop())
            spare_stack.push(element)

    return spare_stack
def sort_stack(stack):
	
	spare_stack = Stack_Node()
	while not stack.is_empty():
		if spare_stack.is_empty():
			spare_stack.push(stack.pop())

		else:
			element = stack.pop()
			while not spare_stack.is_empty() and element > spare_stack.peek():
				stack.push(spare_stack.pop())
			spare_stack.push(element)
		
	return spare_stack
Example #4
0
 def __init__(self):
     self.push_queue = Stack_Node()
     self.pop_queue = Stack_Node()
Example #5
0
class MyQueue(object):
    def __init__(self):
        self.push_queue = Stack_Node()
        self.pop_queue = Stack_Node()

    def enqueue(self, data):
        if self.pop_queue.size > 0:
            while self.pop_queue.size > 0:
                self.push_queue.push(self.pop_queue.pop())
        self.push_queue.push(data)

    def dequeue(self):
        if self.push_queue.size > 0:
            while self.push_queue.size > 0:
                self.pop_queue.push(self.push_queue.pop())
        return self.pop_queue.pop()

    def peek(self):
        if self.push_queue.size > 0:
            while self.push_queue.size > 0:
                self.pop_queue.push(self.push_queue.pop())
        return self.pop_queue.peek()

    def __str__(self):
        if self.push_queue.size > 0:
            return self.push_queue.__str__()
        else:
            return self.pop_queue.__str__()
# 3 - Once we either run out of items or find that the element is < the top element
# in the spare stack then we want to push that element onto the spare stack
# We continue to repeat this step (hence the while) until the given stack is empty
from stack import Stack_Node
def sort_stack(stack):
	
	spare_stack = Stack_Node()
	while not stack.is_empty():
		if spare_stack.is_empty():
			spare_stack.push(stack.pop())

		else:
			element = stack.pop()
			while not spare_stack.is_empty() and element > spare_stack.peek():
				stack.push(spare_stack.pop())
			spare_stack.push(element)
		
	return spare_stack


unsorted_stack = Stack_Node()
unsorted_stack.push(1)
unsorted_stack.push(2)
unsorted_stack.push(8)
unsorted_stack.push(3)
unsorted_stack.push(9)
unsorted_stack.push(4)

sorted_stack = sort_stack(unsorted_stack)
print sorted_stack
print sorted_stack.peek()
Example #7
0
from stack import Stack_Node


def MoveTower(towers, disk, source, destination, spare):
    if disk == 0:
        towers[destination].push(towers[source].pop())
    else:
        MoveTower(towers, disk - 1, source, spare, destination)
        towers[destination].push(towers[source].pop())
        MoveTower(towers, disk - 1, spare, destination, source)


towers = {}

towers[0] = Stack_Node()
towers[0].set_stack([5, 4, 3, 2, 1, 0])

towers[1] = Stack_Node()
towers[2] = Stack_Node()

MoveTower(towers, 5, 0, 2, 1)
# We continue to repeat this step (hence the while) until the given stack is empty
from stack import Stack_Node


def sort_stack(stack):

    spare_stack = Stack_Node()
    while not stack.is_empty():
        if spare_stack.is_empty():
            spare_stack.push(stack.pop())

        else:
            element = stack.pop()
            while not spare_stack.is_empty() and element > spare_stack.peek():
                stack.push(spare_stack.pop())
            spare_stack.push(element)

    return spare_stack


unsorted_stack = Stack_Node()
unsorted_stack.push(1)
unsorted_stack.push(2)
unsorted_stack.push(8)
unsorted_stack.push(3)
unsorted_stack.push(9)
unsorted_stack.push(4)

sorted_stack = sort_stack(unsorted_stack)
print sorted_stack
print sorted_stack.peek()
Example #9
0
	def __init__(self):
		self.push_queue = Stack_Node()
		self.pop_queue = Stack_Node()
Example #10
0
class MyQueue(object):

	def __init__(self):
		self.push_queue = Stack_Node()
		self.pop_queue = Stack_Node()

	def enqueue(self,data):
		if self.pop_queue.size > 0:
			while self.pop_queue.size > 0:
				self.push_queue.push(self.pop_queue.pop())
		self.push_queue.push(data)

	def dequeue(self):
		if self.push_queue.size > 0:
			while self.push_queue.size > 0:
				self.pop_queue.push(self.push_queue.pop())
		return self.pop_queue.pop()
	
	def peek(self):
		if self.push_queue.size > 0:
			while self.push_queue.size > 0:
				self.pop_queue.push(self.push_queue.pop())
		return self.pop_queue.peek()

	def __str__(self):
		if self.push_queue.size > 0:
			return self.push_queue.__str__()
		else:
			return self.pop_queue.__str__()