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

in c++ 1. Write a class called cube. This class will have the members volume and

ID: 674321 • Letter: I

Question

in c++

1. Write a class called cube. This class will have the members volume and surface area.
2. Create a boxNode class for a Foward Linked List
3. Create a linked list and store the values from cubes.txt in it. Store the cubes in the linked list in acending order according to volume
4. Print the the linked list in the following format: "Name, Volume: ##.#"
5. Create a recursive function that prints the list in reverse order.
6. Print the function using the recursive print function in the same format: "Name, Volume: ##.#"
Extra Credit.
7. Write a recursive function that reverses the order of the linked list. Your can check if this is correct by using the regular print function
your created and the values will print in decending order.
8. Forward Print the linked list.

cubes.txt
//
One.5.9.4
Two.7.3.9
Three.1.3.7
Four.6.3.2
Five.8.1.4
Six.3.7.5
Seven.5.5.5
Eight.2.5.3
Nine.2.6.8
Ten.7.3.4
//

Explanation / Answer

1)#include <iostream>
using namespace std;

int findarea(int length, int width = 20, int height = 12);

int main()
{
int length = 100;
int width = 50;
int height = 12;
int area;

area = findarea(length, width, height);
cout << "1st Area : " << area << " ";

area = findarea(length, width);
cout << "2nd Area : " << area << " ";

area = findarea(length);
cout << "3rd Area : " << area << " ";

system ("pause");
return 0;
}


int findarea(int length, int width, int height)
{
return (length * width * height);
}

for the programs 3,4 ,5 ,6 ,7 8

#include <iostream>

using namespace std;

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


void initNode(struct Node *head,int n){
   head->data = n;
   head->next =NULL;
}


void addNode(struct Node *head, int n) {
   Node *newNode = new Node;
   newNode->data = n;
   newNode->next = NULL;

   Node *cur = head;
   while(cur) {
       if(cur->next == NULL) {
           cur->next = newNode;
           return;
       }
       cur = cur->next;
   }
}

void insertFront(struct Node **head, int n) {
   Node *newNode = new Node;
   newNode->data = n;
   newNode->next = *head;
   *head = newNode;
}

struct Node *searchNode(struct Node *head, int n) {
   Node *cur = head;
   while(cur) {
       if(cur->data == n) return cur;
       cur = cur->next;
   }
   cout << "No Node " << n << " in list. ";
}

bool deleteNode(struct Node **head, Node *ptrDel) {
   Node *cur = *head;
   if(ptrDel == *head) {
       *head = cur->next;
       delete ptrDel;
       return true;
   }
  
   while(cur) {
       if(cur->next == ptrDel) {
           cur->next = ptrDel->next;
           delete ptrDel;
           return true;
       }
       cur = cur->next;
   }
   return false;
}


struct Node* reverse(struct Node** head)
{
   Node *parent = *head;
   Node *me = parent->next;
   Node *child = me->next;

  
   parent->next = NULL;
   while(child) {
       me->next = parent;
       parent = me;
       me = child;
       child = child->next;
   }
   me->next = parent;
   *head = me;
   return *head;
}


void copyLinkedList(struct Node *node, struct Node **pNew)
{
   if(node != NULL) {
       *pNew = new Node;
       (*pNew)->data = node->data;
       (*pNew)->next = NULL;
       copyLinkedList(node->next, &((*pNew)->next));
   }
}


int compareLinkedList(struct Node *node1, struct Node *node2)
{
   static int flag;

  
   if(node1 == NULL && node2 == NULL) {
       flag = 1;
   }
   else {
       if(node1 == NULL || node2 == NULL)
           flag = 0;
       else if(node1->data != node2->data)
           flag = 0;
       else
           compareLinkedList(node1->next, node2->next);
   }

   return flag;
}

void deleteLinkedList(struct Node **node)
{
   struct Node *tmpNode;
   while(*node) {
       tmpNode = *node;
       *node = tmpNode->next;
       delete tmpNode;
   }
}

void display(struct Node *head) {
   Node *list = head;
   while(list) {
       cout << list->data << " ";
       list = list->next;
   }
   cout << endl;
   cout << endl;
}

int main()
{
   struct Node *newHead;
   struct Node *head = new Node;
  
   initNode(head,10);
   display(head);

   addNode(head,20);
   display(head);

   addNode(head,30);
   display(head);

   addNode(head,35);
   display(head);

   addNode(head,40);
   display(head);

   insertFront(&head,5);
   display(head);

   int numDel = 5;
   Node *ptrDelete = searchNode(head,numDel);
   if(deleteNode(&head,ptrDelete))
       cout << "Node "<< numDel << " deleted! ";
   display(head);

   cout << "The list is reversed ";
   reverse(&head);
   display(head);

   cout << "The list is copied ";
   copyLinkedList(head,&newHead);
   display(newHead);

   cout << "Comparing the two lists... ";
   cout << "Are the two lists same? ";
   if(compareLinkedList(head,newHead))
       cout << "Yes, they are same! ";
   else
       cout << "No, they are different! ";
   cout << endl;

   numDel = 35;
   ptrDelete = searchNode(newHead,numDel);
   if(deleteNode(&newHead,ptrDelete)) {
       cout << "Node "<< numDel << " deleted! ";
       cout << "The new list after the delete is ";
       display(newHead);
   }
   cout << "Comparing the two lists again... ";
   cout << "Are the two lists same? ";
   if(compareLinkedList(head,newHead))
       cout << "Yes, they are same! ";
   else
       cout << "No, they are different! ";
  
   cout << endl;
   cout << "Deleting the copied list ";
   deleteLinkedList(&newHead);
   display(newHead);
   return 0;
}