Answer to Question #215116 in C++ for bila

Question #215116

With the concepts of Queue Data Structure, write the functions performing the following tasks:

1.Insertion of an element in the Queue i.e. enqueue()

2.Deletion of an Element from the Queue i.e. dequeue()

3.Display All Elements in the Queue i.e. dispqueue()

4.Display the Element at the Front i.e. dispfront()

5.enqueue() the Queue with the elements in ascending order. After that,use the above four functions to display the same elements in the Queue in descending order.



1
Expert's answer
2021-07-08T15:53:03-0400
#include <iostream>
using namespace std;
class Node{
    public:
    int data;
    Node *next;
    Node();
    Node(int);
};
Node::Node(){
    next = NULL;
}
Node::Node(int data){
    this->data = data;
    next = NULL;
}
class Queue{
    Node *head, *tail;
    int size;
    public:
    Queue();
    void enqueue(int);
    void dequeue();
    void dispqueue();
    int dispfront();
    bool isEmpty();
    Queue showReversed(Queue q);
};
Queue::Queue(){
    head = NULL;
    tail = NULL;
    size = 0;
}
void Queue::enqueue(int data){
    if(tail == NULL){
        tail = new Node(data);
        head = tail;
        size++;
    }
    else{
        Node *temp = new Node(data);
        tail->next = temp;
        tail = temp;
        size++;
    }
}
void Queue::dequeue(){
    if(head != NULL){
        Node *temp = head;
        head = head->next;
        if(temp == tail) tail = NULL;
        delete temp;
        size--;
    }
}
void Queue::dispqueue(){
    Node* curr = head;
    while(curr != NULL){
        cout<<curr->data<<" ";
        curr = curr->next;
    }
    cout<<endl;
}
int Queue::dispfront(){
    return head->data;
}
bool Queue::isEmpty(){
    return !size;
}
Queue Queue::showReversed(Queue q){
    if(q.isEmpty()) return q;
    else{
        int data = q.dispfront();
        q.dequeue();
        q = showReversed(q);
        q.enqueue(data);
        return q;
    }
}
int main(){
    Queue q;
    for(int i = 1; i < 11; i++) q.enqueue(i);
    cout<<"In order: "; 
    q.dispqueue();
    cout<<"\nIn reverse: ";
    q.showReversed(q);
    q.dispqueue();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS