Question

In: Computer Science

Give a complete implementation of the queue ADT using a singly linked list that includes a...

Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

''' a Node class to represent a single node on the linked list '''
class Node:
    def __init__(self, data, next=None):
        self.data=data
        self.next=next

    def set_next(self, next):
        self.next=next

    def get_next(self):
        return self.next

    def get_data(self):
        return self.data

    def set_data(self, data):
        self.data=data

''' Queue class implemented using a linked list using Node class'''
class Queue:
    #constructor
    def __init__(self):
        #initializing a sentinel/dummy head node
        self._head=Node(None)

    #method to add an element to the back of queue
    def enqueue(self, data):
        #creating a node with data
        node=Node(data)
        #taking self._head as prev
        prev=self._head
        #taking first node (if exists) as curr
        curr=self._head.get_next()
        #looping until curr becomes None
        while curr is not None:
            #storing curr in prev
            prev=curr
            #advancing curr
            curr=curr.get_next()
        #simply setting node as next of prev
        prev.set_next(node)

    #removes and returns the front element from queue
    def dequeue(self):
        #if queue is empty, returning None
        if self.isEmpty():
            return None
        #storing data at first node (next of dummy head)
        data=self._head.get_next().get_data()
        #removing the node between head and head.next.next
        self._head.set_next(self._head.get_next().get_next())
        #returning removed item
        return data

    #returns the front element without removing
    def front(self):
        if self.isEmpty():
            #empty
            return None
        #returning data of first node
        return self._head.get_next().get_data()

    #returns True if queue is empty
    def isEmpty(self):
        #checking if head is pointing to None
        return self._head.get_next() is None
#end of Queue class


#code for testing
if __name__ == '__main__':
    #creating a queue, adding numbers from 0 to 9
    q=Queue()
    for i in range(10):
        q.enqueue(i)
    #looping and dequeueing each value, it should be in the order
    #0...9
    while not q.isEmpty():
        print(q.dequeue())

#output


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some missing functionality. It contains a templated Node class and a templated LinkedList class. Do not modify the class definitions. The linked list class contains the following methods: • LinkedList – Constructor of a linked list with a head pointing to null (implemented). • ~LinkedList – Destructor of a linked list. • deleteFromHead – Removes and returns content of the first node of the list....
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT