Skip to content

Adding 'n' polynomials with the data structure ,singly linked list.

License

Notifications You must be signed in to change notification settings

sanjanatg/polynomial-addition-using-linked-list

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Header and Definitions

  • Include Libraries:

    • #include <stdio.h>: Standard input and output library.
    • #include <stdlib.h>: Standard library for memory allocation and process control.
  • Macro for Dynamic Memory Allocation:

    • #define MALLOC(p, n, type): Macro to simplify memory allocation with error checking.

Struct Definitions

  • Node Structure:

    • typedef struct Node: Defines a node in the linked list.
    • int coeff;: Coefficient of the polynomial term.
    • int expo;: Exponent of the polynomial term.
    • struct Node* next;: Pointer to the next node in the list.
  • Polynomial Structure:

    • typedef struct: Defines a polynomial.
    • Node* head;: Pointer to the head node of the polynomial's linked list.

Insert Term Function

  • Function Declaration:

    • void insertTerm(Polynomial* poly, int coeff, int expo): Function to insert a term into the polynomial.
  • Memory Allocation:

    • MALLOC(newNode, 1, Node);: Allocate memory for a new node with error checking.
  • Node Initialization:

    • Initialize newNode with the given coefficient and exponent, and set next to NULL.
  • Insert as First Element:

    • If the list is empty (poly->head == NULL), set head to newNode.
  • Insert in Order:

    • Traverse the list to find the correct position for the new node, maintaining descending order by exponents.
  • Combine Like Terms:

    • If a node with the same exponent is found, add the coefficients together to avoid multiple nodes for the same exponent value and keep the polynomial clean.
  • Insert Node:

    • If no node with the same exponent is found, insert newNode at the correct position in the list. Link the previous node to newNode if it exists, or set head to newNode if it's the first element.

Add Polynomials Function

  • Function Declaration:

    • Polynomial addPolynomials(Polynomial* polys, int n): Function to add multiple polynomials.
  • Initialize Result Polynomial:

    • Polynomial result; result.head = NULL;: Create an empty result polynomial.
  • Loop Through Polynomials:

    • For each polynomial in the input array, traverse its linked list and insert each term into the result polynomial using insertTerm.

Display Polynomial Function

  • Function Declaration:

    • void displayPolynomial(Polynomial* poly): Function to display the polynomial.
  • Traverse and Print:

    • Traverse the linked list and print each term in the polynomial. If the term is not the head and the coefficient is positive, print a + sign.

Main Function

  • Input Number of Polynomials:

    • printf("Enter the number of polynomials: "); scanf("%d", &num);: Get the number of polynomials from the user.
  • Initialize Polynomials:

    • Create an array of Polynomial structs and initialize each polynomial's head to NULL.
  • Input Polynomial Terms:

    • For each polynomial, get the number of terms, and for each term, get the coefficient and exponent, then insert the term into the polynomial using insertTerm.
  • Add Polynomials:

    • Polynomial result = addPolynomials(polys, num);: Add all the polynomials together using the addPolynomials function.
  • Display Result:

    • displayPolynomial(&result);: Display the resulting polynomial.

End of Program

  • Return 0:
    • Indicate successful program termination.

This enhanced script now fully encapsulates the process and includes the reasoning behind each function and their interactions.