Preview

10 - Stacks and Queues

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

  Last-In First-Out (LIFO)

  First-In Last-Out (FILO)

  Last-In Last-Out (LILO)

  First-In First-Out (FIFO)

 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.

  popped / pushed / penetrating

  pushed / peeked / popping

  pushed / popped / peeking

  popped / pushed / looking

 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

  front / top / FIFO

  head / end / LIFO

  end / top / FIFO

 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

  pointer

  pagination

  place

 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.

  circular / rear / circular

  circular / front / circular

  central / end / central

  cyclic / end / cyclic

 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())

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

  Error - Stack Full

  geography

  economics

 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())                 

  R

  F

  J

  C

 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 / Queues

  Queues / Stacks

  Stacks / 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())

  True 3 dog True 8.4 3

  True 3 dog False 8.4 3

  False dog 3 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 = 60

  total = 0

  total = 50

  total = 30