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...
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...
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
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)
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).
c++ please Your task is to write the implementation for a class of polynomial operations. Your...
c++ please Your task is to write the implementation for a class of polynomial operations. Your will write the code for: 2 constructors, a destructor, print, addition, multiplication , differentiation and integration of polynomials. The polynomials will be comprised of linked TermNodes. struct TermNode { int exp; // exponent double coef; // coefficient TermNode * next; }; class Polynomial { public: Polynomial (); // default constructor Polynomial (int r, int c); // constructor makes a 1 node polynomial Polynomial(const Polynomial...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT