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...
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...
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...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using a stack to keep track of the numbers. Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. It is suggested that you implement a class RPN with a single...
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT