KEMBAR78
List-Array Implementation: CS6202-Programming & Data Structures | PDF | Algorithms And Data Structures | Software Engineering
0% found this document useful (0 votes)
89 views36 pages

List-Array Implementation: CS6202-Programming & Data Structures

This document describes code for implementing a doubly linked list data structure in C. It includes functions to create and initialize the list, find elements within the list, insert new elements, display the list, and delete elements. The main function provides a menu for testing these list operations and allows the user to repeatedly choose an operation to perform on the doubly linked list.
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)
89 views36 pages

List-Array Implementation: CS6202-Programming & Data Structures

This document describes code for implementing a doubly linked list data structure in C. It includes functions to create and initialize the list, find elements within the list, insert new elements, display the list, and delete elements. The main function provides a menu for testing these list operations and allows the user to repeatedly choose an operation to perform on the doubly linked list.
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/ 36

CS6202- Programming & Data structures

LIST-ARRAY IMPLEMENTATION
#include<stdio.h>
#define MAX 10
int a[10];
int i,ch,n;
void insert()
{
int i,num,pos;
printf("enter the number to insert\n");
scanf("%d", &num);
printf("enter the position in which it has to be inserted\n");
scanf("%d", &pos);
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos]=num;
n++;
}

void insfirst()
{
int i,num;
printf("enter the number to insert\n");
scanf("%d", &num);
for(i=n;i>=0;i--)
{
a[i]=a[i-1];
}
a[0]=num;
n++;
}

void inslast()
{
int num;
printf("enter the number to insert\n");
scanf("%d",&num);
a[n]=num;
n++;
}

void display()
{

1
CS6202- Programming & Data structures

int i;
printf("list elemts\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}

void search()
{
int i,num;
printf("enter the element to be searched\n");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("\n The element %d is present at the %dth position",num,i+1);
return;
}
}
}

void del()
{
int pos,i;
printf("enter the position of elmt to be deleted\n");
scanf("%d",&pos);
for(i=pos;i<n;i++)
{
a[i-1]=a[i];
}
n--;
}

void main()
{
printf("------creating a list------\n\n" );
printf("Enter the number of elements in the list\n");
scanf("%d",&n);
if(n>MAX)
{
printf("out of range\n");
}

2
CS6202- Programming & Data structures

printf("enter the elements \n");


for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("elements in the list\n");
for(i=0;i<n;i++)
{
printf("%d\t", a[i]);
}
while(1)
{
printf("\n\n1.Insert\n");
printf("2.Display\n");
printf("3.Delete\n");
printf("4.search\n");
printf("5.Insertfirst\n");
printf("6.InsertLast\n");
printf("7.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
search();
break;
case 5:
insfirst();
break;
case 6:
inslast();
break;
case 7:
exit(0);
}
}

3
CS6202- Programming & Data structures

OUTPUT:
------creating a list------
Enter the number of elements in the list
3
enter the elements
1
2
3
elements in the list
1 2 3

1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
1
enter the number to insert
4
enter the position in which it has to be inserted
2

1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
1 2 4 3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
4

4
CS6202- Programming & Data structures

enter the element to be searched


2
The element 2 is present at the 2th position
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
5
enter the number to insert
8
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
8 1 2 4 3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
6
enter the number to insert
5
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
8 1 2 4 3 5
1.Insert

5
CS6202- Programming & Data structures

2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
3
enter the position of elmt to be deleted
3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
8 1 4 3 5

6
CS6202- Programming & Data structures

SINGLY LINKED LIST – LINKED LIST IMPLEMENTATION


#include <stdio.h>
typedef struct node *list;
typedef struct node *position;
list head;
struct node
{
int element;
struct node *next;
};
int isempty(list l)
{
return l->next==NULL;
}

int islast(position p)
{
return p->next==NULL;
}

list getnode()
{
position p;
p=(list)malloc(sizeof(struct node));
return p;
}

void display(list l)
{
printf("The list elements are:\n");

while(l->next!=NULL)
{
printf("%d->",l->element);
l=l->next;
}
printf("%d->",l->element);

position find(int x,list l)


{
position p;
p=l->next;

7
CS6202- Programming & Data structures

while(p!=NULL && p->element!=x)


p=p->next;
return p;
}

void insert(int x,position p)


{
position new,tmpcell;
int ch,key;
new=p;
tmpcell=getnode();
if(tmpcell==NULL)
{
printf("out of space\n");
}
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmpcell->element=x;
tmpcell->next=p->next;
p->next=tmpcell;
break;

case 2:
printf("\nEnter the key after which it has to be inserted: ");
scanf("%d",&key);
p=find(key,new);
tmpcell->element=x;
tmpcell->next=p->next;
p->next=tmpcell;
break;

case 3:
while(new->next!=NULL)
{
new=new->next;
}
tmpcell->element=x;
tmpcell->next=NULL;
new->next=tmpcell;

8
CS6202- Programming & Data structures

break;
}
}

void create ( )
{
head=getnode();
printf("\nEnter the header element:");
scanf("%d",&head->element);
head->next=NULL;
}

position findprevious(int x, list l)


{
position p;
p=l;
while(p->next!=NULL && p->next->element!=x)
p=p->next;
return p;
}

void deletion(list l)
{
int x,ch;
position p,tmpcell;
if(l->next==NULL)
printf("LIST IS EMPTY\n");
else
{
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(l->next->next==NULL)
{
l->next=NULL;
free(l->next);
}
else
{
tmpcell=l->next;

9
CS6202- Programming & Data structures

l->next=l->next->next;
free(tmpcell);
}
break;

case 2:
printf("\nEnter the elemnt to be deleted:");
scanf("%d",&x);
p=findprevious(x,l);
if(!islast(p))
{
tmpcell=p->next;
p->next=tmpcell->next;
free(tmpcell);
}
break;

case 3:
p=l;
while(p->next!=NULL)
{
tmpcell=p;
p=p->next;
}
tmpcell->next=NULL;
free(p);
break;
}
}
}

void main()
{
list p;
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();

10
CS6202- Programming & Data structures

break;

case 2:
display(head);
break;

case 3:
printf("\nEnter the element:");
scanf("%d",&ele);
insert(ele,head);
break;

case 4:
deletion(head);
break;

case 5:
exit(0);
}
}
}

OUTPUT:
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:1
Enter the header element:0
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:2
1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display

11
CS6202- Programming & Data structures

3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:5
1.first
2.middle
3.last
Enter the choice:
3
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->2->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:4
1.first
2.middle
3.last
Enter the choice:
2
Enter the key after which it has to be inserted: 2
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:8
1.first
2.middle
3.last
Enter the choice:
1
1.Create

12
CS6202- Programming & Data structures

2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:9
1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->9->8->2->4->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4

1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->8->2->4->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit

13
CS6202- Programming & Data structures

Enter the choice:4


1.first
2.middle
3.last
Enter the choice:
3
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->8->2->4->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.first
2.middle
3.last
Enter the choice:
2
Enter the elemnt to be deleted:2
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->8->4->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:

14
CS6202- Programming & Data structures

DOUBLY LINKED LIST


#include<stdio.h>
typedef struct node *list;
typedef struct node *position;
list head;
position p;

struct node
{
int ele;
position next;
position prev;
};
position create();
position find(int,list);
void insert(int,list);
void display(list);
void deletion(list);

void main()
{
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");

scanf("%d",&choice);
switch(choice)
{
case 1:
head=create();
break;

case 2:
display(head);
break;

case 3:
printf("\nEnter the element:");
scanf("%d",&ele);
insert(ele,head);
break;

15
CS6202- Programming & Data structures

case 4:
deletion(head);
break;

case 5:
exit(0);
}
}
}

void deletion(list temp)


{
position r;
int ch,x;
r=temp;
printf("\n\n1.Start\n2.Intermediate\n3.End");
printf("\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
r=temp->next;
if(temp->next->next==NULL)
{
printf("deleted element is %d and list is now empty\n",r->ele);
temp->next=NULL;
free(r);
}
else
{
printf("deleted element is %d\n",r->ele);
r->next->prev=temp;
temp->next=r->next;
free(r);
}
break;

case 2:
printf("\nEnter the element to be deleted:");
scanf("%d",&x);
p=find(x,temp);
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);

16
CS6202- Programming & Data structures

break;

case 3:
while(r->next!=NULL)
{
r=r->next;
}
temp=r->prev;
temp->next=NULL;
free(r);
break;
}
}

position create()
{
head=(list)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\nunable 2 create head node");
}
printf("\nEnter the header node value:");
scanf("%d",&head->ele);
head->prev=head->next=NULL;
return head;
}

position find(int x,list temp)


{
while(temp!=NULL&&temp->ele!=x)
{
temp=temp->next;
}
return temp;
}

void insert(int x,list head)


{
int ch,key;
position temp;
position r;
r=head;
if(head==NULL)
{

17
CS6202- Programming & Data structures

printf("\nList is empty create a header node and try");


}
printf("\n\n1.Beggining\n2.Middle\n3.End");
printf("\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(list)malloc(sizeof(struct node));
temp->ele=x;
temp->prev=head;
temp->next=head->next;
if(temp->next!=NULL)
{
head->next->prev=temp;
}
head->next=temp;
break;

case 2:
printf("\nEnter the elment after which it has to be inserted: ");
scanf("%d",&key);
temp=(list)malloc(sizeof(struct node));
p=find(key,head);
temp->ele=x;
temp->prev=p;
temp->next=p->next;
temp->next->prev=temp;
p->next=temp;
break;

case 3:
temp=(list)malloc(sizeof(struct node));
temp->ele=x;
while(r->next!=NULL)
{
r=r->next;
}
temp->prev=r;
temp->next=NULL;
r->next=temp;
break;
}
}

18
CS6202- Programming & Data structures

void display(position head)


{
position p;
p=head;
while(p->next!=NULL)
{
printf("%d\t",p->ele);
p=p->next;
}
printf("%d",p->ele);
}

OUTPUT:
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:1
Enter the header node value:0
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:1
1.Beggining
2.Middle
3.End
Enter the choice: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:4
1.Beggining
2.Middle
3.End
Enter the choice: 3

19
CS6202- Programming & Data structures

1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 1 4
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:6
1.Beggining
2.Middle
3.End
Enter the choice: 2
Enter the elment after which it has to be inserted: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:8
1.Beggining
2.Middle
3.End
Enter the choice: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:9
1.Beggining
2.Middle
3.End
Enter the choice: 3
1.Create
2.Display
3.Insert

20
CS6202- Programming & Data structures

4.Delete
5.Exit
Enter the choice:2
0 8 1 6 4 9
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 2
Enter the element to be deleted:1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 8 6 4 9
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 1
deleted element is 8
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 3

21
CS6202- Programming & Data structures

1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 6 4

22
CS6202- Programming & Data structures

CIRCULAR LINKED LIST:


#include <stdio.h>
typedef struct node *list;
typedef struct node *position;
list head;
struct node
{
int element;
struct node *next;
};

list getnode()
{
position p;
p=(list)malloc(sizeof(struct node));
return p;
}

void display(list l)
{
position temp;
printf("The list elements are:\n");
if(l->next==head)
{
printf("List is empty\n");
return;
}
temp=head->next;
while(temp!=head)
{
printf("%d->",temp->element);
temp=temp->next;
}
}

position find(int x,list l)


{
position p;
p=l->next;
while(p!=head && p->element!=x) //changes
p=p->next;
return p;
}

23
CS6202- Programming & Data structures

void insert(int x)
{
position cur,tmpcell;
int ch,key;
tmpcell=getnode();
if(tmpcell==NULL)
{
printf("out of space\n");
}
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmpcell->element=x;
tmpcell->next=head->next;
head->next=tmpcell;
break;

case 2:
printf("\nEnter the key after which it has to be inserted: ");
scanf("%d",&key);
cur=find(key,head);
tmpcell->element=x;
tmpcell->next=cur->next;
cur->next=tmpcell;
break;

case 3:
cur=head->next;
while(cur->next!=head) //changes
{
cur=cur->next;
}
tmpcell->element=x;
tmpcell->next=head; //changes
cur->next=tmpcell;
break;
}
}

24
CS6202- Programming & Data structures

void create ( )
{
head=getnode();
printf("\nEnter the header element:");
scanf("%d",&head->element);
head->next=head; ///changes
}

void deletion(list l)
{
int x,ch;
position p,tmpcell;
if(l->next==NULL)
printf("LIST IS EMPTY\n");
else
{
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(l->next->next==head) //changes
{
l->next=head; //changes
free(l->next);
}
else
{
tmpcell=l->next;
l->next=l->next->next;
free(tmpcell);
}
break;

case 2:
printf("\nEnter the elemnt to be deleted:");
scanf("%d",&x);
position prev;
p=l;
while(p->element!=x) //changes
{
prev=p;
p=p->next;

25
CS6202- Programming & Data structures

}
tmpcell=prev->next;
prev->next=tmpcell->next;
free(tmpcell);
break;

case 3:
p=l;
while(p->next!=head) //changes
{
tmpcell=p;
p=p->next;
}
//tmpcell=findprevious(p->element,l);
tmpcell->next=head;
free(p);
break;
}
}
}

void main()
{
list p;
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;

case 2:
display(head);
break;

case 3:
printf("\nEnter the element:");
scanf("%d",&ele);

26
CS6202- Programming & Data structures

insert(ele);
break;

case 4:
deletion(head);
break;

case 5:
exit(0);
}
}
}

27
CS6202- Programming & Data structures

POLYNOMIAL ADT-ARRAY IMPLEMENTATION

#include<stdio.h>

#include<math.h>

/*

This structure is used to store a polynomial term. An array of such terms represents a

polynomial.

The "coeff" element stores the coefficient of a term in the polynomial,while

the "exp" element stores the exponent.

*/

struct poly

float coeff;

int exp;

};

//declaration of polynomials

struct poly a[50],b[50],c[50],d[50];

int main()

int i;

int deg1,deg2; //stores degrees of the polynomial

int k=0,l=0,m=0;

28
CS6202- Programming & Data structures

printf("Enter the highest degree of poly1:");

scanf("%d",&deg1);

//taking polynomial terms from the user

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

//entering values in coefficient of the polynomial terms

printf("\nEnter the coeff of x^%d :",i);

scanf("%f",&a[i].coeff);

//entering values in exponent of the polynomial terms

a[k++].exp = i;

//taking second polynomial from the user

printf("\nEnter the highest degree of poly2:");

scanf("%d",&deg2);

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

printf("\nEnter the coeff of x^%d :",i);

scanf("%f",&b[i].coeff);

29
CS6202- Programming & Data structures

b[l++].exp = i;

//printing first polynomial

printf("\nExpression 1 = %.1f",a[0].coeff);

for(i=1;i<=deg1;i++)

printf("+ %.1fx^%d",a[i].coeff,a[i].exp);

//printing second polynomial

printf("\nExpression 2 = %.1f",b[0].coeff);

for(i=1;i<=deg2;i++)

printf("+ %.1fx^%d",b[i].coeff,b[i].exp);

//Adding the polynomials

if(deg1>deg2)

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

c[m].coeff = a[i].coeff + b[i].coeff;

30
CS6202- Programming & Data structures

c[m].exp = a[i].exp;

m++;

for(i=deg2+1;i<=deg1;i++)

c[m].coeff = a[i].coeff;

c[m].exp = a[i].exp;

m++;

else

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

c[m].coeff = a[i].coeff + b[i].coeff;

c[m].exp = a[i].exp;

m++;

for(i=deg1+1;i<=deg2;i++)

c[m].coeff = b[i].coeff;

c[m].exp = b[i].exp;

m++;

31
CS6202- Programming & Data structures

//printing the sum of the two polynomials

printf("\nExpression after additon = %.1f",c[0].coeff);

for(i=1;i<m;i++)

printf("+ %.1fx^%d",c[i].coeff,c[i].exp);

return 0;

32
CS6202- Programming & Data structures

POLYNOMIAL MANIPULATION-LINKED LIST IMPLEMENTATION


#include<stdio.h>
#include<stdlib.h>
struct node
{
float coef;
int expo;
struct node *link;
};
typedef struct node *POLY;
POLY start=NULL,start1=NULL,start2=NULL,start3=NULL;
POLY create(POLY);
POLY insert(POLY,float,int);
void display(POLY);
void poly_add(POLY,POLY);
void poly_mult(POLY,POLY);

void main( )
{
// POLY *start1=NULL,*start2=NULL;

printf("Enter polynomial 1 :\n");


start1=create(start1);

printf("Enter polynomial 2 :\n");


start2=create(start2);

printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);

poly_add(start1, start2);
poly_mult(start1, start2);
}/*End of main()*/

POLY create(POLY start)


{
int i,n,ex;
float co;
printf("Enter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

33
CS6202- Programming & Data structures

printf("Enter coeficient for term %d : ",i);


scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert(start,co,ex);
}
return start;
}/*End of create()*/
POLY insert(POLY start,float co,int ex)
{
POLY ptr,tmp;
tmp=(POLY)malloc(sizeof(POLY));
tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex > start->expo)
{
tmp->link=NULL;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}/*End of insert()*/

void display(POLY ptr)


{
if(ptr==NULL)
{
printf("Zero polynomial\n");
return;
}
while(ptr!=NULL)
{
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");

34
CS6202- Programming & Data structures

else
printf("\n");
}
}/*End of display()*/
void poly_add(POLY p1,POLY p2)
{
POLY start3;
start3=NULL;

while(p1!=NULL && p2!=NULL)


{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
/*if poly2 has finished and elements left in poly1*/
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
/*if poly1 has finished and elements left in poly2*/
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is : ");
display(start3);
}/*End of poly_add() */

35
CS6202- Programming & Data structures

void poly_mult(POLY p1, POLY p2)


{
POLY start3;
POLY p2_beg = p2;
start3=NULL;
if(p1==NULL || p2==NULL)
{
printf("Multiplied polynomial is zero polynomial\n");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{
start3=insert(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("Multiplied polynomial is : ");
display(start3);
}/*End of poly_()*/

OUTPUT:
Enter polynomial 1 :
Enter the number of terms : 2
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 1
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 0
Enter polynomial 2 :
Enter the number of terms : 2
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 1
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 0
Polynomial 1 is : (3.0x^1) + (4.0x^0)
Polynomial 2 is : (3.0x^1) + (4.0x^0)
Added polynomial is : (6.0x^1) + (8.0x^0)
Multiplied polynomial is : (9.0x^2) + (12.0x^1) + (12.0x^1) + (16.0x^0)

36

You might also like