Question

In: Computer Science

Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...

Python:

Solve following problems using Linked List Data Structure

2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.

Solutions

Expert Solution

ANS:

# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.

class Node:
def __init__(self):
self.item=None
self.linked=None
class CQueue:
def __init__(self):
front=None
rear=None

#This is enqueue which will make circular queue

def Enqueue(q,value):
temp=Node()
temp.item=value
  
if(q.front==None):
q.front=temp
else:
q.rear.linked=temp
  
q.rear=temp
q.rear.linked=q.front


#deleting element from Circular queue

def Dequeue(q):
if(q.front==None):
print("Queue Empty!")
return -1000
value=None
if(q.front==q.rear):
  
value=q.front.item
q.front=None
q.rear=None
else:
temp=q.front
value=temp.item
q.front=q.front.linked
q.rear.linked=q.front
  
return value
  
def first(q):
return q.front

def display(q):
temp=q.front
print("Circular Queue Elements:")
  
while(temp.linked!=q.front):
print(temp.item, end=" ")
temp=temp.linked
print(temp.item)
  
if __name__ == '__main__':
q = CQueue()
q.front = q.rear = None

Enqueue(q, 20)
Enqueue(q, 18)
Enqueue(q, 9)
Enqueue(q,12)
  
#print("First=",first(q))
  
  
display(q)
  
print("Deleted value = ", Dequeue(q))
print("Deleted value = ", Dequeue(q))
  

display(q)
  
Enqueue(q, 100)
Enqueue(q, 80)
display(q)


COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
I am attempting to construct a queue through a linked list using a node data structure...
I am attempting to construct a queue through a linked list using a node data structure with different data types (char, int) in C++. Is this possible or can the queue be of only one data type? My queue has both names and numbers in it so what data type would be best? Can a "char" data type store integers? Can a "string" data type store integers?
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...
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...
1- Given following data structure of Single linked list :           class ListNode                     &n
1- Given following data structure of Single linked list :           class ListNode                                    { int item ;                                           ListNode next ;                                      ….            } Choose the correct answer :                                                            Suppose reference refers to a node in a List (using the ListNode ) . What statement changes reference so that it refers to the next node?                        1-      reference++ ; 2-    reference = next ; 3-    reference+= next ; reference = reference.next ; Suppose p refers to...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT