Question

In: Computer Science

C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...

C++ Create a program that use the linkedbag

3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest due date. 4. We can represent a polynomial as an ordered list of terms, where the terms are ordered by their exponents. To add two polynomials, you traverse both lists and examine the two terms at the current iterator position. If the exponent of one is smaller than the exponent of the other, then insert this one into the result and advance that list’s iterator. If the exponents are equal, then create a new term with that exponent and the sum of the coefficients, and advance both iterators. For example: 3x4 + 2x2 + 3x + 7 added to 2x3 + 4x + 5 is 3x4 + 2x3 + 2x2 + 7x + 12 Write a program to read and add polynomials. You should define a class Term that contains the exponent and coefficient. This class should implement operator< by comparing the values of the exponents.

Solutions

Expert Solution

I have added comments in the program for better understanding

ASSIGNMENTS PROGRAM

#include <bits/stdc++.h>

using namespace std;

//class representing the Node of the linked list

class Node{
   public:
       string name; //denotes the name of the assignment
       string dueDate; //denotes the due date of the assignment
       Node* next; //points to the next assignment in the list
};

// function to add a new assignment to the list

void addAssignment(Node** head,string n,string d){
   Node* newNode = new Node(); //creating a new node to store the information of the new assignment
   Node* curr = *head;
   newNode->name = n;
   newNode->dueDate = d;
   newNode->next = NULL;
  
   //in case the list does not contain any assignments
  
   if(*head==NULL){
       *head = newNode;
       return;
   }
  
   // If the list is non empty, we go the end of the list and add the assignment there
  
   while(curr->next!=NULL){
       curr = curr->next;
   }
  
   curr->next = newNode;
   return;
}

// function to remove given assignment

void removeAssignment(Node** head,string n){
   Node* temp = *head;
   Node* prev; //pointer to the previous element is neccesary for linking the previous node to the next node after deletion

   // if the head contains the assignment to be deleted
  
if (temp != NULL && temp->name == n){
*head = temp->next;   
free(temp);   
return;
}
  
   // searching the entire list for the required assignment
  
while (temp != NULL && temp->name != n){
prev = temp;
temp = temp->next;
}
  
   // if there was no such assignment we simply return from the function
  
if (temp == NULL){
   return;
}

prev->next = temp->next;
   free(temp); // to avoid memory leaks
}

// function to print the list of assignments

void printAssignment(Node* head){
   while(head!=NULL){
       cout<<"NAME = "<<head->name<<" DUE DATE = "<<head->dueDate<<endl;
       head = head->next;
   }
}

// function to get the assignment with the earliest due date

void getAssignment(Node* head){
   string minYear = "3333";
   string minMonth = "33";
   string minDate = "33";
   string n = "";
  
   while(head != NULL){
       string date = head->dueDate.substr(0,2);
       string month = head->dueDate.substr(3,2);
       string year = head->dueDate.substr(6,4);
      
       if(year > minYear){
           head = head->next;
           continue;
       }
       else{
           if(month > minMonth){
               head = head->next;
               continue;
           }
           else{
               if(date > minDate){
                   head = head->next;
                   continue;
               }
               else{
                   minDate = date;
                   minMonth = month;
                   minYear = year;
                   n = head->name;
                   head = head->next;
               }
           }
       }
   }
   cout<<"Assignment with the earliest due date is "<<n<<endl;
}

int main() {
   Node* head = NULL;
   addAssignment(&head,"Math","01-01-2000");
   addAssignment(&head,"English","01-01-2001");
   addAssignment(&head,"History","01-01-2002");
   addAssignment(&head,"Physics","01-01-2003");
   printAssignment(head);
   cout<<endl;
   removeAssignment(&head,"English");
   cout<<"After removing English assignment the list is "<<endl;
   printAssignment(head);
   cout<<endl;
   getAssignment(head);
   return 0;
}

SAMPLE OUPUT FOR THE ABOVE PROGRAM

NAME = Math DUE DATE = 01-01-2000
NAME = English DUE DATE = 01-01-2001
NAME = History DUE DATE = 01-01-2002
NAME = Physics DUE DATE = 01-01-2003

After removing English assignment the list is 
NAME = Math DUE DATE = 01-01-2000
NAME = History DUE DATE = 01-01-2002
NAME = Physics DUE DATE = 01-01-2003

Assignment with the earliest due date is Math

POLYNOMIAL PROGRAM

#include <bits/stdc++.h>

using namespace std;

class Term{
   public:
       int exponent;
       int coefficient;
       Term* next;
};

// function to create a node inthe linked list

void createNode(Term** head,int x,int y){
   Term* newNode = new Term();
   Term *curr = *head;
   newNode->coefficient = x;
   newNode->exponent = y;
   newNode->next = NULL;
  
   if(*head == NULL){
       *head = newNode;
       return;
   }
  
   while(curr->next != NULL){
       curr = curr->next;
   }
  
   curr->next = newNode;
   return;
}

// function to add the polynomials

Term* addPolynomials(Term* p1, Term* p2){
   Term * p = NULL;
   while(p1 != NULL && p2 != NULL){

// if the exponent of first polynomial is greater than that of second one then copy it as it is in the answer polynomial
if(p1->exponent > p2->exponent){
   createNode(&p,p1->coefficient,p1->exponent);
p1 = p1->next;
}

  // if the exponent of second polynomial is greater than that of first one then copy it as it is in the answer polynomial
else if(p1->exponent < p2->exponent){
   createNode(&p,p2->coefficient,p2->exponent);
p2 = p2->next;
}

// otherwise add the corresponding coefficients
else{
   createNode(&p,p1->coefficient+p2->coefficient,p1->exponent);
p1 = p1->next;
p2 = p2->next;
}
}
   while(p1 != NULL || p2 != NULL) {
   if(p1 != NULL){
       createNode(&p,p1->coefficient,p1->exponent);
   p1 = p1->next;
   }
   if(p2 != NULL){
       createNode(&p,p2->coefficient,p2->exponent);
   p2 = p2->next;
   }
   }
   return p;
}

// function to print polynomials

void printPoly(Term* head){
   while(head!=NULL){
       cout<<head->coefficient<<"x"<<head->exponent;
       head = head->next;
       if(head != NULL){
           cout<<" + ";
       }
   }
}

int main() {
   Term* p1 = NULL,*p2 = NULL,*p =NULL;
// creating the first polynomial
   createNode(&p1,7,2);
   createNode(&p1,5,1);
   createNode(&p1,3,0);

// creating the second polynomial
  
   createNode(&p2,8,2);
   createNode(&p2,9,1);
   createNode(&p2,3,0);
  
   printPoly(p1);
   cout<<endl;
   printPoly(p2);
   cout<<endl;
   p = addPolynomials(p1,p2);
   printPoly(p);
  
  
   return 0;
}

SAMPLE OUTPUT

7x2 + 5x1 + 3x0
8x2 + 9x1 + 3x0
15x2 + 14x1 + 6x0

Related Solutions

Develop a program to maintain a Linked List of homework assignments name and due date. When...
Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. Remove an assignment. Provide a list of the assignments in the order they were assigned. Find the assignment(s) with the earliest due date...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
How would I create a program in C++ where you build and maintain two binary search...
How would I create a program in C++ where you build and maintain two binary search trees of information? I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations: L -- for launching a satellite, which will save it's info to the first set or D -- for deorbit the satellite. The other operations I'm looking to implement are: F -- for find, where user...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT