Stack and Queue
Table Of Contents
Use Case 1: Video Player
Problem Statement: Video Player's queue and back stack
Suppose you are a developer in a company which is making a new video player. You are tasked to implement the video queue (Playlist) and back stack feature.
You are provided with a project which has 3 classes:
Driver: This file contains the logic to drive the application.
video_queue: This is the implementation of a queue using a list. This will be used to hold the queued videos.
Back_stack: This is the implementation of a stack using a list. This will be used to hold the last played videos.
Tasks
-- Implement a method ‘pop’ in Video_queue. This method should pop the element from the front of the queue.
-- Implement method ‘pop’ in Back_Stack. This method should pop the element from the back of the queue.
Documentation
-- Driver contains three methods; add_to_playlist(), play_next() and go_back().
play_next() represents the method which plays the video from the queue.
And go_back() represents the method which lets you go back to the last viewed videos.
class BackStack:
stack = None
def __init__(self):
self.stack = [] # empty
print("Stack created. Right now empty")
def append(self, video):
self.stack.append(video)
print(video,"appended in the back stack")
print("self.stack: ",self.stack)
def pop(self):
if self.stack:
return_val = self.stack[len(self.stack) - 1]
self.stack = self.stack[0 : len(self.stack)-1]
print(return_val,"popped from back stack")
return return_val
class VideoQueue:
queue = None
def __init__(self):
self.queue = [] # empty
print("Queue created. Right now empty")
def append(self, video):
self.queue.append(video)
print(video,"appended in the queue")
print("self.queue: ",self.queue)
def pop(self):
return_val = self.queue[0]
self.queue = self.queue[1:]
return return_val