Showing posts with label Module 3. Show all posts
Showing posts with label Module 3. Show all posts

Saturday 24 September 2016

Polynomial Multiplication Using Circular Header Linked List

Polynomial Multiplication using Circular Linked List with header node


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

struct node
{
             int coeff;
             int expon;
             struct node *link;
};
typedef struct node *NODE;

NODE getnode()
{
            NODE x;
            x =(NODE) malloc(sizeof(struct node));
            return x;
}
void display(NODE head);

NODE attach(int coeff, int expon, NODE head)
{
            NODE temp, cur;
            temp = getnode();
            temp->coeff = coeff;
            temp->expon = expon;
            cur = head->link;
            while(cur->link != head)
            {
                        cur = cur->link;
            }
            cur->link = temp;
            temp->link = head;
            return head;
}

NODE read_poly(NODE head)
{
            int i = 1, coeff, expon;
            printf("\nEnter the coefficient as -999 to end the polynomial ");
            while(1)
            {
                        printf("\nEnter the %d term:\n",i++);
                        printf("\n\tCoeff = ");
                        scanf("%d", &coeff);
                        if(coeff == -999)
                                    break;
                        printf("\n\tPow x = ");
                        scanf("%d", &expon);
                        head = attach(coeff, expon, head);
            }
              return head;
}


NODE poly_mult(NODE head1, NODE head2, NODE head3)
{
            NODE cur1, cur2;
            if(head1->link == head1 || head2->link == head2)
            {
                        printf("\nMultiplied polynomial is zero polynomial");
                        return;
            }
            cur1 = head1->link;
            while(cur1 != head1)
            {
                        cur2 = head2->link;
                        while(cur2!=head2)
                        {
                                    head3 =attach(cur1->coeff*cur2->coeff, cur1->expon+cur2->expon, head3);
                                   cur2=cur2->link;
                        }
                        cur1=cur1->link;
            }
            return head3;
}

void display(NODE head)
{
            NODE temp;
            if(head->link == head)
            {
                         printf("\nPolynomial does not exist");
                         return;
            }
            temp = head->link;
            while(temp != head)
            {
                         printf("%dx^%d", temp->coeff, temp->expon);
                         temp = temp->link;
                         if(temp != head)
                                    printf(" + ");
            }
}

void main()
{
             NODE head1, head2, head3;
            head1 = getnode();
            head2 = getnode();
            head3 = getnode();

            head1->link=head1;
            head2->link=head2;
            head3->link=head3;

              printf("\nEnter the first polynomial \n");
              head1=read_poly(head1);
              printf("\nEnter the second polynomial \n");
              head2=read_poly(head2);
            head3=poly_mult(head1, head2, head3);
                       
              printf("\nPolynomial 1:\t");
              display(head1);
              printf("\nPolynomial 2:\t");
              display(head2);
              printf("\nPolynomial Result:\t");
              display(head3);
}

Output:
Enter the first polynomial
Enter the coefficient as -999 to end the polynomial
Enter the 1 term:
        Coeff = 4
        Pow x = 2
Enter the 2 term:
        Coeff = 3
        Pow x = 1
Enter the 3 term:
        Coeff = -999

Enter the second polynomial
Enter the coefficient as -999 to end the polynomial
Enter the 1 term:
        Coeff = 7
        Pow x = 1
Enter the 2 term:
        Coeff = 9
        Pow x = 0
Enter the 3 term:
       Coeff = -999

Polynomial 1:   4x^2 + 3x^1
Polynomial 2:   7x^1 + 9x^0

Polynomial Result:      28x^3 + 36x^2 + 21x^2 + 27x^1

Polynomial Addition using Circular Header Linked List

Polynomial Addition using Circular Header Linked List


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

struct node
{
             int coeff;
             int expon;
             struct node *link;
};
typedef struct node *NODE;

NODE getnode()
{
            NODE x;
            x = (NODE) malloc(sizeof(struct node));
            return x;
}

NODE attach(int coeff, int expon, NODE head)
{
            NODE temp, cur;
            temp = getnode();
            temp->coeff = coeff;
            temp->expon = expon;
            cur = head->link;
            while(cur->link != head)
            {
                        cur = cur->link;
            }
            cur->link = temp;
            temp->link = head;
            return head;
}

NODE  read_poly(NODE head)
{
            int i = 1, coeff, expon;
            printf("\nEnter the coefficient as -999 to end the polynomial ");
            while(1)
            {
                        printf("\nEnter the %d term:\n",i++);
                        printf("\n\tCoeff = ");
                        scanf("%d", &coeff);
                        if(coeff == -999)
                                    break;
                        printf("\n\tPow x = ");
                        scanf("%d", &expon);
                        head = attach(coeff, expon, head);
            }
              return head;
}

NODE poly_add(NODE head1, NODE head2, NODE head3)
{
            NODE a,b;
            int coeff;
            a = head1->link;
            b = head2->link;
            while(a != head1 && b != head2)
            {
                        if(a->expon == b->expon)
                        {
                                    coeff = a->coeff + b->coeff;
                                    if(coeff != 0)
                                                head3 = attach(coeff, a->expon, head3);
                                    a = a->link;
                                    b = b->link;
                        }
                        else if(a->expon > b->expon)
                        {
                                     head3 = attach(a->coeff, a->expon, head3);
                                    a = a->link;
                        }
                        else
                        {
                                    head3 = attach(b->coeff, b->expon, head3);
                                    b = b->link;
                        }
            }
            while(a != head1)
            {
                        head3 = attach(a->coeff, a->expon, head3);
                        a = a->link;
            }
            while(b != head2)
            {
                        head3 = attach(b->coeff, b->expon, head3);
                        b = b->link;
            }
            return head3;
}

void display(NODE head)
{
            NODE temp;
            if(head->link == head)
            {
                         printf("\nPolynomial does not exist");
                         return;
            }
            temp = head->link;
            while(temp != head)
            {
                         printf("%dx^%d",temp->coeff, temp->expon);
                         temp = temp->link;
                         if(temp != head)
                                    printf(" + ");
            }
}

void main()
{
            NODE head1, head2, head3;
            head1 = getnode();
            head2 = getnode();
            head3 = getnode();
            head1->link=head1;
            head2->link=head2;
            head3->link=head3;

            printf("\nEnter the first polynomial \n");
            head1 = read_poly(head1);
            printf("\nEnter the second polynomial \n");
            head2 = read_poly(head2);

            head3 = poly_add(head1, head2, head3);

            printf("\nPolynomial 1:\t");
            display(head1);
            printf("\nPolynomial 2:\t");
            display(head2);
            printf("\nPolynomial Result:\t");
            display(head3);
}

Output:

Enter the first polynomial
Enter the coefficient as -999 to end the polynomial
Enter the 1 term:
        Coeff = 1
        Pow x = 3
Enter the 2 term:
        Coeff = 1
        Pow x = 2
Enter the 3 term:
        Coeff = 1
        Pow x = 1
Enter the 4 term:
        Coeff = 1
        Pow x = 0
Enter the 5 term:
        Coeff = -999

Enter the second polynomial
Enter the coefficient as -999 to end the polynomial
Enter the 1 term:
        Coeff = 7
        Pow x = 2
Enter the 2 term:
        Coeff = 5
        Pow x = 1
Enter the 4 term:
        Coeff = -999

Polynomial 1:   1x^3 + 1x^2 + 1x^1 + 1x^0
Polynomial 2:   7x^2 + 5x^1
Polynomial Result:      1x^3 + 8x^2 + 6x^1 + 1x^0

Circular Doubly Linked List: Delete at End

Circular Doubly Linked List: Delete at End


#include<stdio.h>
#include<stdlib.h>
struct node
{
                int data;
                struct node *llink;
                struct node *rlink;
};
typedef struct node* NODE;

NODE getnode();
NODE insert_at_end(NODE last, int item);
NODE delete_at_end(NODE last);
void display(NODE last);

NODE getnode()
{
                NODE x;
                x =(NODE) malloc(sizeof(struct node));
                return x;
}
NODE insert_at_end(NODE last, int item)
{
                int val;
                NODE cur, temp;
                temp = getnode();
                temp->data = item;
                if(last == NULL)
                {
                                last = temp;
                }
                else
                {
                                temp->rlink = last->rlink;
                                last->rlink->llink = temp;
                }
                last->rlink = temp;
                temp->llink = last;
                return temp;
}
NODE delete_at_end(NODE last)
{
                NODE prev, cur;
                if(last == NULL)
                {
                                printf("\nList is empty");
                                return NULL;
                }
                if(last->rlink == last)
                {
                                printf("\nThe node %d is deleted",last->data);
                                free(last);
                                return NULL;
                }
                prev = last->rlink;
                while(prev->rlink!=last)
                {
                                prev = prev->rlink;
                }
                prev->rlink=last->rlink;
                last->rlink->llink = prev;
                printf("The node %d is deleted \n",last->data);
                free(last);
                return prev;
}
void display(NODE last)
{
                NODE cur;
                printf("\nContents are:\n");
                if(last == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                 cur = last->rlink;
                                while(cur != last)
                                {
                                                printf("|%d | %d | %d | ==>  ", cur->llink, cur->data, cur->rlink);
                                                cur = cur->rlink;
                                }
                                printf("| %d | %d | %d |  ", cur->llink, cur->data, cur->rlink);
                }
}
void main()
{
                int ch, item;
                NODE last = NULL;
                while(1)
                {
                                printf("\n\n~~MENU~~");
                                printf("\n1.Insert at end");
                                printf("\n2.Delete from end");
                                printf("\n3.Display");
                                printf("\n4.exit");
                                printf("\nEnter your choice: ");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                              case 1:  printf("\nEnter the value: ");
                                                           scanf("%d", &item);
                                                           last = insert_at_end(last, item);
                                                           break;
                                                case 2:   last = delete_at_end(last);
                                                               break;
                                                case 3:   display(last);
                                                              break;
                                                case 4:   exit(0);
                                }
                }
}




Output:
~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 11

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 13

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 3
Contents are:
|5321840 | 11 | 5321816 | ==>  |5321792 | 12 | 5321840 | ==>  | 5321816 | 13 | 5321792 |

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 2
The node 13 is deleted

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 4