Question

In: Computer Science

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

This C++ CODE has many functions that needs to be tested piece 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 the bottom of this file

this is the code:

#include <iostream>

#include <string>

#include <cstddef> //for NULL

using namespace std;

class List

{

   private:

       struct Node

       {

           int data;

           Node* next;

           Node(int data): 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()

       {

           Nodeptr cursor = first;

           Nodeptr temp;

           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;

           return first->data;

       }

       int getLast()

       {

           if(isEmpty())

               return 0;

           return last->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 1 node

               {

                   delete first;

                   first = NULL;

                   last = NULL;

               }

               else

               {

                   Nodeptr temp = first;

                   while(temp -> next != last)

                   {

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

                   }

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

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

                   last = temp;

                   size -- ; // decreasing size

               }

           }

       }

       void removeFirst()

       {

           if (size == 0)

           {

               cout << "removeFirst: List is empty. no data to remove." << endl;

           }

           else if (size == 1)

           {

               delete first;

               first = last = NULL;

               size = 0;

           }

           else

           {

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

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

               delete temp; //free the memory for the original first node

               size--;

           }

       }

       /*{

           if(isEmpty() == false)

           {

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

               {

                   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 List Operations*/

       void printList()

       {

           Nodeptr temp = first;

           while(temp != NULL)

           {

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

               temp = temp -> next;

           }

           cout << endl;

       }

       };

int main()

{

   List list;

   list.insertFirst(2);

   list.insertFirst(4);

   list.printList();

   list.removeFirst();

   list.printList();

   cout<<"Size: "<<list.getSize()<<endl;

   return 0;

}

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <cstddef> //for NULL


using namespace std;


class List
{

private:
struct Node
{
int data;
Node* next;

Node(int data): 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()
{
Nodeptr cursor = first;
Nodeptr temp;

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;

return first->data;
}


int getLast()
{
if(isEmpty())
return 0;

return last->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 1 node
{
delete first;
first = NULL;
last = NULL;
}

else
{

Nodeptr temp = first;

while(temp -> next != last)
{
temp = temp -> next; //advance the pointer

}

delete last; //free the memory for the original last node
temp -> next = NULL; //so last->next is not pointing at freed memory
last = temp;

size -- ; // decreasing size
}

}
}



void removeFirst()
{
if (size == 0)
{
cout << "removeFirst: List is empty. no data to remove." << endl;
}

else if (size == 1)
{
delete first;
first = last = NULL;
size = 0;

}

else
{
Nodeptr temp = first; //store pointer to first so we dont lose access
first = first -> next; //advance the pointer
delete temp; //free the memory for the original first node
size--;
}
}

/*{
if(isEmpty() == false)
{

if(first -> next == NULL)// list has only one node
{
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 ++;
  
  
}


/**Additional List Operations*/

void printList()
{
Nodeptr temp = first;
cout <<"[";
while(temp != NULL)
{
cout << temp -> data <<",";
temp = temp -> next;
}
cout <<"]"<< endl;
}

};


int main()
{

List list;
list.insertFirst(1);
list.printList(); // this should print [1]
  
list.insertFirst(2);
list.printList(); // this should print [2,1]
  
list.insertLast(3);
list.printList(); // this should print [2,1,3]
  
list.insertFirst(4);
list.printList(); // this should print [4,2,1,3]
  

list.removeFirst();
list.printList(); // this should print [2,1,3]

cout<<"Size: "<<list.getSize()<<endl;
return 0;
}


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has asked for your help. Write a program that will accept a pirate’s starting amount of treasure in number of gold pieces. The program will then run one of two simulations, indicated by the user: 1) The first simulation runs indefinitely, until one of two conditions is met: the pirate’s treasure falls to 0 or below, or the pirate’s treasure grows to 1000 or above....
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
In SAS code please. Tennis balls are tested in a machine to show how many bounces...
In SAS code please. Tennis balls are tested in a machine to show how many bounces they can withstand before they fail to bounce 30% of their dropping height. Two brands of balls (W and P) are compared. In addition, the effect of shelf life on these brands is tested. Half of the balls of each brand are 6 months old, the other half, fresh. Using a two-way analysis of variance, what conclusions can you reach? The data are shown...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
SDES C++ The code is not working currently I know the functions are correct but the...
SDES C++ The code is not working currently I know the functions are correct but the original plaintext should match the "ciphertext after" when inputting 4 #include <iostream> #include <iomanip> #include <string> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <time.h> #include <windows.h> using namespace std; //function prototypes void splitString(string &x, string &y, string &original); void expansionFunction(string &input); string XORstring(string& str1, string& str2); string getSBOXValue(string arr[][8], string val); int binaryToDecimal(int n); string generateKey(); string findKey(string Key, int round); string Encryption(string &plaintext,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT