Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
868 views

Polynomial Addition & Doubly Linked List

The document contains code for implementing polynomial addition and a doubly linked list data structure in C programming language. The polynomial addition code defines a linked list node structure with coefficient and power fields and implements functions to create two polynomials by taking user input, add the polynomials term by term, and display the resulting polynomial. The doubly linked list code defines a node structure with data and next/prev pointer fields and implements functions to create an empty list, insert/delete nodes at the beginning, middle, or end of the list, and display the list.

Uploaded by

SHIVALKAR J
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
868 views

Polynomial Addition & Doubly Linked List

The document contains code for implementing polynomial addition and a doubly linked list data structure in C programming language. The polynomial addition code defines a linked list node structure with coefficient and power fields and implements functions to create two polynomials by taking user input, add the polynomials term by term, and display the resulting polynomial. The doubly linked list code defines a node structure with data and next/prev pointer fields and implements functions to create an empty list, insert/delete nodes at the beginning, middle, or end of the list, and display the list.

Uploaded by

SHIVALKAR J
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Program:

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n continue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
main()
{
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st Number:");
show(poly1);
printf("\n2nd Number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:");
show(poly);
printf("\n add two more numbers:");
ch=getch();
}
while(ch=='y' || ch=='Y');
}

DOUBLY LINKED LIST

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *prev,*next;
};
typedef struct node node;
node *head,*last,*temp,*t,*p;
int d;
void createlist();
void disp();
void insfirst();
void insmiddle();
void inslast();
void delfirst();
void delmiddle();
void dellast();
void main()
{
int ch;
while(1)
{
clrscr();
printf("\n1. Create List");
printf("\n2. Insert First ");
printf("\n3. Insert Middle ");
printf("\n4. Insert Last ");
printf("\n5. Delete First ");
printf("\n6. Delete Middle ");
printf("\n7. Delete Last ");
printf("\n8. Exit");
printf("\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
createlist();
disp();
break;
case 2:
insfirst();
disp();
break;
case 3:
insmiddle();
disp();
break;
case 4:
inslast();
disp();
break;
case 5:
delfirst();
disp();
break;
case 6:
delmiddle();
disp();
break;
case 7:
dellast();
disp();
break;
case 8:
exit(0);
default:
printf("\nInvalid Choice");
}
getch();
}
}
void createlist()
{
char c;
do
{
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=last=temp;
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
printf("\nDo you like to add another node - (press y) : ");
fflush(stdin);
scanf("%c",&c);
}while(c=='y');
}
void insfirst()
{
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
void disp()
{
printf("\n List Status \n\n");
if(head==NULL)
printf("\nList is Empty");
else
{
temp=head;
while(temp!=NULL)
{
printf("->%d",temp->data);
temp=temp->next;
}
}
}
void inslast()
{
node *t;
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
while(t->next!=NULL)
t=t->next;

t->next=temp;
temp->prev=t;
}
}
void insmiddle()
{
int d;
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
printf("\nEnter the node after which insertion to be made : ");
scanf("%d",&d);
while(t!=NULL)
{
if(t->data==d)
{
temp->next=t->next;
temp->prev=t;
t->next=temp;
return;
}
else
t=t->next;
}
printf("\nInsert node not found");
}
}
void delfirst()
{
if(head==NULL)
printf("\nList is Empty");
else
{
t=head;
printf("\nDeleted node is %d\n",t->data);

head=head->next;
head->prev=NULL;
free(t);
}
}

void delmiddle()
{
int d;
node *s,*n;
if(head==NULL)
printf("\nList is Empty");
else
{
printf("\nEnter the node data to be deleted : ");
scanf("%d",&d);
if(head->data==d)
{
t=head;
head=head->next;
head->prev=NULL;
printf("\nDeleted node is %d\n",t->data);
free(t);
}
else
{
t=head;
while(t->next!=NULL)
{
if(t->data==d)
{
s=t;
printf("\nDeleted node is %d\n",s->data);
p=t->prev;
n=t->next;
p->next=t->next;
n->prev=p;

free(s);
}
else
{
p=p->next;
t=t->next;
}
}
}
}}
void dellast()
{
if(head==NULL)
printf("\nList is Empty");
else if(head->next==NULL)
{
t=head;
printf("\nDeleted node is %d\n",t->data);
head=NULL;
}
else
{
t=head;
while(t->next!=NULL)
{
t=t->next;
}
p=t->prev;
p->next=NULL;
printf("\nDeleted node is %d\n",t->data);
free(t);
}
}

You might also like