Goal: [10 pts] In our video lecture, we discussed the abstract data type Queue. A queue...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Goal: [10 pts] In our video lecture, we discussed the abstract data type Queue. A queue is a collection of items where the addition of new items happens at one end (tail) and the removal of existing items occurs at the other end (head) (FIFO). Use the Node class (an object with a data field and a pointer to the next element) to implement the queue data structure with the following operations: • enqueue(item) adds a new Node with value=item to the tail of the queue. It needs the value of the Node and returns nothing. • dequeue () removes the head Node from the queue. It needs no parameters and returns the value of the Node removed from the queue. The queue is modified. • is Empty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value. • • len(queue_object) returns the number of items in the queue. It needs no parameters and returns an integer. reverse() reverses all nodes from the queue. Modifies the original queue and returns nothing. Recursion could be useful here, however, you are allowed to use a helper Python list to assist the process if you want to work iteratively, but you are not allowed to use the reverse method on the Python list. You are not allowed to use any other data structures to copy the elements of the Queue for manipulating purposes in the enqueue and dequeue methods. Tips: You are not allowed to modify the given constructor You can't use queue functions from the Python library, otherwise no credit will be given Starter code contains the special methods_str___ and __repr___, use them to ensure the queue operations are updating the elements in the queue correctly When calling dequeue, you always need to check the size of the queue first to ensure there are elements in the queue. Remember that calling dequeue when the size is 1 should update the head and tail pointers to None class Node: J definit__(self, value): self.value = value self.next = None J def _str_(self): J return "Node({})".format(self.value) class Queue: _repr_ = _str_ >>> x=Queue () >>> x.isEmpty() True >>> x.dequeue () 'Queue is empty' >>> x.enqueue (1) >>> x.enqueue (2) >>> x.enqueue (3) >>> x.dequeue () 1 >>> x.reverse() Head: Node (3) Tail:Node(2) Queue:3 2 def __init__(self): self.head=None self.tail=None self.count=0 def_str_(self): temp=self.head out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' '.join(out) return f'Head:{self.head}Tail:{self.tail}Queue:{out}' _repr__=_ _str_ def isEmpty(self): def def enqueue (self, x): # --- Your code starts here def dequeue (self): # Your code starts here Your code starts here _len_(self): --- Your code starts here def reverse(self): Your code starts here Goal: [10 pts] In our video lecture, we discussed the abstract data type Queue. A queue is a collection of items where the addition of new items happens at one end (tail) and the removal of existing items occurs at the other end (head) (FIFO). Use the Node class (an object with a data field and a pointer to the next element) to implement the queue data structure with the following operations: • enqueue(item) adds a new Node with value=item to the tail of the queue. It needs the value of the Node and returns nothing. • dequeue () removes the head Node from the queue. It needs no parameters and returns the value of the Node removed from the queue. The queue is modified. • is Empty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value. • • len(queue_object) returns the number of items in the queue. It needs no parameters and returns an integer. reverse() reverses all nodes from the queue. Modifies the original queue and returns nothing. Recursion could be useful here, however, you are allowed to use a helper Python list to assist the process if you want to work iteratively, but you are not allowed to use the reverse method on the Python list. You are not allowed to use any other data structures to copy the elements of the Queue for manipulating purposes in the enqueue and dequeue methods. Tips: You are not allowed to modify the given constructor You can't use queue functions from the Python library, otherwise no credit will be given Starter code contains the special methods_str___ and __repr___, use them to ensure the queue operations are updating the elements in the queue correctly When calling dequeue, you always need to check the size of the queue first to ensure there are elements in the queue. Remember that calling dequeue when the size is 1 should update the head and tail pointers to None class Node: J definit__(self, value): self.value = value self.next = None J def _str_(self): J return "Node({})".format(self.value) class Queue: _repr_ = _str_ >>> x=Queue () >>> x.isEmpty() True >>> x.dequeue () 'Queue is empty' >>> x.enqueue (1) >>> x.enqueue (2) >>> x.enqueue (3) >>> x.dequeue () 1 >>> x.reverse() Head: Node (3) Tail:Node(2) Queue:3 2 def __init__(self): self.head=None self.tail=None self.count=0 def_str_(self): temp=self.head out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' '.join(out) return f'Head:{self.head}Tail:{self.tail}Queue:{out}' _repr__=_ _str_ def isEmpty(self): def def enqueue (self, x): # --- Your code starts here def dequeue (self): # Your code starts here Your code starts here _len_(self): --- Your code starts here def reverse(self): Your code starts here
Expert Answer:
Answer rating: 100% (QA)
class Node def initselfvalue selfvaluevalue selfnextNone def strse... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
An internal iterator for a bag is a collection of methods that allows a programmer to step through the elements of a bag one at a time. For example, we might have an internal iterator consisting of...
-
A _________ is a collection of predefined, built-in classes that you can use when writing programs. a. Vault b. Black box c. Library d. Store
-
A population is the collection of some outcomes, responses, measurements, or counts that are of interest. Determine whether the statement is true or false. If it is false, rewrite it as a true...
-
In Exercises 3538, evaluate C F dr. F(x, y, z) = xi + yj + zk C: r(t) = 2 cos ti + 2 sin tj + tk, 0t 2
-
(a) If a0, a1, a2, a3 is a list of four real numbers, what is 3i=1(a1 - a1-1)? (b) Given a list--a0, a1, a2,...,an--of n + 1 real numbers, where n is a positive integer, determine ni=1 (c) Determine...
-
Solve each problem. Sale Price Suppose that a computer that originally sold for x dollars has been discounted 60%. Which one of the following expressions does not represent its sale price? A. x -...
-
What is meant by corporate governance and why is it important?
-
(Amortization of Accumulated OCI (G/L), Corridor Approach, Pension Expense Computation) The actuary for the pension plan of Gustafson Inc. calculated the following net gains and losses. Other...
-
5. An investigator measuring various characteristics of a large group of athletes found that the correlation between the weight of an athlete and the amount of weight that athlete could lift was...
-
Hypothesis Testing for the Population Proportion Use a cell reference or a single formula where appropriate in order to receive full credit. Do not copy and paste values or type values, as you will...
-
Once you have updated your leadership development action plan, respond to the following: in this module, you have read about encouragement and recognition. Do encouragement and recognition really...
-
A company invests $400,000 in a new Information System but chooses to forgo on the training and support offered by the IT vendor to save on the yearly cost of additional $100,000. As a result, errors...
-
Emily transferred a building (FMV $200,000; AB $62,000) to Island Corporation in exchange for 95% of its only class of stock worth $172,000 and a machine (FMV $28,000; AB $20,000). What is Island's...
-
Use this lecture outline/guide to add notes from your reading under each topic. Include references to examples, exercises and problems that illustrate the concept. What is job order costing? When is...
-
The ABC Corporation had capital gains of $3,000 and capital losses of $2,000 this year. The corporation's gains are taxed at the 25% tax bracket. What is the amount of gains subject to tax?
-
The buyer at ABC wants to perform a break-even analysis on an item with a known supplier. The supplier's fixed overhead costs are US$3,000, and the variable costs per unit are US$6 to produce a lot...
-
Last year, Parking Corporation had net sales of $3,000,000 and cost of goods sold of $1,500,000. Its depreciation charge was $400,000 and selling, general, and administrative expense was $600,000. If...
-
Banner Company acquires an 80% interest in Roller Company for $640,000 cash on January 1, 2013. The NCI has a fair value of $160,000. Any excess of cost over book value is attributed to goodwill. To...
-
Which of the following is an indefinite loop? a. A loop that executes exactly 10 times b. A loop that follows a prompt that asks a user how many repetitions to make and uses the value to control the...
-
Which type of sort compares list items in pairs, swapping any two adjacent values that are out of order? a. Insertion sort b. Indexed sort c. Bubble sort d. Selection sort
-
Which of the following is a poorly written, trivial Boolean expression? a. a > b AND b > c b. a = 100 OR b > 200 c. a < b AND c < 100 AND d 5 d. a < 10 AND a > 40
-
Consider the dynamic system, a mass, spring and damper structure, shown in Figure 2.2. (a) Draw a free-body diagram for the dynamic system (b) Derive the Input-Output model of the dynamic system k...
-
Two connected cars with an applied input force \(u(t)\) and negligible rolling friction can be represented by a translational mechanical system as shown below. (a) Draw the free-body diagrams of this...
-
Consider an RLC circuit consisting of a resistor \((R)\), an inductor \((L)\), and a capacitor \((C)\), connected in series, as depicted in Figure 2.4. Derive the Input-Output model of the network....
Study smarter with the SolutionInn App