Preview

10 - Stacks and Queues

 1. The 'STACK' is a ____________________ data structure.

  First-In Last-Out (FILO)

  First-In First-Out (FIFO)

  Last-In Last-Out (LILO)

  Last-In First-Out (LIFO)

 2. In a stack data is added (or ________) at one end. It is removed (or _______) from the same end of the structure. A stack is a "last in first out" structure. The top of the stack can also be viewed by _______ at it.

  pushed / popped / peeking

  popped / pushed / looking

  pushed / peeked / popping

  popped / pushed / penetrating

 3. A queue is similar to an array, but data is only added at one ____ (the tail). It is removed from the other end (the _____). A queue is a "_____" structure.

  end / head / FIFO

  end / top / FIFO

  front / top / FIFO

  head / end / LIFO

 4. In order to locate the last item in the stack, a special ________ is used, called the 'stack _________'. In Python a list can act like a stack so you can manage without a _______.

  pop

  place

  pagination

  pointer

 5. The _____ queue is a particular kind of queue where new items are added to the ____ of the queue as items are read off the front of the queue. So there is constant stream of data flowing into and out of the queue. Another name for it is '______ buffer.

  central / end / central

  cyclic / end / cyclic

  circular / rear / circular

  circular / front / circular

 6. The following is a stack containing the names of subjects. Certain subjects are added to the stack. Can you predict the output of the pop call on line 5?
stack = ["geography","statistics","biology","linear algebra"]
stack.append("physics")
stack.append("history")
stack.append("economics")
print(stack.pop())

  economics

  ["geography","statistics","biology","linear algebra"]

  Error - Stack Full

  geography

 7. Can you predict the output from Line 7?
# Python code to demonstrate Implementing 
# Queue using deque and list
from collections import deque
queue = deque(["R", "T", "A", "J"])
queue.append("C")
queue.append("F")
print(queue.popleft())                 

  F

  C

  R

  J

 8. _________are used whenever you want to process things one at a time as they come in where as ___________ are useful for tracing back to access previous elements/operations For example, undo operations in editors.

  Linked Lists / Stacks

  Stacks / Stacks

  Stacks / Queues

  Queues / Stacks

 9. Here is a class implementation of a Stack. Can you predict the output?
class Stack:
     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())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.size())

  False dog 3 True 8.4 3

  True 3 dog False 8.4 3

  True 3 dog True 8.4 3

  True dog 3 False 8.4 3

 10. The following functions demonstrate the working and functionality of a stack. Analyse the code and see if you can predict the output? (line 11)
#Functions to show how the stack works

def sumList(theList):
 if (theList==[]):
  return 0
 else:
  return theList[0] + sumList(theList[1:])
    
def go():
  total = sumList([10,20,30])
  print("total=",total)

go()    

  total = 50

  total = 0

  total = 30

  total = 60