This C++ CODE has many functions that needs to be tested piece by piece For example, you...

70.2K

Verified Solution

Question

Programming

This C++ CODE has many functions that needs to be testedpiece by piece

For example, you going to display each function with [ ]and a , separating each piece of data, like so:[1,2,3,4]?

results must be copied and pasted as commnets at thebottom of this file

this is the code:

#include

#include

#include //for NULL

using namespace std;

class List

{

   private:

       struct Node

       {

           int data;

           Node*next;

           Node(intdata): data(data), next(NULL){}

       };

       typedef struct Node*Nodeptr;

       Nodeptr first;

       Nodeptr last;

       int size;

   public:

       /**Constructors */

       List()

       {

              first = NULL;

              last = NULL;

              size = 0;

       }

       /* Destructors*/

       ~List()

       {

           Nodeptrcursor = first;

           Nodeptrtemp;

           while (cursor!= NULL)

           {

              temp = cursor -> next;

              delete cursor;

              cursor = temp;

           }

       }

       //ANOTHER WAY TO WRITE

       //AND YES IT WORKS

              /* last = NULL;

              while(first != NULL)

              {

                  Nodeptr temp = first;

                  first = first->next;

                  delete temp;

              } /*

       /**Accessors*/

       int getFirst()

       {

          if(isEmpty())

              return 0;

           returnfirst->data;

       }

       int getLast()

       {

          if(isEmpty())

              return 0;

           returnlast->data;

       }

       bool isEmpty()

       {

           if(first ==NULL)

           {

              return true;

           }

              else return false;

       }

       int getSize()

       {

           return size;// return the size of the list

       }

       /**Manipulation Procedures*/

       void removeLast()

       {

           if(isEmpty()== false) // if list is not empty

           {

              if(first -> next == NULL) // list has only 1node

              {

                  delete first;

                  first = NULL;

                  last = NULL;

              }

              else

              {

                  Nodeptr temp = first;

                  while(temp -> next !=last)

                  {

                      temp =temp -> next; //advance the pointer

                  }

                  delete last; //free thememory for the original last node

                  temp -> next = NULL; //solast->next is not pointing at freed memory

                  last = temp;

                  size -- ; // decreasingsize

              }

           }

       }

       void removeFirst()

       {

           if (size ==0)

           {

              cout << \"removeFirst: List is empty. nodata to remove.\" << endl;

           }

           else if (size== 1)

           {

              delete first;

              first = last = NULL;

              size = 0;

           }

           else

           {

              Nodeptr temp = first; //store pointer to firstso we dont lose access

              first = first -> next; //advance thepointer

              delete temp; //free the memory for the originalfirst node

              size--;

           }

       }

       /*{

           if(isEmpty()== false)

           {

              if(first -> next == NULL)// list has only onenode

              {

                  first = NULL;

                  last = NULL;

                  delete first;

              }

              else

              {

                  Nodeptr temp = first;

                  first = first -> next;

                  delete temp;

              }

              size = size - 1;

           }

       } */

       void insertLast(int data)

       {

           if(first ==NULL)

           {

              first = new Node(data);

              last = first;

           }

           else

           {

              last -> next = new Node(data);

              last = last -> next;

           }

           size++;

       }

       void insertFirst(int data)

       {

           if (size ==0)

           {

              first = new Node(data);

              last = first;

           }

           else

           {

              Nodeptr N = new Node(data);

              N -> next = first;

              first = N;

           }

           size ++;

           /*if(first ==NULL)

           {

              first = new Node(data);

              last = first;

           }

           else

           {

              Nodeptr temp = new Node(data);

              temp -> next = first;

              first = temp;

           }

           size++;*/

       }

       /**Additional ListOperations*/

       void printList()

       {

           Nodeptr temp= first;

           while(temp !=NULL)

           {

              cout << temp -> data <<\" \";

              temp = temp -> next;

           }

           cout <

       }

       };

int main()

{

   List list;

   list.insertFirst(2);

   list.insertFirst(4);

   list.printList();

   list.removeFirst();

   list.printList();

   cout<<\"Size:\"<

   return 0;

}

Answer & Explanation Solved by verified expert
4.1 Ratings (838 Votes)
include include include for NULLusing namespace stdclass Listprivatestruct Nodeint dataNode nextNodeint data datadata nextNULLtypedef struct Node NodeptrNodeptr firstNodeptr lastint sizepublicConstructors Listfirst NULLlast NULLsize 0 DestructorsListNodeptr cursor firstNodeptr tempwhile    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students