#include #include /* * Struct we will be using for a polynomial term (our doubly linked list object) */ typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial...

sdfsf


#include #include /* * Struct we will be using for a polynomial term (our doubly linked list object) */ typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial term associated with the object int coef; // power of the polynomial term associated with the object int power; // pointer to the next object (to facilitate a doubly linked list) struct Term *next; // pointer to the previous object (to facilitate a doubly linked list) struct Term *prev; // pointer to the Doubly Linked List this object is in (so we don't lose it) struct Polynomial *parent; } t_Term; /* * Struct we will be using for the polynomial (our doubly linked list, list) */ typedef struct Polynomial { // number of items in the list int numTerms; // pointer to the first term in the list struct Term *head; // pointer to the last term in the list struct Term *tail; } t_Polynomial; /* * Function to print the polynomial and help you debug */ int Print_Polynomial(t_Polynomial *poly) { /* (1) check that the input is valid */ if(poly == NULL) { return -1; } /* (2) print the polynomial */ t_Term *currentTerm = poly->head; while(currentTerm != NULL) { if(currentTerm == poly->head) { printf("(%d*(x^%d))", currentTerm->coef, currentTerm->power); } else { printf(" + (%d*(x^%d))", currentTerm->coef, currentTerm->power); } currentTerm = currentTerm->next; } /* (3) return 1 to indicate success if we reach this point */ return 1; } /* * Problem 1: * * Allocate space for a new polynomial term, set the coefficient and power * values, set the next, prev, and parent pointers to be NULL, and then return a * pointer to the allocated and initialized memory. */ t_Term * Create_Poly_Term(int coefficient, int power) { return NULL; } /* * Problem 2: * * Allocate space for a polynomial, initialize the number of terms to be zero, * set the head and tail pointers to be NULL, and then return a pointer to the * allocated and initialized memory. */ t_Polynomial * Create_Poly() { } /* * Problem 3: * * Adds the given term to the given polynomial. The term must be added using * the following rules. If the polynomial has no terms, the head and tail * should point to the term. If the polynomial has multiple terms the new term * should be added such that the powers of the terms remain in descending order. * For example. Suppose our polynomial could be expressed as * (2*(x^2)) + (3*(x^0)). The first term in the polynomial (i.e. what the head * points to) would be the term with coefficient 2 and power 2. The next (and * last) term would be the term with coefficient 3 and power 0. Suppose we wish * to add the term with coefficient 4 and power 1 to the polynomial. Then the * term should be added between the first and last terms of the polynomial so * that when we print the polynomial out (using the given function above) we get * (2*(x^2)) + (4*(x^1)) + (3*(x^0)). Suppose instead we wish to add the term * with coefficient 1 and power 0. Then the resulting polynomial should be * (2*(x^2)) + (4*(x^0)). Note, terms that add to zero should be removed. * * To reiterate, the powers of the terms MUST remain in proper, descending order * for THIS question (for other questions we will change this). You can assume * that the polynomial is properly ordered when this function is called. * Finally, don't forget to update the number of terms in the polynomial and the * terms parent (or anything else that might change)! * * Make sure to return -1 if the input is invalid or 1 if successful. */ int Add_Term_To_Polynomial(t_Polynomial *poly, t_Term *term) { return -1; } /* * Problem 4: * * Swaps the two terms in the polynomial so that if term One is directly before * term two, it is after term two after this function is called. * * Hint: Make sure that the terms are in the same polynomial. Also, be careful * of which pointers you assign (think back to class about which pointers are * extra important)! Finally, the terms should be next to one another. If they * are not, or something else is wrong with the input, you should return -1 (1 * for success). */ int Swap_Terms(t_Term *termOne, t_Term *termTwo) { return -1; } /* * Problem 5: * * Instead of the polynomial being sorted by the powers of the terms in * descending order, sort the polynomial by the coefficients of the terms in * ascending order. For example, if the input is * (2*(x^2)) + (4*(x^1)) + (3*(x^0)) the polynomial should be ordered as * (2*(x^2)) + (3*(x^0)) + (4*(x^1)) on return. * * Use bubble sort and the Swap_Terms function you wrote above (when turning in * each function though, each should have their own files as per the * instructions). * * Make sure to return -1 if the input is invalid and 1 if successful. */ int Sort_Polynomial_By_Coefficients(t_Polynomial *poly) { return -1; } /* * Problem 6: * * Frees all of the memory allocated by/for the polynomial. * * Hint: This problem may not be as straight forward as it appears on the * surface. Think about what memory is on the stack and what is in the heap. * If you aren't careful things could blow up on you. */ void Free_Polynomial_Memory(t_Polynomial *poly) { } /* * Extra Credit: * * Add the two given polynomials together and return the result. However, you * cannot use nested loops. In particular, you can use no more than THREE loops * (informally, running time should be O(3N)). Moreover, terms that sum to 0 * (i.e. (3*(x^0)) + (-3*(x^0))) should be removed and memory freed (you will * need to make sure the input is in the heap to get this to work). * * Note, this is actually a version of a real (albeit significantly simplified) * problem I have encountered in the wild. What makes this problem difficult * is the need for speed. The simplest approach would be to use nested loops * to iterate over all terms (O(N^2) running time). However, we want to as fast * fast as possible which precludes the use of nested loops. * * With this in mind, note that the function Add_Term_To_Polynomial will have a * loop in it. Therefore, if you use the loop in this function and call * Add_Term_To_Polynomial in a loop you will essentially have nested loops and * violate the required running time. To get this function to work you will * have to duplicate some of the code you wrote for the Add_Term_To_Polynomial * but do it in such a way as to have no nested loops. * * If the input is invalid return NULL otherwise return a pointer to the new * polynomial. * * Hint: break the problem up into three parts. */ t_Polynomial * Add_Polynomials(t_Polynomial *polyOne, t_Polynomial *polyTwo) { return NULL; } /* * main method for testing. */ int main(int argc, char **argv, char **envp) { /* (1) create polynomial to test the print function */ // we do this manually since none of the functions have been completed // also, note that these variables are on the stack. For some questions you // will need to make sure the variables are on the heap to prevent problems t_Polynomial poly; t_Term one; t_Term two; t_Term three; one.coef = 3; one.power = 3; one.parent = &poly; one.prev = NULL; one.next = &two; two.coef = -2; two.power = 2; two.parent = &poly; two.prev = &one; two.next = &three; three.coef = 1; three.power = 1; three.parent = &poly; three.prev = &two; three.next = NULL; poly.head = &one; poly.tail = &three; poly.numTerms = 3; printf("\nSuccess: %d\n", Print_Polynomial(&poly)); }
Oct 01, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here