In: Computer Science
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.
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