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

(EDITED) The following is a c++ program with issues in the functions InsertBaske

ID: 3667565 • Letter: #

Question

(EDITED) The following is a c++ program with issues in the functions InsertBasket, and DeleteBasket. If you can show me what the issues are, how to fix them (the code) and why then that would be amazing!

Functions File:

#include

#include

#include

#include "BasketList.h"

using namespace std;

//---------------------------------------------------

//Basket Members

//---------------------------------------------------

Basket::Basket(int _datum, Basket * _next):

   egg_num(_datum), nextBasket(_next)

{}

int Basket::getEggs() const

{

   return egg_num;

}

Basket const* Basket::getNextBasket() const

{

   return nextBasket;

}

BasketList::BasketList() :

    head (NULL)

{}

void BasketList::insertBasket(int eggs){

   Basket *currPtr = head;

   Basket *prevPtr = NULL;

   Basket *newBasketPtr;      //points to a new basket

   while(currPtr != NULL && eggs > currPtr->egg_num){

      prevPtr = currPtr;

      currPtr = currPtr->nextBasket;

   }

   newBasketPtr = new Basket(eggs, currPtr);

   prevPtr->nextBasket = newBasketPtr;

}

void BasketList::makeBasketList()

{

   const int INITIALBASKETS = 5;

   int eggValue;

   Basket *newBasketPtr;

   srand(time(0));

   for (int i=0; i < INITIALBASKETS; i++)

   {

       eggValue = rand() % (MAXEGGS + 1);

       insertBasket(eggValue);

   }

}

void BasketList::deleteBasket (int eggs)

{

   Basket *delPtr = head;

   Basket *prevPtr = NULL;

   // If the list is empty, then do nothing;

   if (delPtr == NULL)

        return;

   //Treat the first basket as a special case... head needs to be updated

   if (head->egg_num == eggs)

   {

        head = head->nextBasket;

   }

   else

   {

        prevPtr = delPtr;

        //Look for the basket to be deleted

        while (delPtr != NULL && delPtr->egg_num < eggs)

        {

            prevPtr = delPtr;

            delPtr = delPtr->nextBasket;

        }

        prevPtr->nextBasket = delPtr->nextBasket;

   }

   //Delete the node

   delete delPtr;

}

void BasketList::printBasketList ()

{

    Basket *currPtr;

    currPtr = head;

    int count = 0;

    if (currPtr == NULL)

    {

        cout << "Empty! Collect more..." << endl;

        return;

    }

    while (currPtr != NULL)

    {

        count++;

        cout << "Basket " << count << " contains " << currPtr->egg_num << " eggs." << endl;

        currPtr = currPtr->nextBasket;

    }

}

Header File:

#ifndef BASKETLIST_H

#define BASKETLIST_H

#include

#include

using namespace std;

const int MAXEGGS = 500;

class BasketList; // needed for friend line below

class Basket

{

private:

    int egg_num;

    Basket* nextBasket;

public:

    Basket(int, Basket*);

    int getEggs() const;

    Basket const* getNextBasket() const;

    friend class BasketList; // gives BasketList access to egg_num and nextBasket

};

class BasketList

{

private:

    Basket* head;

public:

    BasketList ();

    void insertBasket (int);

    void makeBasketList ();

    void deleteBasket (int);

    void printBasketList ();

};

#endif /* BASKETLIST_H */

MAIN:

#include<iostream>

#include<string>

#include"BasketList.h"

using namespace std;

int readEggsInBasket(string);

int main()

{

   char choice;

   int egg_in_a_basket;

   BasketList BL_of_bunny;

   cout << "This program demonstrates the collection of baskets for the Easter Bunny. " <<$

   cout << "Initially, you will be asked to create the basket list for the bunny." << endl;

   cout << "You will be later prompted to manipulate the basket list." <<endl << endl;

   BL_of_bunny.makeBasketList();

   cout << "The basket collection for the Easter Bunny are in order: "<< endl;

   BL_of_bunny.printBasketList();

   do

   {

      cout << "*******************************************************" << endl;

      cout<<"i: Insert a new basket" << endl;

      cout<<"d: Delete (Delete a basket with the given number of eggs)" << endl;

      cout<<"p: Print (Display the number of eggs in each basket from the basket list)" <$

      cout<<"q: Quit   (Quit the program)" << endl;

      cout << "*******************************************************" << endl << endl;

      cout<< endl << "Please enter your choice here:";

      cin>>choice;

      cin.ignore(256, ' ');

               switch(choice)

      {

         case 'i':

            egg_in_a_basket = readEggsInBasket("to insert:");

            BL_of_bunny.insertBasket(egg_in_a_basket);

            break;

         case 'd':

            egg_in_a_basket = readEggsInBasket("to delete:");

            BL_of_bunny.deleteBasket(egg_in_a_basket);

            break;

         case 'p':

            cout << "The basket collection for the Easter Bunny are in order: "<< endl;

            BL_of_bunny.printBasketList();

            break;

         case 'q':

            break;

         default:

            cout<<" Invalid choice. Please try again. ";

            break;

}

   } while(choice != 'q');

   cout<< "Thank you for using this program" << endl;

   return 0;

}

int readEggsInBasket(string descr)

{

   int eggs;

   cout << endl << "Please enter the number of eggs in the basket " << descr;

   cin >> eggs;

   cin.ignore(256, ' ');

   while(cin.fail())

   {

       cin.clear();

       cin.ignore(256,' ');

       cout << endl << "Please enter the number of eggs in the basket " << descr;

       cin >> eggs;

       cin.ignore(256,' ');

   }

return eggs;

}

Explanation / Answer

include #include #include #include "BasketList.h" using namespace std; Basket::Basket(int _datum, Basket * _next): egg_num(_datum), nextBasket(_next) {} int Basket::getEggs() const { return egg_num; } Basket const* Basket::getNextBasket() const { return nextBasket; } BasketList::BasketList() : head (NULL) {} void BasketList::insertBasket(int eggs) { Basket *currPtr = head; Basket *prevPtr = NULL; Basket *newBasketPtr; if(eggs egg_num) { currPtr->nextBasket = head; head = currPtr; } while(currPtr != NULL && eggs > currPtr->egg_num) { prevPtr = currPtr; currPtr = currPtr->nextBasket; } newBasketPtr = new Basket(eggs, currPtr); prevPtr->nextBasket = newBasketPtr; }