Understanding stacks and queues in python

So, I was asked this question. Consider the Stack and Queue class with a standard set of operations. Using the Stack and Queue classes, what elements are contained in them immediately before the mysteryFunction call And right after the mysteryFunction call?

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 since I'm new to this topic. Is there any connection that breaks the stacks and queues and what do they do?

+5
source share
4 answers

In general, stacks are LIFOs and queues are FIFOs.

In Python, you can use the collection 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 
+8
source

For more information, see the following links:

Stack
Queue

Visually, these two data structures can be seen as follows:

Stack:

Stack visual

Description:

Variations exist in this data structure. However, in simple terms - as you can see in the presented image, when you add data to this structure, you put on top of what is already there, and when you delete, you also gain the upper hand. You can view it as a stack of books that you are viewing one after another, starting from top to bottom to top.

Queue

Queue visually

Description:

There are also variants of this particular data structure, however, in simple terms - as you can see in the presented image, when you add data to this structure, the new element goes at the beginning and when you delete its last element from which it is deleted. You can imagine this as the line you received in the store, where you stand behind a lot of people waiting for your line to come to the counter to pay for your items.

+4
source

To test this line by line, here is the implementation 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 
+1
source

STACK #LIFO

 class stack(object): def __init__(self): self.items = [] def isEmpty(self): return self.items==[] def push(self,item): self.items.append(item) def pop (self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return (len(self.items)) s = stack() print (s.isEmpty()) >> True s.push(1) s.push('3') s.peek() >>'3' s.size() >> 2 

Queue #FIFO

 class Queue(object): def __init__(self): self.items = [] def isEmpty(self): return self.items==[] def enqueue(self,item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return (len(self.items)) q = Queue() q.isEmpty() >>True q.enqueue(1) q.enqueue(2) q.dequeue() >>1 
0
source

Source: https://habr.com/ru/post/1242310/


All Articles