Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Stacks and Queues in python

So i was given this question. Consider the Stack and the Queue class with standard set of operations. Using the Stack and Queue class, what items are contained in them just before the mysteryFunction is called AND just after the mysteryFunction is called?

Here is the code:

def mysteryFunction(s, q):
    q.enqueue('csc148')
    q.enqueue(True)
    q.enqueue(q.front())
    q.enqueue('abstract data type')

    for i in range(q.size()):
        s.push(q.dequeue())

    while not s.is_empty():
        q.enqueue(s.pop())


if __name__ == '__main__':
    s=Stack()
    q=Queue()

#About to call mysteryFunction
#What are contents of s and q at this point?
    mysteryFunction(s, q)
#mysteryFunction has been called.
#What are contents of s and q at this point?

I'm having trouble understanding object oriented programming as i'm new to this topic. Is there any link that breaks down Stacks and Queues and what they do?

like image 594
ZeroKEz Avatar asked Feb 04 '16 16:02

ZeroKEz


People also ask

What are stacks in Python?

A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop.

Is list in Python a queue?

List is a Python's built-in data structure that can be used as a queue. Instead of enqueue() and dequeue(), append() and pop() function is used.

Does Python have stacks?

In Python, we can implement python stacks by: Using the built-in List data structure. Python's built-in List data structure comes with methods to simulate both stack and queue operations. Using the deque library which efficiently provides stack and queue operations in one object.

What are queues in Python?

Queue is a linear data structure that stores items in a First-In/First Out(FIFO) manner. In queue, the data element that is inserted first will be removed first. Operations that can be performed on the queue are: Enqueue : It adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.


3 Answers

In general, stacks are LIFO and queues are FIFO.

In Python, you can use the collections module to experiment with stacks and queues:

>>> from collections import deque
>>> stack = deque()
>>> stack.append(10)
>>> stack.append(20)
>>> stack.append(30)
>>> stack
deque([10, 20, 30])
>>> stack.pop()           # LIFO
30
>>> stack.pop()
20
>>> 
>>> queue = deque()
>>> queue.append(10)
>>> queue.append(20)
>>> queue.append(30)
>>> queue
deque([10, 20, 30])
>>> queue.popleft()       # FIFO
10
>>> queue.popleft()
20
like image 67
Raymond Hettinger Avatar answered Sep 28 '22 20:09

Raymond Hettinger


See following links for more information:

Stack
Queue

Visually these two data structures can be seen in a following way:

Stack:

Stack visual

Description:

There are variations of this data structure. However, in simple terms - as one can observe in the image provided, when you add to this data structure you place on top of what is already there and when you remove you also take from the top. You can view it as a stack of books which you go through one by one starting from the top and all the way down.

Queue

Queue visual

Description:

There are also variations on this particular data structure, however in simple terms - as you can see in the image provided, when you add to this data structure the new element goes in the begining and when you remove its the last element from the list which is being removed. You can imagine it as a queue you got in a shop where you stand behind a lot of people waiting for your turn to come to the counter to pay for your items.

like image 41
e.doroskevic Avatar answered Sep 28 '22 20:09

e.doroskevic


To test this line by line, here are implementations of the classes (wrappers around deque) that are used in the task:

from collections import deque

class Queue(deque):
    enqueue = deque.append
    dequeue = deque.popleft
    def front(self):
        return self[-1]
    def size(self):
        return len(self)

class Stack(deque):
    push = deque.append
    def is_empty(self):
        return not self
like image 25
L3viathan Avatar answered Sep 28 '22 22:09

L3viathan