Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Current code, output is not correct. Can someone please help? Thank you. Expecte

ID: 3664092 • Letter: C

Question

Current code, output is not correct. Can someone please help? Thank you. Expected output is at the bottom. Seems the deq() function the culprit?

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


class queue{
private:
    struct node{
        int val;
        node* next;
    };
    node* frontPtr;
    node* backPtr;
    node* head;

public:
    queue();
    void enq(int);
    void deq();
    void front();
    bool isEmpty();
    void printq();


};

queue::queue()
{
    frontPtr = NULL;
    backPtr = NULL;
    head= NULL;
}



void queue::enq(int num)
{
    node* tmp;
    tmp = new node;
    tmp->val = num;
    tmp->next = NULL;

    if (head == NULL) {
        head = tmp;
        frontPtr = head;
    } else{
        tmp->next = head;
        head = tmp;
    }
    backPtr = head;
}

void queue::deq()
{
    //node* tmp;
    if (head == NULL)
       cout << "No elements";
    else
    {
//       tmp = new node;
//       tmp->val = frontPtr->val;
//       tmp->next = NULL;
       cout << frontPtr->val;
       if (frontPtr->next != NULL) {
           frontPtr = frontPtr->next;
        } else {
           //only one node
           head = NULL;
           frontPtr = NULL;
           backPtr = NULL;
        }

    }
}

void queue::front()
{
   // node* tmp ;
   if(frontPtr == NULL)
       cout << "Queue is empty";
   else {
       // tmp = new node;
       //tmp->val = frontPtr->val;
       //tmp->next = frontPtr->next;
       cout << frontPtr->val;
   }

}

bool queue::isEmpty()
{
    if (frontPtr == NULL)
        return true;
    else
        return false;
}

void queue::printq()
{
    node* tmp;
    tmp = head;
    cout << "Back -> ";
    while (tmp != NULL)
    {
        cout << tmp->val << " ";
        tmp = tmp->next;
    }
    cout << " <- Front " <<endl;
}

int main()
{
    queue q;
    ifstream input;
    int cmd, val;

    input.open("D://ravi//Cheg//queue-cmd.txt");
    bool empty;
    string line,word;
    int i;
    if (input.fail()) {
             cout << "file not found!";
         }

    while (!input.eof())
    {
       //cout << " Reading file : "<<endl;
       if (!getline(input, line))
                   break;
       //cout << line << endl;
       istringstream ss(line);
       i = 0;
       cmd = -1;
       val = -1;
       while (ss && i <=2) {
           word = "";
           if (getline(ss, word, ' ')) {

               if(i == 0)
                   istringstream(word) >> cmd;
               else if(i == 1)
                   istringstream(word) >> val;

               i++;
           }
       }
        switch (cmd)
        {
        case 1:
            q.enq(val);
            break;
        case 2:
            cout << "Deq: " ;
            q.deq();
            cout << endl;
            break;
        case 3:
            cout << "Front: ";
            q.front();
            cout << endl;
            break;
        case 4:
            empty = q.isEmpty();
            if (empty)
                cout << "Queue is empty" << endl;
            else
                cout << "Queue is not empty" << endl;
        case 5:
            q.printq();
            break;
        }
    }
    return 0;
}
---output---

Back -> 1   <- Front
Back -> 2 1   <- Front
Back -> 3 2 1   <- Front
Back -> 4 3 2 1   <- Front
Front: 1
Deq: 1
Back -> <- Front
Deq: No elements
Back -> <- Front
Front: Queue is empty

Back -- 1 -- Front Back -- 2 1 -- Front Back -- 3 2 1 -- Front Back -- 4 3 2 1 -- Front Front: 1 Deq 1 Back -- 4 3 2 -- Front Deq 2 Back -- 4 3 Front Front: 3 Queue is not empty Back -- 4 3 __ Front Back-- 4 3 __ Front 3 3 t33 1234 4 4 n44

Explanation / Answer

please check the solution for stack implementation using c++

OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3

1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5

1.push 2.pop 3.display 4.exit
Enter ur choice3
5 2

1.push 2.pop 3.display 4.exit
Enter ur choice4

  #include<iostream>  #include<conio.h>  #include<stdlib.h>  using namespace std;  class stack  {               int stk[5];               int top;        public:               stack()                {                  top=-1;                 }               void push(int x)                {                   if(top >  4)                         {                             cout <<"stack over flow";                             return;                         }                   stk[++top]=x;                   cout <<"inserted" <<x;                 }               void pop()                 {                    if(top <0)                     {                           cout <<"stack under flow";                           return;                      }                      cout <<"deleted" <<stk[top--];                  }               void display()                 {                     if(top<0)                      {                              cout <<" stack empty";                              return;                      }                      for(int i=top;i>=0;i--)                      cout <<stk[i] <<" ";                  }  };     main()  {       int ch;       stack st;       while(1)          {               cout <<" 1.push  2.pop  3.display  4.exit Enter ur choice";               cin >> ch;               switch(ch)                {                 case 1:  cout <<"enter the element";                          cin >> ch;                          st.push(ch);                          break;                 case 2:  st.pop();  break;                 case 3:  st.display();break;                 case 4:  exit(0);                 }           }  return (0);  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote