DSA

It is a way of storing and organizing the data efficiently.
It is classified in two categories

  1. Primitive Data Structure
  2. Non-Primitive Data Structure

1. Primitive DS :

Integer, Character, Float : i.e. pre-defined things

  • You can store data in a system defined variable.

2. Non-Primitive DS :

it is of two types

  1. Linear : Has two types, Static and Dynamic
  2. Non-Linear

1. Linear :

Static : Array
Dynamic : Linked List, Stack, Queue

2. Non-Linear :

Trees and graphs


Array :

Linked List : Refer to notebook as well

Polynomial Addition :

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int coef;
    int exp;
    struct Node *next;
};

typedef struct Node Node;

void insert(Node *&poly, int coef, int exp)
{
    Node *t = (Node *)malloc(sizeof(Node));
    t->coef = coef;
    t->exp = exp;
    t->next = NULL;

    if (poly == NULL)
        poly = t;
    else
    {
        Node *current = poly;
        while (current->next != NULL)
            current = current->next;
        current->next = t;
    }
}

void print(Node *poly)
{
    if (poly == NULL)
        printf("0\n");
    else
    {
        while (poly)
        {
            printf("%dx^%d", poly->coef, poly->exp);
            if (poly->next != NULL)
            {
                printf(" + ");
            }
            poly = poly->next;
        }
    }
    printf("\n");
}

Node *add(Node *poly1, Node *poly2)
{
    Node *result = NULL;

    while (poly1 != NULL && poly2 != NULL)
    {
        if (poly1->exp == poly2->exp)
        {
            insert(result, poly1->coef + poly2->coef, poly1->exp);
            poly1 = poly1->next;
            poly2 = poly2->next;
        }
        else if (poly1->exp > poly2->exp)
        {
            insert(result, poly1->coef, poly1->exp);
            poly1 = poly1->next;
        }
        else
        {
            insert(result, poly2->coef, poly2->exp);
            poly2 = poly2->next;
        }
    }

    while (poly1 != NULL)
    {
        insert(result, poly1->coef, poly1->exp);
        poly1 = poly1->next;
    }

    while (poly2 != NULL)
    {
        insert(result, poly2->coef, poly2->exp);
        poly2 = poly2->next;
    }

    return result;
}

int main()
{
    Node *poly1 = NULL;

    insert(poly1, 5, 4);
    insert(poly1, 3, 2);
    insert(poly1, 1, 0);

    Node *poly2 = NULL;
    insert(poly2, 4, 4);
    insert(poly2, 2, 2);
    insert(poly2, 1, 1);

    printf("First polynomial: ");
    print(poly1);

    printf("Second polynomial: ");
    print(poly2);

    Node *result = add(poly1, poly2);
    printf("Result: ");
    print(result);

    return 0;
}

Multiplication Completed (Self) : Sort karna padega exp ke hisaab se tabhi chalega ye

// program to multiply two polynomials using linked list
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int coef;
    int exp;
    struct Node *next;
};

typedef struct Node Node;

void insert(Node **poly, int coef, int exp)
{
    Node *t = (Node *)malloc(sizeof(Node));
    t->coef = coef;
    t->exp = exp;
    t->next = NULL;

    if (*poly == NULL)
        *poly = t;
    else
    {
        Node *current = *poly;
        while (current->next != NULL)
            current = current->next;
        current->next = t;
    }
}

void print(Node *poly)
{
    if (poly == NULL)
        printf("0\n");
    else
    {
        while (poly)
        {
            printf("%dx^%d", poly->coef, poly->exp);
            if (poly->next != NULL)
            {
                printf(" + ");
            }
            poly = poly->next;
        }
    }
    printf("\n");
}

Node* polyAddMult(Node *poly3)
{
    Node *poly = NULL;
    while(poly3)
    {
        int pow = poly3->exp;
        int coef =0;
        while(poly3 && poly3->exp == pow)
        {
            coef+=poly3->coef;
            poly3=poly3->next;
        }
        insert(&poly, coef, pow);
    }
    // print(poly);
    return poly;
}

Node* multiplyPoly(Node *poly1, Node *poly2)
{
    Node *poly = NULL;
    Node *current1 = poly1;
    while (current1)
    {
        Node *current2 = poly2;
        while (current2)
        {
            int coef = current1->coef * current2->coef;
            int exp = current1->exp + current2->exp;
            insert(&poly, coef, exp);
            current2 = current2->next;
        }
        current1 = current1->next;
    }
    // print(poly);
    return polyAddMult(poly);
}

void pushValues(Node **poly1, Node **poly2)
{

    printf("Enter the number of elements in polynomial 1: ");
    int n1;
    scanf("%d", &n1);
    for (int i = 0; i < n1; i++)
    {
        int coef, exp;
        printf("Enter the coefficient and exponent: ");
        scanf("%d %d", &coef, &exp);
        insert(poly1, coef, exp);
    }

    printf("Enter the number of elements in polynomial 2: ");
    int n2;
    scanf("%d", &n2);
    for (int i = 0; i < n2; i++)
    {
        int coef, exp;
        printf("Enter the coefficient and exponent: ");
        scanf("%d %d", &coef, &exp);
        insert(poly2, coef, exp);
    }
}


int main()
{
    Node *poly1 = NULL;
    Node *poly2 = NULL;

    // insert(&poly1, 1, 2);
    // insert(&poly1, 2, 1);
    // insert(&poly1, 3, 0);

    // insert(&poly2, 4, 1);
    // insert(&poly2, 5, 0);

    pushValues(&poly1, &poly2);

    printf("First polynomial: ");
    print(poly1);
    printf("Second polynomial: ");
    print(poly2);

    printf("Product of the polynomials: ");
    Node * poly3 = multiplyPoly(poly1, poly2);
    print(poly3);

    return 0;
}

Half Assed Multiplication :

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int coef;
    int exp;
    struct Node *next;
};

typedef struct Node Node;

void insert(Node *&poly, int coef, int exp)
{
    Node *t = (Node *)malloc(sizeof(Node));
    t->coef = coef;
    t->exp = exp;
    t->next = NULL;

    if (poly == NULL)
        poly = t;
    else
    {
        Node *current = poly;
        while (current->next != NULL)
            current = current->next;
        current->next = t;
    }
}

void print(Node *poly)
{
    if (poly == NULL)
        printf("0\n");
    else
    {
        while (poly)
        {
            printf("%dx^%d", poly->coef, poly->exp);
            if (poly->next != NULL)
            {
                printf(" + ");
            }
            poly = poly->next;
        }
    }
    printf("\n");
}

Node *add(Node *poly1, Node *poly2)
{
    Node *poly = NULL;
    while (poly1 && poly2)
    {
        if (poly1->exp > poly2->exp)
        {
            insert(poly, poly1->coef, poly1->exp);
            poly1 = poly1->next;
        }
        else if (poly1->exp < poly2->exp)
        {
            insert(poly, poly2->coef, poly2->exp);
            poly2 = poly2->next;
        }
        else
        {
            insert(poly, poly1->coef + poly2->coef, poly1->exp);
            poly1 = poly1->next;
            poly2 = poly2->next;
        }
    }

    while (poly1)
    {
        insert(poly, poly1->coef, poly1->exp);
        poly1 = poly1->next;
    }

    while (poly2)
    {
        insert(poly, poly2->coef, poly2->exp);
        poly2 = poly2->next;
    }

    return poly;
}

Node *multiply(Node *poly1, Node *poly2)
{
    Node *poly = NULL;
    Node *p1 = poly1;
    Node *p2 = poly2;

    while (p1)
    {
        while (p2)
        {
            insert(poly, p1->coef * p2->coef, p1->exp + p2->exp);
            p2 = p2->next;
        }
        p1 = p1->next;
        p2 = poly2;
    }

    return poly;
}

int main()
{
    
    Node *poly1 = NULL;

    insert(poly1, 5, 4);
    insert(poly1, 3, 2);
    insert(poly1, 1, 0);

    Node *poly2 = NULL;
    insert(poly2, 4, 4);
    insert(poly2, 2, 2);
    insert(poly2, 1, 1);

    printf("First polynomial: ");
    print(poly1);

    printf("Second polynomial: ");
    print(poly2);

    Node *result = add(poly1, poly2);
    printf("Result: ");
    print(result);

    Node *result2 = multiply(poly1, poly2);
    printf("Result: ");
    print(result2);

    return 0;
}

Pasted image 20240128132518.png

Gfg

// C++ implementation of the above approach 
#include <bits/stdc++.h> 
using namespace std; 

// Node structure containing powerer 
// and coefficient of variable 
struct Node { 
	int coeff, power; 
	Node* next; 
}; 

// Function add a new node at the end of list 
Node* addnode(Node* start, int coeff, int power) 
{ 
	// Create a new node 
	Node* newnode = new Node; 
	newnode->coeff = coeff; 
	newnode->power = power; 
	newnode->next = NULL; 

	// If linked list is empty 
	if (start == NULL) 
		return newnode; 

	// If linked list has nodes 
	Node* ptr = start; 
	while (ptr->next != NULL) 
		ptr = ptr->next; 
	ptr->next = newnode; 

	return start; 
} 

// Function To Display The Linked list 
void printList(struct Node* ptr) 
{ 
	while (ptr->next != NULL) { 
		cout << ptr->coeff << "x^" << ptr->power ; 
	if( ptr->next!=NULL && ptr->next->coeff >=0) 
		cout << "+"; 

		ptr = ptr->next; 
	} 
	cout << ptr->coeff << "\n"; 
} 

// Function to add coefficients of 
// two elements having same powerer 
void removeDuplicates(Node* start) 
{ 
	Node *ptr1, *ptr2, *dup; 
	ptr1 = start; 

	/* Pick elements one by one */
	while (ptr1 != NULL && ptr1->next != NULL) { 
		ptr2 = ptr1; 

		// Compare the picked element 
		// with rest of the elements 
		while (ptr2->next != NULL) { 

			// If powerer of two elements are same 
			if (ptr1->power == ptr2->next->power) { 

				// Add their coefficients and put it in 1st element 
				ptr1->coeff = ptr1->coeff + ptr2->next->coeff; 
				dup = ptr2->next; 
				ptr2->next = ptr2->next->next; 

				// remove the 2nd element 
				delete (dup); 
			} 
			else
				ptr2 = ptr2->next; 
		} 
		ptr1 = ptr1->next; 
	} 
} 

// Function two Multiply two polynomial Numbers 
Node* multiply(Node* poly1, Node* poly2, 
			Node* poly3) 
{ 

	// Create two pointer and store the 
	// address of 1st and 2nd polynomials 
	Node *ptr1, *ptr2; 
	ptr1 = poly1; 
	ptr2 = poly2; 
	while (ptr1 != NULL) { 
		while (ptr2 != NULL) { 
			int coeff, power; 

			// Multiply the coefficient of both 
			// polynomials and store it in coeff 
			coeff = ptr1->coeff * ptr2->coeff; 

			// Add the powerer of both polynomials 
			// and store it in power 
			power = ptr1->power + ptr2->power; 

			// Invoke addnode function to create 
			// a newnode by passing three parameters 
			poly3 = addnode(poly3, coeff, power); 

			// move the pointer of 2nd polynomial 
			// two get its next term 
			ptr2 = ptr2->next; 
		} 

		// Move the 2nd pointer to the 
		// starting point of 2nd polynomial 
		ptr2 = poly2; 

		// move the pointer of 1st polynomial 
		ptr1 = ptr1->next; 
	} 

	// this function will be invoke to add 
	// the coefficient of the elements 
	// having same powerer from the resultant linked list 
	removeDuplicates(poly3); 
	return poly3; 
} 

// Driver Code 
int main() 
{ 

	Node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL; 

	// Creation of 1st Polynomial: 3x^2 + 5x^1 + 6 
	poly1 = addnode(poly1, 3, 3); 
	poly1 = addnode(poly1, 6, 1); 
	poly1 = addnode(poly1, -9, 0); 

	// Creation of 2nd polynomial: 6x^1 + 8 
	poly2 = addnode(poly2, 9, 3); 
	poly2 = addnode(poly2, -8, 2); 
	poly2 = addnode(poly2, 7, 1); 
	poly2 = addnode(poly2, 2, 0); 

	// Displaying 1st polynomial 
	cout << "1st Polynomial:- "; 
	printList(poly1); 

	// Displaying 2nd polynomial 
	cout << "2nd Polynomial:- "; 
	printList(poly2); 

	// calling multiply function 
	poly3 = multiply(poly1, poly2, poly3); 

	// Displaying Resultant Polynomial 
	cout << "Resultant Polynomial:- "; 
	printList(poly3); 

	return 0; 
} 

Union in Polynomial

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int coef;
    int exp;
    struct Node *next;
};

typedef struct Node Node;

void insert(Node *&poly, int coef, int exp)
{
    Node *t = (Node *)malloc(sizeof(Node));
    t->coef = coef;
    t->exp = exp;
    t->next = NULL;

    if (poly == NULL)
        poly = t;
    else
    {
        Node *current = poly;
        while (current->next != NULL)
            current = current->next;
        current->next = t;
    }
}

void print(Node *poly)
{
    if (poly == NULL)
        printf("0\n");
    else
    {
        while (poly)
        {
            printf("%dx^%d", poly->coef, poly->exp);
            if (poly->next != NULL)
            {
                printf(" + ");
            }
            poly = poly->next;
        }
    }
    printf("\n");
}

Node *unionPoly(Node *poly1, Node *poly2)
{
	Node *poly = NULL;
	while (poly1 && poly2)
	{
		if (poly1->exp > poly2->exp)
		{
			insert(poly, poly1->coef, poly1->exp);
			poly1 = poly1->next;
		}
		else if (poly1->exp < poly2->exp)
		{
			insert(poly, poly2->coef, poly2->exp);
			poly2 = poly2->next;
		}
		else if (poly1->coef == poly2->coef && poly1->exp == poly2->exp)
		{
			insert(poly, poly1->coef, poly1->exp);
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
		else
		{
			insert(poly, poly1->coef + poly2->coef, poly1->exp);
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
	}

	while (poly1)
	{
		insert(poly, poly1->coef, poly1->exp);
		poly1 = poly1->next;
	}

	while (poly2)
	{
		insert(poly, poly2->coef, poly2->exp);
		poly2 = poly2->next;
	}

	return poly;
}

Node *add(Node *poly1, Node *poly2)
{
    Node *result = NULL;

    while (poly1 != NULL && poly2 != NULL)
    {
        if (poly1->exp == poly2->exp)
        {
            insert(result, poly1->coef + poly2->coef, poly1->exp);
            poly1 = poly1->next;
            poly2 = poly2->next;
        }
        else if (poly1->exp > poly2->exp)
        {
            insert(result, poly1->coef, poly1->exp);
            poly1 = poly1->next;
        }
        else
        {
            insert(result, poly2->coef, poly2->exp);
            poly2 = poly2->next;
        }
    }

    while (poly1 != NULL)
    {
        insert(result, poly1->coef, poly1->exp);
        poly1 = poly1->next;
    }

    while (poly2 != NULL)
    {
        insert(result, poly2->coef, poly2->exp);
        poly2 = poly2->next;
    }

    return result;
}

int main()
{
    Node *poly1 = NULL;

    insert(poly1, 5, 4);
    insert(poly1, 3, 2);
    insert(poly1, 1, 0);

    Node *poly2 = NULL;
    insert(poly2, 4, 4);
    insert(poly2, 3, 2);
    insert(poly2, 1, 1);

    printf("First polynomial: ");
    print(poly1);

    printf("Second polynomial: ");
    print(poly2);

    // Node *result = add(poly1, poly2);
    Node *result = unionPoly(poly1, poly2);
    printf("Result: ");
    print(result);

    return 0;
}

Intersection of Polynomials :

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int coef;
    int exp;
    struct Node *next;
};

typedef struct Node Node;

void insert(Node *&poly, int coef, int exp)
{
    Node *t = (Node *)malloc(sizeof(Node));
    t->coef = coef;
    t->exp = exp;
    t->next = NULL;

    if (poly == NULL)
        poly = t;
    else
    {
        Node *current = poly;
        while (current->next != NULL)
            current = current->next;
        current->next = t;
    }
}

void print(Node *poly)
{
    if (poly == NULL)
        printf("0\n");
    else
    {
        while (poly)
        {
            printf("%dx^%d", poly->coef, poly->exp);
            if (poly->next != NULL)
            {
                printf(" + ");
            }
            poly = poly->next;
        }
    }
    printf("\n");
}

Node *unionPoly(Node *poly1, Node *poly2)
{
	Node *poly = NULL;
	while (poly1 && poly2)
	{
		if (poly1->exp > poly2->exp)
		{
			insert(poly, poly1->coef, poly1->exp);
			poly1 = poly1->next;
		}
		else if (poly1->exp < poly2->exp)
		{
			insert(poly, poly2->coef, poly2->exp);
			poly2 = poly2->next;
		}
		else if (poly1->coef == poly2->coef && poly1->exp == poly2->exp)
		{
			insert(poly, poly1->coef, poly1->exp);
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
		else
		{
			insert(poly, poly1->coef + poly2->coef, poly1->exp);
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
	}

	while (poly1)
	{
		insert(poly, poly1->coef, poly1->exp);
		poly1 = poly1->next;
	}

	while (poly2)
	{
		insert(poly, poly2->coef, poly2->exp);
		poly2 = poly2->next;
	}

	return poly;
}

Node *intersectionPoly(Node *poly1, Node*poly2)
{
	Node *poly = NULL;
	while (poly1 && poly2)
	{
		if (poly1->exp > poly2->exp)
		{
			poly1 = poly1->next;
		}
		else if (poly1->exp < poly2->exp)
		{
			poly2 = poly2->next;
		}
		else if (poly1->coef == poly2->coef && poly1->exp == poly2->exp)
		{
			insert(poly, poly1->coef, poly1->exp);
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
		else
		{
			poly1 = poly1->next;
			poly2 = poly2->next;
		}
	}

	return poly;
}

Node *add(Node *poly1, Node *poly2)
{
    Node *result = NULL;

    while (poly1 != NULL && poly2 != NULL)
    {
        if (poly1->exp == poly2->exp)
        {
            insert(result, poly1->coef + poly2->coef, poly1->exp);
            poly1 = poly1->next;
            poly2 = poly2->next;
        }
        else if (poly1->exp > poly2->exp)
        {
            insert(result, poly1->coef, poly1->exp);
            poly1 = poly1->next;
        }
        else
        {
            insert(result, poly2->coef, poly2->exp);
            poly2 = poly2->next;
        }
    }

    while (poly1 != NULL)
    {
        insert(result, poly1->coef, poly1->exp);
        poly1 = poly1->next;
    }

    while (poly2 != NULL)
    {
        insert(result, poly2->coef, poly2->exp);
        poly2 = poly2->next;
    }

    return result;
}

int main()
{
    Node *poly1 = NULL;

    insert(poly1, 5, 4);
    insert(poly1, 3, 2);
    insert(poly1, 1, 0);

    Node *poly2 = NULL;
    insert(poly2, 4, 4);
    insert(poly2, 3, 2);
    insert(poly2, 1, 1);

    printf("First polynomial: ");
    print(poly1);

    printf("Second polynomial: ");
    print(poly2);

    // Node *result = add(poly1, poly2);
    // Node *result = unionPoly(poly1, poly2);
    Node *result = intersectionPoly(poly1, poly2);
    printf("Result: ");
    print(result);

    return 0;
}

Infix to Postfix :

Pasted image 20240207235541.pngPasted image 20240207235558.png