Showing posts with label Linked Lists. Show all posts
Showing posts with label Linked Lists. Show all posts

Thursday, 30 November 2017

POLYNOMIAL ADDITION USING LINKED LIST IN C

#include <stdio.h>
#include <stdlib.h>
struct Node{
int co,po;
struct Node*plink;
};
struct head{
struct Node*pfirst;
};
struct head *pl1,*pl2,*pl3;
void createlist(struct head*);
struct Node* create();
struct Node* mem(int,int);
void disp(struct head *);
void insert(struct head*,struct Node*);
void Add(struct head*,struct head*,struct head*);
int main() {
pl1=(struct head*)malloc(sizeof(struct head));
pl1->pfirst=NULL;
pl2=(struct head*)malloc(sizeof(struct head));
pl2->pfirst=NULL;
pl3=(struct head*)malloc(sizeof(struct head));
pl3->pfirst=NULL;
if(pl1&&pl2&&pl3){
printf("  enter poly exp1:\n");
createlist(pl1);
printf("  enter poly exp2:\n");
createlist(pl2);
printf("poly 1:\n");
disp(pl1);
printf("\n");
printf("poly 2:\n");
disp(pl2);
printf("\n");
Add(pl1,pl2,pl3);
printf("poly exp after adding is\n");
disp(pl3);

}
return 0;
}
struct Node* create(){
struct Node* ptemp=(struct Node*)malloc(sizeof(struct Node));
if(!ptemp) return NULL;
else{
printf("enter Coefficient,power\n");scanf("%d%d",&(ptemp->co),&(ptemp->po));ptemp->plink=NULL;return ptemp;
}
}
void createlist(struct head*ptr){struct Node* ptemp=NULL;
int i,n=0;
printf("enter highest power of expression\n");
scanf("%d",&n);for(i=0;i<=n;i++)
{
ptemp=create();
insert(ptr,ptemp);
}
return;}
void insert(struct head*ptr1,struct Node*p1)
{
if(!p1){
printf("no memory \n");
return;
}
else{
struct Node *ptemp1=NULL;
if(!(ptr1->pfirst)){
ptr1->pfirst=p1;
}
else
{
ptemp1=ptr1->pfirst;
while(ptemp1->plink)
ptemp1=ptemp1->plink;
ptemp1->plink=p1;
}
return;
}
}
struct Node* mem(int c,int p){
struct Node* ptr=(struct Node*)malloc(sizeof(struct Node));
if(!ptr)
return NULL;
else{
ptr->co=c;
ptr->po=p;ptr->plink=NULL;return ptr;
}}
void disp(struct head*ptr2){
struct Node*ptemp=NULL;
if(!ptr2->pfirst){
printf("empty polynomial\n");return;
}
else{
int i=0;
ptemp=ptr2->pfirst;
do{
if(((ptemp->co)>0)&&i!=0){
printf("+");
}
printf("%dx^%d",ptemp->co,ptemp->po);
ptemp=ptemp->plink;
i++;
}while(ptemp);
}
return;
}
void Add(struct head*pt1,struct head*pt2,struct head*pt3){
struct Node *ptl1=NULL,*ptl2=NULL,*ptl3=NULL;
ptl1=pt1->pfirst; ptl2=pt2->pfirst;
int a;
while((ptl1)&&(ptl2)){
if((ptl1->po)>(ptl2->po)){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
else if((ptl2->po)>(ptl1->po)){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
else{
a=(ptl1->co)+(ptl2->co);
ptl3=mem(a,ptl1->po);
ptl1=ptl1->plink; ptl2=ptl2->plink;insert(pt3,ptl3);
}
}
while(ptl1){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
while(ptl2){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
return;
} 🔜


Tuesday, 26 September 2017

C PROGRAM TO PERFORM CONCATENATION OF TWO SINGLY LINKED LISTS

#include<stdio.h>
#include<stdlib.h>
struct num
{
int a;
struct num*plink;
};
struct list
{
int n;
struct num*pfirst;
};
struct list*pl1,*pl2;
struct num* create();
void insert(struct list*,struct num*);
void listcon(struct list*,struct list*);
void print(struct list*);
void createlist(struct list*);

int main()
{
pl1=(struct list*)malloc(sizeof(struct list));
pl2=(struct list*)malloc(sizeof(struct list));
if((!pl1)||(!pl2))
{
printf("no memory\n");
return 0;
}
else
{
pl1->n=0;
pl1->pfirst=NULL;
pl2->n=0;
pl2->pfirst=NULL;
printf("creating list for list 1\n");
printf("\n");
createlist(pl1);
printf("creating list for list 2\n");
printf("\n");
createlist(pl2);
listcon(pl1,pl2);
print(pl1);
printf("\n");
return 0;
}
}
struct num* create()
{
struct num *ptemp=NULL;
ptemp=(struct num*)malloc(sizeof(struct num));
if(!ptemp)
return NULL;
else
{
printf("enter integer\n");
scanf("%d",&(ptemp->a));
ptemp->plink=NULL;
return ptemp;
}
}
void insert(struct list*pl,struct num*pn)
{
struct num*ptemp=NULL;
if(!(pl->pfirst))
{
pl->pfirst=pn;
}
else
{
ptemp=pl->pfirst;
while(ptemp->plink)
{
ptemp=ptemp->plink;
}
ptemp->plink=pn;
}
return;
}
void createlist(struct list*ptr)
{
struct num*pnew=NULL;
int i,n=0;
printf("enter number of elements to be inserted\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pnew=create();
insert(ptr,pnew);
}
return;
}
void listcon(struct list*l1,struct list*l2)
{
if((!l1)||(!l2))
{
printf("concatention cannot be done\n");
return;
}
else
{
struct num*ptemp=NULL,*ptrav=NULL,*plast=NULL;
plast=l1->pfirst;
ptrav=l2->pfirst;
while(plast->plink)
{
plast=plast->plink;
}
do
{
ptemp=(struct num*)malloc(sizeof(struct num));
if(!ptemp)
{
printf("concatention cannot be done\n");
return;
}
else
{
ptemp->a=ptrav->a;
ptemp->plink=NULL;
plast->plink=ptemp;
plast=plast->plink;
ptrav=ptrav->plink;
}
}while(ptrav);
}
}
void print(struct list*plk)
{
struct num*ptemp=NULL;
if(plk->pfirst==NULL)
{
printf("EMPTY LIST\n");
return;
}
else
{
ptemp=plk->pfirst;
while(ptemp!=NULL)
{
printf("%d\t",ptemp->a);
ptemp=ptemp->plink;
}
return;
}
}


FERMATS LITTLE THEOREM

import java.math.*; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) {    Sca...