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

Hello. I am obviously very unfamiliar with C/C++, yet I am trying to complete an

ID: 3664011 • Letter: H

Question

Hello.

I am obviously very unfamiliar with C/C++, yet I am trying to complete an assignment. My instructor is of no use even though I have said I am drowning. So here I am for help/clarity.

Assignment:

I cannot use a library.

Your program should implement a C++ queue class with the following member functions:

-Void enq(int)

-Void deq()

-Void front()

-Bool isEmpty()

-Void printq()

-=-=-=-=-=-=-=-=-=-=-=

Your program will take in a command file called “cmd.txt”

cmd.txt:

1 1

5

1 2

5

1 3

5

1 4

5

3

2

5

2

5

3

-=-=-=-=-=-=-=-=-=-=-=

Commands

-1              enq

-2              deq

-3              front

-4              isEmpty

-5              printq

-=-=-=-=-=-=-=-=-=-=-=

Current Code:

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


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

private:
   struct node{
       int val;
       node* next;
   };
   node* frontPtr;
};

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

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

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

void queue::deq()
{
   node* tmp;
   node* walk;
   tmp = new node;
   tmp->val = num;
   tmp->next = NULL;
   walk = frontPtr;

   if (head == NULL)
       head = tmp;
   else
   {
       while (walk->next != NULL)
           walk = walk->next;
       walk->next = tmp;
   }
}

void queue::front()
{
  
}

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

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

int main()
{
   queue q;
   ifstream input;
   int cmd, val;
   input.open("cmd.txt");
   bool empty;
  
   while (input >> cmd)
   {
       switch (cmd)
       {
       case 1:
           input >> val;
           q.enq(val);
           break;
       case 2:
           cout << "Deq: " << q.deq() << endl;
           break;
       case 3:
           cout << "Front: " << q.front() << endl;
           break;
       case 4:
           empty = q.isEmpty();
           if (empty)
               cout << "Queue is empty" << endl;
           else
               cout << "Queue is not empty" << endl;
       case 6:
           q.printq();
           break;  
       }
   }
   return 0;
}

Output example:

Suggestions/assistance would be greatly appreciated. Thank you.

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

#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