0% found this document useful (0 votes)
38 views10 pages

Exp:1 Implementation of Singly 20/12/'18 Linked List Operations

The document describes code to implement polynomial addition using circular linked lists. It defines nodes with coefficient and exponent fields, and circular list heads for three polynomials. Functions are defined to create the list structures, insert terms into individual polynomials, attach terms during addition, and display results. The main function tests it by creating two polynomials, adding them and displaying the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views10 pages

Exp:1 Implementation of Singly 20/12/'18 Linked List Operations

The document describes code to implement polynomial addition using circular linked lists. It defines nodes with coefficient and exponent fields, and circular list heads for three polynomials. Functions are defined to create the list structures, insert terms into individual polynomials, attach terms during addition, and display results. The main function tests it by creating two polynomials, adding them and displaying the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXP :1 IMPLEMENTATION OF SINGLY

20/12/’18 LINKED LIST OPERATIONS


AIM:
To create a singly linked list and perform certain operations on it.

SOURCE CODE:
#include<stdio.h>

#include<stdlib.h>

struct node

int ele;

struct node *link;

};

struct node *head;

void create()

head=(struct node *)malloc(sizeof(struct node));

head->ele=0;

head->link=NULL;

void insert()

int i,ele,n;

struct node *x,*p;

printf("\n Enter the number of elements: ");


scanf("%d",&n);

p=head;

for(i=0;i<n;i++)

scanf("%d",&ele);

x=(struct node *)malloc(sizeof(struct node));

x->ele=ele;

x->link=NULL;

if(head->link==NULL)

head->link=x;

else

while(p->link!=NULL)

p=p->link;

p->link=x;

void deletes(int x)

struct node *p,*q;


p=(struct node *)malloc(sizeof(struct node));

q=(struct node *)malloc(sizeof(struct node));

p=head->link;

while((p->ele)!=x)

q=p;

p=p->link;

q->link=p->link;

free(p);

void display()

struct node *p;

p=head->link;

printf("\n The elements are: ");

while(p!=NULL)

printf("\n%d",p->ele);

p=p->link;

void main()

create();
insert();

deletes(5);

printf("After removal of element 5 :");

display();

SAMPLE INPUT AND OUTPUT:

RESULT:
Thus the above code has been verified successfully.
EXP :2 IMPLEMENTATION OF POLYNOMIAL ADDITON
20/12/’18 USING CIRCULAR LINKED LIST
AIM:
To implement polynomial addition using circular linked list.

SOURCE CODE:
#include<stdio.h>

#include<stdlib.h>

struct node

int coeff,exp;

struct node *link;

};

struct node *head1,*head2,*head3;

void create()

head1=(struct node *)malloc(sizeof(struct node));

head1->coeff=0;

head1->exp=-1;

head1->link=head1;

head2=(struct node *)malloc(sizeof(struct node));

head2->coeff=0;

head2->exp=-1;

head2->link=head2;

head3=(struct node *)malloc(sizeof(struct node));


head3->coeff=0;

head3->exp=-1;

head3->link=head3;

void insert(struct node *h)

int ex,co,i,n;

struct node *p;

struct node *x;

printf("\nEnter the number of terms: ");

scanf("%d",&n);

p=h;

for(i=0;i<n;i++)

x=(struct node *)malloc(sizeof(struct node));

printf("\nCo-efficient : ");

scanf("%d",&co);

printf("\nExponent : ");

scanf("%d",&ex);

x->coeff=co;

x->exp=ex;

x->link=h;

p->link=x;

p=x;

}
}

void display()

struct node *p;

p=head3->link;

while(p->exp!=-1)

printf("\n coeff: %d",p->coeff);

printf("\n exp: %d",p->exp);

p=p->link;

void attach(int c,int e,struct node *d)

struct node *s;

s=(struct node *)malloc(sizeof(struct node));

s->coeff=c;

s->exp=e;

s->link=head3;

d->link=s;

d=s;

display();

void add(struct node *h1,struct node *h2)

{
struct node *p;

struct node *q;

struct node *r;

struct node *d;

d=head3->link;

p=h1->link;

q=h2->link;

while((p->exp!=-1)&&(q->exp!=-1))

int x;

if((p->exp)==(q->exp))

x=p->coeff+q->coeff;

if(x!=0)

attach(x,p->exp,d);

p=p->link;

q=q->link;

else if((p->exp)>(q->exp))

attach(p->coeff,p->exp,d);

p=p->link;

}
else

attach(q->coeff,q->exp,d);

q=q->link;

while(p->exp!=-1)

attach(p->coeff,p->exp,d);

p=p->link;

while(q->exp!=-1)

attach(q->coeff,q->exp,d);

q=q->link;

void main()

create();

printf("\nPOLYNOMIAL 1\n");

insert(head1);

printf("\nPOLYNOMIAL 2\n");

insert(head2);
printf("\nRESULT\n");

add(head1,head2);

SAMPLE INPUT AND OUTPUT:

RESULT:
Thus the above code has been verified successfully.

You might also like