2.3.1 stacks and queues Flashcards
QUEUES
Queues is a type of list where data is inserted at one end and retrieved from the other end.
FIFO
Queues are known as FIFO data structure, first in first out!
STACK
stack is a list which is limited to only allow data to be inserted and retrieved from the same end.
LIFO
stacks are known as last in first out!
STACK coding
stack = []
Push
stack.append(‘A’)
stack.append(‘B’)
stack.append(‘C’)
print(“Stack: “, stack)
Pop
element = stack.pop()
print(“Pop: “, element)
Peek
topElement = stack[-1]
print(“Peek: “, topElement)
isEmpty
isEmpty = not bool(stack)
print(“isEmpty: “, isEmpty)
Size
print(“Size: “,len(stack))
QUEUES coding
queue = []
Enqueue
queue.append(‘A’)
queue.append(‘B’)
queue.append(‘C’)
print(“Queue: “, queue)
Dequeue
element = queue.pop(0)
print(“Dequeue: “, element)
Peek
frontElement = queue[0]
print(“Peek: “, frontElement)
isEmpty
isEmpty = not bool(queue)
print(“isEmpty: “, isEmpty)
Size
print(“Size: “, len(queue))