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

The following code below must be edited to be able to run using Linked list inst

ID: 3792157 • Letter: T

Question

The following code below must be edited to be able to run using Linked list instead of using arrays

---------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>

using namespace std;
//Class to hold due date
class Date
{
public:
  
   int m, d, y;//month,day,year
                 
       //default constructor
   Date()
   {
   }
   //parameterized constructor
   Date(int a, int b, int c)
   {
       m = a;
       d = b;
       y = c;
   }
};
class Homework
{

public:
   Date da;
   int assgn_id;
   Homework()
   {
   }
   Homework(int id, int m, int d, int y)
   {
       assgn_id = id;
       Date t(m, d, y);
       da = t;
   }
   //Displat id and due date of assignment
   void Display()
   {
       cout << " Assignment ID " << assgn_id << endl;
       cout << "Assignment Due date " << da.d << "/" << da.m << "/" << da.y << endl;
   }
};

int main(int argc, char const *argv[])
{
   int flag = 1;

   Homework *a[10];
   int count = 0;
   int ch;
   int id;
   int m, d, y;
   int c = 0;
   while (flag)
   {
       cout << " 1)Add assignment 2)Delete assignment 3)Display List of assignments 4)Exit ";
       cin >> ch;
       switch (ch)
       {
       case 1:cout << " Enter assignment id ";
           cin >> id;
           cout << "Enter due day ";
           cin >> d;
           cout << "Enter due month ";
           cin >> m;
           cout << "Enter due year ";
           cin >> y;
           a[count] = new Homework(id, d, m, y);

           count++;
           break;
       case 2:
           cout << "Enter ID to Delete assignment ";
           cin >> id;

           for (int i = 0; i < count; ++i)
           {

               if (a[i]->assgn_id == id)
               {
                   for (int j = i; j < count - 1; ++j)
                   {
                       a[j] = a[j + 1];
                   }
                   c++;
                   count--;
                   break;
               }
           }
           if (c == 0)
               cout << "Assignment not found ";
           else
               cout << "Assignment Deleted ";

           break;
       case 3:
           for (int i = 0; i < count; ++i)
           {
               a[i]->Display();
           }
           break;
       case 4:flag = 0; break;
       }
   }

   system("pause");
   return 0;
}

/*
Output


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

1

Enter assignment id
1
Enter due day
2
Enter due month
3
Enter due year
1980


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

1

Enter assignment id
2
Enter due day
3
Enter due month
5
Enter due year
1475


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

1

Enter assignment id
3
Enter due day

6
Enter due month
5
Enter due year
6846


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

3

Assignment ID 1
Assignment Due date 3/2/1980

Assignment ID 2
Assignment Due date 5/3/1475

Assignment ID 3
Assignment Due date 5/6/6846


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

2
Enter ID to Delete assignment
2
Assignment Deleted


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

3

Assignment ID 1
Assignment Due date 3/2/1980

Assignment ID 3
Assignment Due date 5/6/6846


1)Add assignment
2)Delete assignment
3)Display List of assignments
4)Exit

*/

Explanation / Answer

// Representation of both the classes is same as you have mentioned

/* Intially i have written the same code for declaring your classes then i have written 4 functions createNode, addAssignment, deleteAssignment, dispalyAssignment

*/

#include<iostream>

using namespace std;
//Class to hold due date
class Date
{
public:
  
   int m, d, y;//month,day,year
                 
       //default constructor
   Date()
   {
   }
   //parameterized constructor
   Date(int a, int b, int c)
   {
       m = a;
       d = b;
       y = c;
   }
};
class Homework
{

public:
   Date da;
   int assgn_id;
   Homework()
   {
   }
   Homework(int id, int m, int d, int y)
   {
       assgn_id = id;
       Date t(m, d, y);
       da = t;
   }
   //Displat id and due date of assignment
   void Display()
   {
       cout << " Assignment ID " << assgn_id << endl;
       cout << "Assignment Due date " << da.d << "/" << da.m << "/" << da.y << endl;
   }
};

// Now implementing Everything through Linked List

/* Let the linked list structure node is "node" which contains Homwwork object as data and next node to the next Homework node

*/

struct node {

Homework hw;

node *next;

};

node *head = NULL; // this is the Global Head node of the List

// This function will create a new node whenever we need

node * createNode(HomeWork homework){

node * newNode = (newNode *) malloc(sizeOf(node));

newNode -> hw = homework;

newNode->next = NULL;

return newNode;

}

// Add Assignment

// This function will add new assignment in the list

void addAssignment(node * newNode){

if(head == NULL){

head = newNode;

}

else{

node *temp = head;

while(temp->next != NULL){

temp = temp->next;

}

temp->next = newNode;

free(temp); // release the temp memory

}

void displayAssignment(){

node *temp = head;

while(temp != NULL){

cout<<"Assignment Id " << temp->hw.assgn_id<<endl;

cout<<"Assignment Due Date " << temp->hw.da.d<<"/"<<temp->hw.da.m<<"/"<<temp->hw.da.y<<endl;

}

}

void deleteAssignment(int id){

if(head==NULL){

cout<<"List is Empty";

}

node *temp = head;

node *preTemp = NULL;

while(temp != NULL){

if(temp->hw.assign_id == id && preTemp == NULL){

// That means we have to remove first element

head = head->next;

free(temp);

}

// If its not the first element

if(temp->hw.assign_id == id && preTemp != NULL){

preTemp -> next = temp->next;

free(temp);

}

preTemp = temp;

}

}

// Now Modifying your main function

int main(int argc, char const *argv[])
{
   int flag = 1;

   int ch;
   int id;
   int m, d, y;
   int c = 0;
   while (flag)
   {
       cout << " 1)Add assignment 2)Delete assignment 3)Display List of assignments 4)Exit ";
       cin >> ch;
       switch (ch)
       {
       case 1:cout << " Enter assignment id ";
           cin >> id;
           cout << "Enter due day ";
           cin >> d;
           cout << "Enter due month ";
           cin >> m;
           cout << "Enter due year ";
           cin >> y;
Homework homework = new Homework(id, d, m, y);

node * newNode = createNode(homework);

addAssignment(newNode);

           break;
       case 2:
           cout << "Enter ID to Delete assignment ";
           cin >> id;

deleteAssignment(id);

break;

       case 3:
dispalyAssignment();
           break;
       case 4:flag = 0; break;
       }
   }

   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote