Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FRONT of Queue; Bottom of Stack as REAR of Queue * Useful hint: student may try to use two stacks to simulate a queue behaviour.

FRONT of Queue; Bottom of Stack as REAR of Queue * Useful hint: student may try to use two stacks to simulate a queue behaviour. o front top A B B rear External: A Queue (behaviour) Internal: Use Stack to hold and access data o Given an implemented Stack (below) in file Stack.py and an uncompleted Queue in A2B1.py Operations (Stack) GIVEN Description _init_(): Initiate/create a new Stack (constructor / initializer) * This code sample is in Python-style push(elt): Insert an element elt at the top of stack pop():elt Remove and return the top element from the stack - If failed, return null/None; e.g. empty stack peek():elt Get and return the top element of the stack, without removal - If failed, return null/None; e.g. empty stack displays():

files. Complete the Stack-based Queue based on Queue ADT, by modifying our given file A2B1.py Extra Operations (methods of the class) to be implemented by Students: * ONLY access the interface (methods) of the given Stack data type. * DO NOT access the internal fields of the given stack, such as pList, top and capacity. At least one line of simple comment for each extra operation required Operation (Queues) Description _init__(): Initiate/create a new Queue (constructor / initializer) ; GIVEN displayQ(): Display all elements of the Queue in order ; GIVEN peek():elt Get and return the front element of queue, without removal (EASY) - If failed, return None; e.g. empty queue dequeue():elt Remove and return the front element from the queue (EASY) - If failed, return None; e.g. empty queue reverseQ(): Reverse the queue, by reversing

self.inStack = Stack() # an initial empty Stack, for internal Stack def displayQ(self): # (REAR/Left to FRONT/Right) print(f\">>> Queue Display, (REAR/Left to FRONT/Right)\") if self.inStack.peek()==None: print(\" ... AN EMPTY QUEUE\"); return else: self.inStack.displays() print() #### ### ###### STUDNET'S WORK ### # simple comment HERE def peek (self): pass # TO BE DONE # MORE TO BE DONE ## END of STUDNET'S WORK ## File MA2B1.py for basic running and testing # MA2B1.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program from A2B1 import Queues def main(): print(\"=== A2B1, Stack-based Queue, by === \") myQ = Queues() print(\" --- 1. New Queues created ---\") myQ.displayo() print(f\" peek(), front elt is {myQ.peek()}\") myQ.enqueue('A'); myQ.enqueue('B'); myQ.enqueue('C') print(\" --- 2. ENQueued: A,B,C ---\") myq.displaye() 2.1 reverseQ once ---\") myQ.reverseQ() print(\"- myQ.displayQ() myQ.reverseQ() print(\"- myQ.displaye() 2.2 reverseQ twice BACK ---\") deqelt = myQ. dequeue() print(\" --- 3. DEQueued: elt is:\", deqElt) myQ.displayQ() print(f\" peek(), front elt is {myQ. peek()}\") myQ.enqueue('D') deqElt = myQ. dequeue() print(\" --- 4. ENQueued: D, then DEQueued: elt is:\", deqelt) myq.displayQ() # below, avoid execution if import only if name main \": main() Sample console display output of executing the main testing program MA2B1.py === A2B1, Stack-based Queue, by

====================================

A2B1.py:

# Do NOT modify the given part, unless specified # ONLY access the interface of the internal Stack # displayS(), push(), peek(), pop()

from Stack import Stack

class QueueS: # defining a class of Queue, based on Stack # Treating TOP of Stack as FRONT of Queue; Bottom of Stack as REAR of Queue def __init__(self): # constructor self.inStack = Stack() # an initial empty Stack, for internal Stack

def displayQ(self): # (REAR/Left to FRONT/Right) print(f\">>> Queue Display, (REAR/Left to FRONT/Right)\") if self.inStack.peek()==None: print(\" ... AN EMPTY QUEUE\"); return else: self.inStack.displayS() print()

###################### STUDNET's WORK ###################### # simple comment HERE def peek(self): pass # TO BE DONE BY STUDENT

def dequeue(self): # method, remove and return front elt pass # TO BE DONE BY STUDENT def reverseQ(self): # method, reverse the internal field inStack pass # TO BE DONE BY STUDENT def enqueue(self, inValue): # method, insert to tail / rear pass # TO BE DONE BY STUDENT

#################### END of STUDNET's WORK ######################

====================================

MA2B1.py:

# for basic running and testing. # * DO NOT modify this given test file # Main Testing Program

from A2B1 import QueueS

def main(): print(\"=== A2B1, Stack-based Queue, by === \") myQ = QueueS() print(\" --- 1. New QueueS created ---\") myQ.displayQ() print(f\" peek(), front elt is {myQ.peek()}\")

myQ.enqueue('A'); myQ.enqueue('B'); myQ.enqueue('C') print(\" --- 2. ENQueued: A,B,C ---\") myQ.displayQ() myQ.reverseQ() print(\"--------- 2.1 reverseQ once ---\") myQ.displayQ() myQ.reverseQ() print(\"--------- 2.2 reverseQ twice BACK ---\") myQ.displayQ() deqElt = myQ.dequeue() print(\" --- 3. DEQueued: elt is:\", deqElt) myQ.displayQ() print(f\" peek(), front elt is {myQ.peek()}\")

myQ.enqueue('D') deqElt = myQ.dequeue() print(\" --- 4. ENQueued: D, then DEQueued: elt is:\", deqElt) myQ.displayQ()

# below, avoid execution if import only if __name__ == \"__main__\": main()

====================================

Stack.py:

# Do NOT modify the given file.

# Please ignore the implementation details of this given file.

class Stack: # defining a class of Stack, with Array-List

def __init__(self, inSize=6): # constructor, default size = 6 self.pList = [None]*inSize # new a Python List to hold data self.top = 0 # Position of top element, starts from 1 self.capacity = inSize # The current capacity of the stack

def displayS(self): # (Bottom/Left >[0] to Top/Right for i in range(0, self.top): print(f\" > {self.pList[i]}\", end='') print()

def push(self, eltIn): # method, push eltIn to the top of stack ########## section: for Enlarging the capacity if necessary # if (self.top == self.capacity): # full capacity reached? self.capacity *=2 # resize: double the original capacity newPList = [None]*(self.capacity) # create new pList for i in range(self.top): # copy original to new list newPList[i] = self.pList[i] self.pList = newPList # use the new list as updated ########## section: Insert new element, and shift the rest # self.top +=1 # update/increase position of top element self.pList[self.top-1] = eltIn # insert/add new element to top def peek(self): # method, peek/get top elt of stack without removal if self.top==0: # case of empty Stack, return None return None else: return self.pList[self.top-1]

def pop(self): # method, Remove & return top elt of stack if self.top==0: # case of empty Stack, return None return None else: popElt = self.pList[self.top-1] self.pList[self.top-1] = None self.top -= 1 # update/decrease position of top element return popElt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

When will n(A B) = 0? Explain and give an example.

Answered: 1 week ago