Computer Programms & Informations
Tuesday, November 22, 2016
Thursday, April 30, 2015
Sum of two polynomial: Implementation using linked-list.
#include<alloc.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct poly
{
int deg;
int constant;
struct poly *next;
};
void create_poly(int, int, struct poly**);
void disp_poly(struct poly*);
void sum_poly(struct poly*, struct poly*, struct poly**);
void main()
{
struct poly *P1,*P2,*SUM_POLY;
int degree,constant;
clrscr();
P1=P2=SUM_POLY=NULL;
printf("\n\tPOLYNOMIAL 1 construction :: \n");
printf("\nEnter degree of polynomial 1 : ");
scanf("%d",°ree);
if(degree==0)
goto NEXTP1;
while(degree>=0)
{
printf("\nEnter constant for degree %d : ",degree);
scanf("%d",&constant);
if(constant==0)
{
degree--;
continue;
}
else
create_poly(degree,constant,&P1);
degree--;
}
NEXTP1:
printf("\n\tPOLYNOMIAL 2 construction :: \n");
printf("\nEnter degree of polynomial 2 : ");
scanf("%d",°ree);
if(degree==0)
goto NEXTP2;
while(degree>=0)
{
printf("\nEnter constant for degree %d : ",degree);
scanf("%d",&constant);
if(constant==0)
{
degree--;
continue;
}
else
create_poly(degree,constant,&P2);
degree--;
}
NEXTP2:
sum_poly(P1,P2,&SUM_POLY);
printf("\nPolynomial 1 ==>\n\t");
disp_poly(P1);
printf("\nPolynomial 2 ==>\n\t");
disp_poly(P2);
printf("\nSum of Polynomials is ==>\n\t");
disp_poly(SUM_POLY);
getch();
}
void create_poly(int d, int c, struct poly **q)
{
struct poly *temp;
temp=*q;
if(temp==NULL)
{
temp=*q=(struct poly*)malloc(sizeof(struct poly));
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct poly*)malloc(sizeof(struct poly));
temp=temp->next;
}
temp->deg=d;
temp->constant=c;
temp->next=NULL;
}
void disp_poly(struct poly *q)
{
struct poly *temp;
temp=q;
while(temp!=NULL)
{
printf("%dx^%d",abs(temp->constant),temp->deg);
if(temp->next!=NULL)
if((temp->next)->constant>=0)
printf(" + ");
else
printf(" - ");
temp=temp->next;
}
}
//***********************************************
//sum_poly() function for adding two polynomials.
//***********************************************
void sum_poly(struct poly *p1, struct poly *p2, struct poly **sum_poly)
{
struct poly *t1,*t2,*sum;
int count=1;
t1=p1;
t2=p2;
if(t1==NULL && t2==NULL)
{
printf("Cannot merge polynomials of order 0");
getch();
exit(0);
}
if(t1==NULL)
{
*sum_poly=t2;
goto LAST;
}
if(t2==NULL)
{
*sum_poly=t1;
goto LAST;
}
sum=*sum_poly;
while(t1!=NULL || t2!=NULL)
{
if(t1==NULL)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t2->constant;
sum->deg=t2->deg;
sum->next=NULL;
t2=t2->next;
}
else
if(t2==NULL)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t1->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
}
else
if(t1->deg==t2->deg)
{
if(sum==NULL)
{
*sum_poly=(struct poly*)malloc(sizeof(struct poly));
sum=*sum_poly;
}
else
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
}
sum->constant=t1->constant+t2->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
t2=t2->next;
if(count==1)
*sum_poly=sum;
}
else
if(t1->deg>t2->deg)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t1->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
if(count==1)
*sum_poly=sum;
}
else
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t2->constant;
sum->deg=t2->deg;
sum->next=NULL;
t2=t2->next;
if(count==1)
*sum_poly=sum;
}
count++;
}//wend
LAST:
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct poly
{
int deg;
int constant;
struct poly *next;
};
void create_poly(int, int, struct poly**);
void disp_poly(struct poly*);
void sum_poly(struct poly*, struct poly*, struct poly**);
void main()
{
struct poly *P1,*P2,*SUM_POLY;
int degree,constant;
clrscr();
P1=P2=SUM_POLY=NULL;
printf("\n\tPOLYNOMIAL 1 construction :: \n");
printf("\nEnter degree of polynomial 1 : ");
scanf("%d",°ree);
if(degree==0)
goto NEXTP1;
while(degree>=0)
{
printf("\nEnter constant for degree %d : ",degree);
scanf("%d",&constant);
if(constant==0)
{
degree--;
continue;
}
else
create_poly(degree,constant,&P1);
degree--;
}
NEXTP1:
printf("\n\tPOLYNOMIAL 2 construction :: \n");
printf("\nEnter degree of polynomial 2 : ");
scanf("%d",°ree);
if(degree==0)
goto NEXTP2;
while(degree>=0)
{
printf("\nEnter constant for degree %d : ",degree);
scanf("%d",&constant);
if(constant==0)
{
degree--;
continue;
}
else
create_poly(degree,constant,&P2);
degree--;
}
NEXTP2:
sum_poly(P1,P2,&SUM_POLY);
printf("\nPolynomial 1 ==>\n\t");
disp_poly(P1);
printf("\nPolynomial 2 ==>\n\t");
disp_poly(P2);
printf("\nSum of Polynomials is ==>\n\t");
disp_poly(SUM_POLY);
getch();
}
void create_poly(int d, int c, struct poly **q)
{
struct poly *temp;
temp=*q;
if(temp==NULL)
{
temp=*q=(struct poly*)malloc(sizeof(struct poly));
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct poly*)malloc(sizeof(struct poly));
temp=temp->next;
}
temp->deg=d;
temp->constant=c;
temp->next=NULL;
}
void disp_poly(struct poly *q)
{
struct poly *temp;
temp=q;
while(temp!=NULL)
{
printf("%dx^%d",abs(temp->constant),temp->deg);
if(temp->next!=NULL)
if((temp->next)->constant>=0)
printf(" + ");
else
printf(" - ");
temp=temp->next;
}
}
//***********************************************
//sum_poly() function for adding two polynomials.
//***********************************************
void sum_poly(struct poly *p1, struct poly *p2, struct poly **sum_poly)
{
struct poly *t1,*t2,*sum;
int count=1;
t1=p1;
t2=p2;
if(t1==NULL && t2==NULL)
{
printf("Cannot merge polynomials of order 0");
getch();
exit(0);
}
if(t1==NULL)
{
*sum_poly=t2;
goto LAST;
}
if(t2==NULL)
{
*sum_poly=t1;
goto LAST;
}
sum=*sum_poly;
while(t1!=NULL || t2!=NULL)
{
if(t1==NULL)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t2->constant;
sum->deg=t2->deg;
sum->next=NULL;
t2=t2->next;
}
else
if(t2==NULL)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t1->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
}
else
if(t1->deg==t2->deg)
{
if(sum==NULL)
{
*sum_poly=(struct poly*)malloc(sizeof(struct poly));
sum=*sum_poly;
}
else
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
}
sum->constant=t1->constant+t2->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
t2=t2->next;
if(count==1)
*sum_poly=sum;
}
else
if(t1->deg>t2->deg)
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t1->constant;
sum->deg=t1->deg;
sum->next=NULL;
t1=t1->next;
if(count==1)
*sum_poly=sum;
}
else
{
sum->next=(struct poly*)malloc(sizeof(struct poly));
sum=sum->next;
sum->constant=t2->constant;
sum->deg=t2->deg;
sum->next=NULL;
t2=t2->next;
if(count==1)
*sum_poly=sum;
}
count++;
}//wend
LAST:
}
Wednesday, April 8, 2015
Merge to Nodes in Doubly Link List
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(*q==NULL)
{
*q=(struct node *)malloc(sizeof(struct node));
printf("\n***list is empty**\nenter the data");
scanf("%d",&(*q)->data);
(*q)->next=NULL;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
printf("\nenter the another node :");
scanf("%d",&temp->data);
temp->next=NULL;
}
printf("\nnode created successfully");
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
}
void main()
{
struct node *start,*start2;
clrscr();
start=NULL;
start2=NULL;
append(&start);
append(&start);
printf("\nfirst link list");
display(start);
append(&start2);
append(&start2);
printf("\n\nsecond link list");
display(start);
printf("\nmerge ");
merge(&start,&start2);
display(start);
getch();
}
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(*q==NULL)
{
*q=(struct node *)malloc(sizeof(struct node));
printf("\n***list is empty**\nenter the data");
scanf("%d",&(*q)->data);
(*q)->next=NULL;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
printf("\nenter the another node :");
scanf("%d",&temp->data);
temp->next=NULL;
}
printf("\nnode created successfully");
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
}
void main()
{
struct node *start,*start2;
clrscr();
start=NULL;
start2=NULL;
append(&start);
append(&start);
printf("\nfirst link list");
display(start);
append(&start2);
append(&start2);
printf("\n\nsecond link list");
display(start);
printf("\nmerge ");
merge(&start,&start2);
display(start);
getch();
}
Tuesday, April 7, 2015
What is the Function of Converting Doubly Link List into Circular Link List.Write a programm to convert Doubly Link List into Circular Link List
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(temp==NULL)
{
(*q)=(struct node *)malloc(sizeof(struct node));
scanf("%d",&(*q)->data);
(*q)->next=NULL;
(*q)->prev=NULL;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
}
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
getch();
}
void append2(struct node **q) //DLL to CLL
{
struct node *temp;
temp=*q;
while(temp!=NULL)
temp=temp->next;
temp->next=(*q);
}
void display2(struct node *q)
{
struct node *temp;
temp=q;
while(temp->next!=q)
{
printf(" %d",temp->data);
temp=temp->next;
}
getch();
}
void main()
{
struct node *start;
start=NULL;
clrscr();
printf("\nenter the Doubly Link
List");
append(&start);
append(&start);
append(&start);
printf("\nthe Following Doubly Link
List");
display(start);
append2(&start);
printf("\nList After Conversion Circular Link List");
display2(start);
getch();
Convert of Singly Link List into Circular Link List.Write a programm to Convert of Singly Link List into Circular Link List.
WAP to convert SLL into DLL.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(temp==NULL)
{
(*q)=(struct node *)malloc(sizeof(struct node));
scanf("%d",&(*q)->data);
(*q)->next=NULL;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
scanf("%d",&temp->data);
temp->next=NULL;
}
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
}
void append2(struct node **q) //Function of making SLL into CLL
{
struct node *temp;
temp=*q;
while(temp!=NULL)
temp=temp->next;
temp->next=(*q);
}
void display2(struct node *q)
{
struct node *temp;
temp=q;
while(temp->next!=q)
{
printf(" %d",temp->data);
temp=temp->next;
}
getch();
}
void main()
{
struct node *start;
start=NULL;
clrscr();
printf("\nenter the Singly Link
List");
append(&start);
append(&start);
append(&start);
printf("\nthe Fllowing Singly Link
List");
display(start);
append2(&start);
printf("\nAfter Conversion of Singly Link List into Circular Link
List");
display2(start);
getch();
}
The following will be the output :
The following will be the output :
SLL to CLL |
Subscribe to:
Posts (Atom)