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

//This is the main.cpp file #include <ctime> #include <sstream> #include <iostre

ID: 3802088 • Letter: #

Question

//This is the main.cpp file

#include <ctime>
#include <sstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include "queue.h"

using namespace std;

int main()
{
   Queue q;
   test(q.size() == 0);
   cout << "5% earned." << endl;

   q.pop();
   q.front();
   test(q.size() == 0);
   cout << "10% earned." << endl;

   q.push("hello");
   // Queue is now ["hello"]
   test(q.size() == 1);
   test(q.front() == "hello");
   cout << "20% earned." << endl;

   q.push("cat");
   // Queue is now ["cat", "hello"]
   test(q.size() == 2);
   test(q.front() == "hello");
   cout << "25% earned." << endl;

   q.push("bird");
   // Queue is now ["bird", "cat", "hello"]
   test(q.size() == 3);
   test(q.front() == "hello");
   cout << "30% earned." << endl;
  
   q.push("goodbye");
   // Queue is now ["goodbye", "bird", "cat", "hello"]
   test(q.size() == 4);
   test(q.front() == "hello");
   cout << "35% earned." << endl;

   q.pop();  
   // Queue is now ["goodbye", "bird", "cat"]
   test(q.size() == 3);
   test(q.front() == "cat");
   cout << "40% earned." << endl;

   q.pop();
   // Queue is now ["goodbye", "bird"]
   test(q.size() == 2);
   test(q.front() == "bird");
   cout << "45% earned." << endl;

   q.pop();
   // Queue is now ["goodbye"]
   test(q.size() == 1);
   test(q.front() == "goodbye");
   cout << "55% earned." << endl;

   q.pop();
   // Queue is now []
   test(q.size() == 0);
   cout << "65% earned." << endl;

   // Test whether popping off empty queue
   // doesn't cause problems
   q.pop();
   test(q.size() == 0);
   q.push("1");
   test(q.size() == 1);
   q.pop();
   test(q.size() == 0);
   cout << "70% earned." << endl;


   // Push a few special things at the front  
   q.push("first");
   q.push("second");
   q.push("third");

   // Time how long about 1000000 pushes takes
   clock_t start = clock();
   for (int i = 4; i <= 999999; ++i)
   {
       q.push("dog");
       test(q.size() == i);
   }
   clock_t end = clock();
   float push_elapsed = static_cast<float>(end - start) / CLOCKS_PER_SEC;
   q.push("last");
   test(q.size() == 1000000);

   // Test that the queue contents at the front are ok
   test(q.front() == "first");
   q.pop();
   test(q.front() == "second");
   q.pop();
   test(q.front() == "third");
   q.pop();

   // Time how long about 100000 pops takes
   start = clock();
   for (int i = 999997; i >= 2; --i)
   {
       test(q.size() == i);
       q.pop();
   }
   end = clock();
   float pop_elapsed = static_cast<float>(end - start) / CLOCKS_PER_SEC;
   test(q.front() == "last");
   cout << "100% earned." << endl;

   // Print a score (just for fun)
   cout << "Pushing ~1000000 elements took " << push_elapsed << " seconds." << endl;
   cout << "Popping ~1000000 elements took " << pop_elapsed << " seconds." << endl;
}
//**********************************************************************************************
// ***Edit the queue.cpp file***

//This is the queue.cpp file

#include "queue.h"

Queue :: Queue()
{
   // This already works
   head = tail = 0;
   count = 0;
}

void Queue :: push(string s)
{
   // EDIT HERE
}

void Queue :: pop()
{
   // EDIT HERE  
}

string Queue :: front()
{
   // This already works
   if (head != 0)
       return head->s;
   return "";
}

int Queue :: size()
{
   // This already works
   return count;
}
//**************************************************************

//This is the queue.h header file

#ifndef QUEUE_H
#define QUEUE_H

#include <string>

using namespace std;

class Queue
{
        public:
                Queue();
                void push(string s);
                void pop();
                string front();
                int size();

        private:
       class Node
       {
           public:
               string s;
               Node* next;
       };

       Node* head;
       Node* tail;
       int count;
};

#endif

-----------------------------------------------------------------------------

Please edit the queue.cpp file in order for it to pass all assertions in the main.cpp file

Explanation / Answer

Answer:

#include<iostream>
using namespace std;
struct node
{
    int info;
    node *later;
}*front_end = NULL,*rear_end = NULL,*p = NULL,*node_pointer = NULL;
void push(int value)
{
    node_pointer = new node;
    node_pointer->info = value;
    node_pointer->later = NULL;
    if(front_end == NULL)
    {
        front_end = rear_end = node_pointer;
        rear_end->later = NULL;
    }
    else
    {
        rear_end->later = node_pointer;
        rear_end = node_pointer;
        rear_end->later = NULL;
    }
}
int remove()
{
    int value;
    if(front_end == NULL)
    {
        cout<<" queue is empty ";
    }
    else
    {
        p = front_end;
        value = p->info;
        front_end = front_end->later;
        delete(p);
        return(value);
    }
}
int main()
{
    int n,c = 0,value;
    cout<<"Enter the number of values to be pushed into queue ";
    cin>>n;
    while (c < n)
    {
   cout<<"Enter the value to be entered into queue ";
   cin>>value;
        push(value);
        c++;
     }
     cout<<" Deleted Values ";
     while(true)
     {
        if (front_end != NULL)
            cout<<remove()<<endl;
        else
            break;
     }
   
}