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)
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...
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...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
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. ·...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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.
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT