Question

In: Computer Science

Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...

Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that
reverses the whole queue.
In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse
function to reverse the queue and then print the queue.

Solutions

Expert Solution

Queue work in order is First In First Out (FIFO) with front and rear. In simple queue with linked list reverse implementation is not possible so you have to use the D-queue with Linklist. In this program I implement the reverse by simple storing value in array and print in reverse.

Write a function “reverse” in your queue class (linked list implementation) that
reverses the whole queue.

#include <iostream>

using namespace std;
struct node {
int data;
struct node *next;
};
struct node* front_1 = NULL;
struct node* rear_1 = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in the queue : "<<endl;
cin>>val;
if (rear_1 == NULL) {
rear_1 = (struct node *)malloc(sizeof(struct node));
rear_1->next = NULL;
rear_1->data = val;
front_1 = rear_1;
} else {
temp=(struct node *)malloc(sizeof(struct node));
rear_1->next = temp;
temp->data = val;
temp->next = NULL;
rear_1 = temp;
}
}

void Display() {
temp = front_1;
if ((front_1 == NULL) && (rear_1 == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void RevDisplay()
{
int a[100];
temp = front_1;
if ((front_1 == NULL) && (rear_1 == NULL)) {
cout<<"Queue Empty"<<endl;
return;
}

cout<<"The Reverse elements of Queue are: ";
int i=0;
while (temp != NULL) {
a[i]=temp->data;
temp = temp->next;
i++;
}

for(int k=i-1;k>=0;k--)
{
cout<<a[k]<<" ";
}
cout <<endl;
}
int main() {
int ch;
cout<<"1) Insert element in queue"<<endl;
cout<<"2) Reverse Display of elements of queue"<<endl;
cout<<"3) Display of elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: RevDisplay();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice of selection"<<endl;
}
} while(ch!=4);
return 0;
}

Output:

I hope u like my effort.... thank u


Related Solutions

DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
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 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...
please answer my question... Write a template class that implements an extended queue (use Linked List)....
please answer my question... Write a template class that implements an extended queue (use Linked List). Ex: ExtendedQueue<int> int_queue; ExtendedQueue<double> double_queue; ExtendedQueue<char> char_queue; –Write a program to test this template class in c++. the details of the ExtendedQueue: insert delete front rear queue size. the output will be: Inserting 2 Inserting 4 Inserting 6 Front element is: 2 Removing 2 Inserting 8 Queue size is 3 Removing 4 Removing 6 Removing 8 Queue Is Empty
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...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT