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

File name: a2.cpp Task For this assignment, you are required to either A) modify

ID: 3789923 • Letter: F

Question

File name: a2.cpp

Task

For this assignment, you are required to either A) modify your linked list code to implement a queue or B) modify the provided stack code to implement a queue. 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” which will instruct your program what operations to run

File Format

<command> <0 or 1 arguments>

Commands

-          1            enq

-          2            deq

-          3            front

-          4            isEmpty

-          5            printq

Example File

1 1

5

1 2

5

1 3

5

1 4

5

3

2

5

2

5

3

Expectations

You should not use any already pre implemented code for your queue such as a standard library object

Your code should be well formatted with proper spacing and proper naming

Your code should have well named variables. No a’s b’s or c’s as names unless it is for something like a loop counter

Your code should have the same output formatting you see below

Your code MUST follow the formatting you see below

Your file MUST be named a2.cpp

My example file does not contain all possible test cases it is up to you to determine any other special cases to test your program against.

Example Output

//LinkedList class
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <sstream>

using namespace std;

struct Node
{
   int data;
   Node *next;
};

class LinkedList
{
private:
   Node *head;
   Node *tail;
public:
   LinkedList(); //constructor
   void prepend(int num);
   void append(int num);
   void removeFront();
   void removeBack();
   void remove(int num);
   int search(int num);
   void printList();  
};
//constructor
LinkedList::LinkedList()
{
   head=NULL;
   tail=NULL;  
}
//prepend() method
void LinkedList::prepend(int num)
{
   //allocate space for new node
   Node *node=new Node;
  
   //populate node
   node->data=num;
   node->next= NULL;
  
   //attach new node to start of list
   //if linked list empty
   if(head==NULL)
   {
       head=node;
       tail=node;
   }
   else //linked list not empty
   {
       node->next=head;
       head=node;
   }
}
//append() method
void LinkedList::append(int num)
{
   //allocate space for new node
   Node *node=new Node;
  
   //populate node
   node->data=num;
   node->next= NULL;
  
   //attach new node to end of list
   //if linked list empty
   if(head==NULL)
   {
       head=node;
       tail=node;
   }
   else //linked list not empty
   {
       tail->next=node;
       tail=node;
   }
}
//removeFront() method
void LinkedList::removeFront()
{
   //if linked list empty, then underflow
   if(head==NULL)
   {
       cerr<<"Underflow"<<endl;
       return;
   }
   //preserve front (head) node
   Node *temp=head;
  
   //detach node from linked list
   head=head->next;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
//removeBack() method
void LinkedList::removeBack()
{
   //if linked list empty, then underflow
   if(head==NULL)
   {
       cerr<<"Underflow"<<endl;
       return;
   }
   //preserve back (tail) node
   Node *temp=tail;
  
   //detach node from linked list
   Node *curr=head;
   Node *prev=NULL;
   while(curr->next!=NULL)
   {
       /*if(curr==tail)
           break;*/
       prev=curr;
       curr=curr->next;
   }
   prev->next=NULL;
   tail=prev;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
  
//remove() method
void LinkedList::remove(int num)
{
   //first locate node
   Node *curr=head;
   Node *prev=NULL;
   while(curr!=NULL)
   {
       if(curr->data=num)
           break;
       prev=curr;
       curr=curr->next;
   }
  
   //preserve node
   Node *temp=curr;
  
   //detach node from linked list
   prev->next=curr->next;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
//search() method
int LinkedList::search(int num)
{
   int found=0; //assuming num not found
   //locate node containing num
   Node *curr=head;
   while(curr!=NULL)
   {
       if(curr->data=num)
       {
           found=1; //num found
           break;
       }          
       curr=curr->next;
   }
  
   return found;
}
//printList() method
void LinkedList::printList()
{
   //traverse linked list
   Node *curr=head;
   while(curr!=NULL)
   {
       cout<<curr->data<<"->";
       curr=curr->next;
   }
}
---------------------------
main() method
------------------------------
    //read data from file
   string line;
   while(getline(infile,line))
   {
       stringstream ss(line);
       int op; //operation to be performed
       ss>>op;
       int num;
       switch(op)
       {
           case 1:
               ss>>num;
               cout<<"Prepending "<<num<<" onto the list"<<endl;
               ll.prepend(num);
               break;
           case 2:
               ss>>num;
               cout<<"Appending "<<num<<" onto the list"<<endl;
               ll.append(num);
               break;
           case 3:
               ll.removeFront();
               break;
           case 4:
               ll.removeBack();
               break;
           case 5:
               ss>>num;
               if(ll.search(num)==1)
                   cout<<"Found "<<num<<" in the list."<<endl;
               else
                   cout<<"Not found "<<num<<" in the list."<<endl;
               break;
           case 6:
               cout<<"Your list:";
               ll.printList();
               cout<<endl;
               break;
           case 7:
               ss>>num;
               ll.remove(num);
               break;
       }      
   }
   infile.close();
   return 0;
}

Back K-- Front 1 Front Back 2 1 Front Back 3 2 1 K-- Front Back 4 3 2 1 Front Front 1 De q 1. Front Back 2 4 3 2 K- Deq Back 4 3 K-- Front Front 3 Queue is not empty Back 4 3 Front Back 4 3 Front

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <sstream>

using namespace std;

struct Node
{
   int data;
   Node *next;
};

class LinkedList
{
private:
   Node *head;
   Node *tail;
public:
   LinkedList(); //constructor
   void prepend(int num);
   void append(int num);
   void removeFront();
   void removeBack();
   void remove(int num);
   int search(int num);
   void printList();  
};
//constructor
LinkedList::LinkedList()
{
   head=NULL;
   tail=NULL;  
}
//prepend() method
void LinkedList::prepend(int num)
{
   //allocate space for new node
   Node *node=new Node;
  
   //populate node
   node->data=num;
   node->next= NULL;
  
   //attach new node to start of list
   //if linked list empty
   if(head==NULL)
   {
       head=node;
       tail=node;
   }
   else //linked list not empty
   {
       node->next=head;
       head=node;
   }
}
//append() method
void LinkedList::append(int num)
{
   //allocate space for new node
   Node *node=new Node;
  
   //populate node
   node->data=num;
   node->next= NULL;
  
   //attach new node to end of list
   //if linked list empty
   if(head==NULL)
   {
       head=node;
       tail=node;
   }
   else //linked list not empty
   {
       tail->next=node;
       tail=node;
   }
}
//removeFront() method
void LinkedList::removeFront()
{
   //if linked list empty, then underflow
   if(head==NULL)
   {
       cerr<<"Underflow"<<endl;
       return;
   }
   //preserve front (head) node
   Node *temp=head;
  
   //detach node from linked list
   head=head->next;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
//removeBack() method
void LinkedList::removeBack()
{
   //if linked list empty, then underflow
   if(head==NULL)
   {
       cerr<<"Underflow"<<endl;
       return;
   }
   //preserve back (tail) node
   Node *temp=tail;
  
   //detach node from linked list
   Node *curr=head;
   Node *prev=NULL;
   while(curr->next!=NULL)
   {
       /*if(curr==tail)
           break;*/
       prev=curr;
       curr=curr->next;
   }
   prev->next=NULL;
   tail=prev;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
  
//remove() method
void LinkedList::remove(int num)
{
   //first locate node
   Node *curr=head;
   Node *prev=NULL;
   while(curr!=NULL)
   {
       if(curr->data=num)
           break;
       prev=curr;
       curr=curr->next;
   }
  
   //preserve node
   Node *temp=curr;
  
   //detach node from linked list
   prev->next=curr->next;
  
   //delete node
   cout<<"Removing "<<temp->data<<" from the list"<<endl;
   delete temp;
}
//search() method
int LinkedList::search(int num)
{
   int found=0; //assuming num not found
   //locate node containing num
   Node *curr=head;
   while(curr!=NULL)
   {
       if(curr->data=num)
       {
           found=1; //num found
           break;
       }          
       curr=curr->next;
   }
  
   return found;
}
//printList() method
void LinkedList::printList()
{
   //traverse linked list
   Node *curr=head;
   while(curr!=NULL)
   {
       cout<<curr->data<<"->";
       curr=curr->next;
   }
}
---------------------------
main() method
------------------------------
    //read data from file
   string line;
   while(getline(infile,line))
   {
       stringstream ss(line);
       int op; //operation to be performed
       ss>>op;
       int num;
       switch(op)
       {
           case 1:
               ss>>num;
               cout<<"Prepending "<<num<<" onto the list"<<endl;
               ll.prepend(num);
               break;
           case 2:
               ss>>num;
               cout<<"Appending "<<num<<" onto the list"<<endl;
               ll.append(num);
               break;
           case 3:
               ll.removeFront();
               break;
           case 4:
               ll.removeBack();
               break;
           case 5:
               ss>>num;
               if(ll.search(num)==1)
                   cout<<"Found "<<num<<" in the list."<<endl;
               else
                   cout<<"Not found "<<num<<" in the list."<<endl;
               break;
           case 6:
               cout<<"Your list:";
               ll.printList();
               cout<<endl;
               break;
           case 7:
               ss>>num;
               ll.remove(num);
               break;
       }      
   }
   infile.close();
   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