Question

Raphael was very happy last week until his boss suddenly called him yesterday, asking him to...

  1. Raphael was very happy last week until his boss suddenly called him yesterday, asking him to rewrite his code. Despite being terribly upset about it, he has finished up writing part of the code below. However, same as his predicament from last week, he doesn't understand how to complete the code to include functions for a) searching for a person and b) deleting the person at the front. Please help him complete his .cpp file ABOVE by defining the two remaining functions.

    He keeps the people in his queue inside a structure called node_t, which consists of the data part string name and the pointer part node* next. He is using a class named BookstoreQueue, which consists of three private data members: int size, node_t* head, node_t* tail. He has implemented the functions getSize(), printQueue(), and addToEnd() already. Please help him finish the methods positionOfPerson() and deleteFromFront().

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

typedef struct node
{
        string name;
        struct node* next;
}node_t;

class BookstoreQueue
{
        // Private data members:
        int size;
        node_t* head;
        node_t* tail;
        
        public:
        // Public functions:
        // 1. Function to return the current size of the queue.
        int getSize()
        {
                return size;
        }
        // 2. Function to print the queue starting from the head.
        void printQueue()
        {
                if(size==0)
                {
                        cout<<"EMPTY\n";
                        return;
                }
                node_t* p = head;
                for(int i=0; i<size-1; i++)
                {
                        cout<<"("<<p->name<<")->";
                        p = p->next;
                }
                cout<<"("<<p->name<<").\n";
        }
        // 3. Function that takes as input a string searchPerson, searches whether that person is found in the queue, and returns his/her position from the front. The frontmost person is the 1st person, and so on.
        // If searchPerson is not found in the queue, this function should return -1.
        // int positionOfPerson(string searchPerson);
        
        // 4. Function that adds a newPerson to the end of the queue.
        void addToEnd(string newPerson)
        {
                // Create a new Node
                node_t* newNode = new node_t;
                newNode->name = newPerson;
                newNode->next = NULL;
                if(size==0)             // If queue is empty:
                {
                        head = newNode;
                        tail = newNode;
                        size = 1;
                }
                else            // else queue had at least one element:
                {
                        tail->next = newNode;
                        tail = newNode;
                        size++;
                }
        }

        // 5. Function that deletes a person from the front of the queue. It returns the name of the person who was at the front.
        // string deleteFromFront();
        

        //Constructor:
        void BookStoreQueue()
        {
                size = 0;
                head = NULL;
                tail = NULL;
        }
};

int main()
{
        BookstoreQueue queue;
        cout<<"Current queue: ";
        queue.printQueue();
        queue.addToEnd("Muimi");
        cout<<"Added Muimi to queue.\nCurrent queue: ";
        queue.printQueue();
        queue.addToEnd("Kyouka");
        cout<<"Added Kyouka to queue.\nCurrent queue: ";
        queue.printQueue();
        /* Tester code for positionOfPerson():
        cout<<"Searching for Kyouka.\n";
        cout<<"Found Kyouka in position"<< queue.positionOfPerson("Kyouka");
        cout<<"\nSearching for Christina.\n";
        cout<<"positionOfPerson() returned "<< queue.positionOfPerson("Christina");
        */

        /* Tester code for deleteFromFront():
        string deletedPerson = queue.deleteFromFront();
        cout<<"Deleting the following person from the front: "<<deletedPerson<<"\nCurrent queue: ";
        queue.printQueue();
        */

        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Function for searching the position of the person : -

int positionOfPerson(string searchPerson)
   {
       node_t* temp=head; // Creating a temporary node to which is pointing to head of the queue.
       int pos=1; // Initialising the pos variable to 1 as the person at the head will be positioned as 1
       while(temp) // This loop will continue till the temp becomes NULL.
       {
           if(temp->name == searchPerson) // If the person is found then its position will be returned.
               return pos;
               temp=temp->next; // Otherwise we will move to the next person.
           pos++; // Position will be increased while moving to the next person.
       }
       return -1; // If person not found then -1 is returned.
   }

Function for deleting a person from the front of the queue :-

string deleteFromFront()
   {
       if(size==0)
           return "EMPTY"; // If no one is in the queue then return EMPTY.
       string first = head->name; // Sting variable first is initialised with the name of the person at front.
       node_t* temp = head; // Temporary node pointing to the head of the queue.This will help in deleting the //unused memory.
       head=head->next; // New head will be the next person to the head.
       delete(temp); // Deleting the unused memory.
       size--; // Now the size of the queue is reduced by one.
       return first; // The name of the person deleted from the queue is returned.
   }

Output :-

Function for searching the position of the person : -

int positionOfPerson(string searchPerson)
        {
            node_t* temp=head;              // Creating a temporary node to which is pointing to head of the queue.
            int pos=1;                               // Initialising the pos variable to 1 as the person at the head will be positioned as 1
            while(temp)                            // This loop will continue till the temp becomes NULL.
            {
                if(temp->name == searchPerson)      // If the person is found then its position will be returned.
                return pos;
                temp=temp->next;                              // Otherwise we will move to the next person.
                pos++;                                                // Position will be increased while moving to the next person.
            }
            return -1;                                                // If person not found then -1 is returned.
        }

Function for deleting a person from the front of the queue :-

string deleteFromFront()
        {
            if(size==0)
                return "EMPTY";                     // If no one is in the queue then return EMPTY.
            string first = head->name;          // Sting variable first is initialised with the name of the person at front.
            node_t* temp = head;                // Temporary node pointing to the head of the queue.This will help in deleting the                                                                         //unused memory.
            head=head->next;                     // New head will be the next person to the head.
            delete(temp);                             // Deleting the unused memory.
            size--;                                        // Now the size of the queue is reduced by one.
            return first;                                // The name of the person deleted from the queue is returned.
        }

Output :-

Note:-

The constructor should not have any return type.

Add a comment
Know the answer?
Add Answer to:
Raphael was very happy last week until his boss suddenly called him yesterday, asking him to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: ...

    Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of...

    PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode {   string data;   qNode* next;   qNode* prev; }; class CBQueue {   public:     CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( );   private:     qNode* front;     qNode* rear;     int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • in C++ creat a DynamicQueue class, add a new data member called count to trace the...

    in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT