CONCATENATION OF A SINGLY LINKED LIST IN C
#include<stdio.h>
#include<stdlib.h>
struct NODE
{
int x;
struct NODE *pnext;
};
struct LIST
{
int count;
struct NODE *pfirst;
};
struct LIST *PL1,*PL2;
void createlist(struct LIST *);
void insertnode(struct LIST *,struct NODE *);
void listconcat(struct LIST *,struct LIST *);
struct NODE * createnode(void);
void display(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->count=0;
PL2->count=0;
PL1->pfirst=NULL;
PL2->pfirst=NULL;
createlist(PL1);
createlist(PL2);
display(PL1);
display(PL2);
listconcat(PL1,PL2);
display(PL1);
display(PL2);
return 0;
}
}
struct NODE * createnode(void)
{
int n=0;
printf("enter a number :\n");
scanf("%d",&n);
struct NODE * pt=(struct NODE *)malloc(sizeof(struct NODE));
if(!pt)
return NULL;
else
{
pt->x=n;
return pt;
}
}
void display(struct LIST *l)
{
struct NODE *pt=l->pfirst;
if(!pt)
{
printf("Empty list\n");
}
else
{
printf("The List has :\n");
do{
printf("%d\t",pt->x);
pt=pt->pnext;
}while(pt);
}
}
void createlist(struct LIST *pl)
{
struct NODE * pnew=NULL;
int n,i=0;
printf("enter number of elements :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pnew=createnode();
insertnode(pl,pnew);
}
return ;
}
void insertnode(struct LIST *pl,struct NODE *pnew)
{
struct NODE *pt=NULL;
if(pl->pfirst==NULL)
pl->pfirst=pnew;
else
{
pt=pl->pfirst;
while(pt->pnext)
pt=pt->pnext;
pt->pnext=pnew;
}
(pl->count)++;
return ;
}
void listconcat(struct LIST *l1,struct LIST *l2)
{
struct NODE *ptm=NULL,*pl=l1->pfirst,*pt=l2->pfirst;
while(pl->pnext!=NULL)
pl=pl->pnext;
while(pt)
{
ptm=(struct NODE *)malloc(sizeof(struct NODE));
ptm->x=pt->x;
ptm->pnext=NULL;
pl->pnext=ptm;
pl=pl->pnext;
pt=pt->pnext;
(l1->count)++;
}
return ;
}
#include<stdlib.h>
struct NODE
{
int x;
struct NODE *pnext;
};
struct LIST
{
int count;
struct NODE *pfirst;
};
struct LIST *PL1,*PL2;
void createlist(struct LIST *);
void insertnode(struct LIST *,struct NODE *);
void listconcat(struct LIST *,struct LIST *);
struct NODE * createnode(void);
void display(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->count=0;
PL2->count=0;
PL1->pfirst=NULL;
PL2->pfirst=NULL;
createlist(PL1);
createlist(PL2);
display(PL1);
display(PL2);
listconcat(PL1,PL2);
display(PL1);
display(PL2);
return 0;
}
}
struct NODE * createnode(void)
{
int n=0;
printf("enter a number :\n");
scanf("%d",&n);
struct NODE * pt=(struct NODE *)malloc(sizeof(struct NODE));
if(!pt)
return NULL;
else
{
pt->x=n;
return pt;
}
}
void display(struct LIST *l)
{
struct NODE *pt=l->pfirst;
if(!pt)
{
printf("Empty list\n");
}
else
{
printf("The List has :\n");
do{
printf("%d\t",pt->x);
pt=pt->pnext;
}while(pt);
}
}
void createlist(struct LIST *pl)
{
struct NODE * pnew=NULL;
int n,i=0;
printf("enter number of elements :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pnew=createnode();
insertnode(pl,pnew);
}
return ;
}
void insertnode(struct LIST *pl,struct NODE *pnew)
{
struct NODE *pt=NULL;
if(pl->pfirst==NULL)
pl->pfirst=pnew;
else
{
pt=pl->pfirst;
while(pt->pnext)
pt=pt->pnext;
pt->pnext=pnew;
}
(pl->count)++;
return ;
}
void listconcat(struct LIST *l1,struct LIST *l2)
{
struct NODE *ptm=NULL,*pl=l1->pfirst,*pt=l2->pfirst;
while(pl->pnext!=NULL)
pl=pl->pnext;
while(pt)
{
ptm=(struct NODE *)malloc(sizeof(struct NODE));
ptm->x=pt->x;
ptm->pnext=NULL;
pl->pnext=ptm;
pl=pl->pnext;
pt=pt->pnext;
(l1->count)++;
}
return ;
}
Comments
Post a Comment