1.11M Members

concatenation of two linked lists

 
0
 
#include<stdio.h>
#include<conio.h>
struct link
{
int data;
struct link*ptr;
};
typedef struct link node;
struct link1
{
int info;
struct link1*next;
};
typedef struct link1 node1;
int main()
{
int i,size;
node*start,*temp,*p;
int j,listsize;
node1*start1,*temp1,*k;
clrscr();

{
printf("enter the size of first list:\n"); /*creating first list*/
scanf("%d",&size);
printf("enter list:\n");
start=NULL;
for(i=0;i<size;i++)
 {
  if(start==NULL)
   {
   start=(node*)malloc(sizeof(node));
   scanf("%d",&start->data);
   start->ptr=NULL;
   }
  else
  {
  for(p=start;p->ptr!=NULL;p=p->ptr)
  temp=(node*)malloc(sizeof(node));
  scanf("%d",&temp->data);
  temp->ptr=NULL;
  p->ptr=temp;
  }
 }
printf("enter the size of second list:\n"); /*creating 2nd list */
scanf("%d",&listsize);
printf("enter list:\n");
start1=NULL;
for(j=0;j<listsize;j++)
 {
   if(start1==NULL)
    {
     start1=(node1*)malloc(sizeof(node1));
     scanf("%d",&start1->info);
     start1->next=NULL;
    }
     else
      {
       for(k=start1;k->next!=NULL;k=k->next)
       temp1=(node1*)malloc(sizeof(node1));
       scanf("%d",&temp1->next);
       temp1->next=NULL;
       k->next=temp1;
      }
  }
for(p=start;p!=NULL;p=p->ptr) /*concatenation of two lists*/
{
for(k=start1;k!=NULL;k=k->next)
{
 temp->ptr=start1;
 temp1->next=NULL;
 }
 }
printf("concatenated list is:\n");
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
getch();
return 0;
}
}

it is giving me a warning when i compile this program that there is a "suspicious pointer conversion"....????
now i know that it must be somewhere in the concatenating part over there so plz let me know the problem area in my code here so that i can make the necessary changes...plz help me out!!!
what i am trying to do here is that im creating 2 lists suppose if the first list is 10 20 30 and the second list is 40 50 60 then concatenating them i should get...10 20 30 40 50 60
so that means that the address part of 3rd node of first list that is 30 should point to the first node of second list now and the pointer part should store 40 now in its address and so on.and then the last node that is 60 should have in its address part the NULL value...
but when i run this it is giving me some garbage value of several other digits...why?...
please help me out here!!!!

 
1
 

>temp->ptr=start1;
temp->ptr is a pointer to a link structure, start1 is a pointer to a link1 structure. They are not interchangeable.

 
0
 

so what do i do then? it is still not running.....help me out here ......please...

 
0
 

hello,
i tried this program to eliminate the warning message...

#include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void main()
{


int size1,size2,i, num;
struct node *ptr,*ptr2,*result,*temp;
struct node * concat(struct node *,struct node *);
void display(struct node *);
void add(struct node **,int );
clrscr();
ptr=NULL;
ptr2 =NULL;
result=NULL;
printf("enter the size of 1st list \n");
scanf("%d",&size1);
printf("enter the elements\n");
for(i=1;i<=size1;i++)
{
scanf("%d",&num);
add(&ptr,num);
}
printf("enter the size of 2st list \n");
scanf("%d",&size2);
printf("enter the elements\n");
for(i=1;i<=size2;i++)
{
scanf("%d", &num);
add(&ptr2, num);
}


result = concat(ptr,ptr2);
printf("the elements in concat list\n");
display(result);
getch();
}


// add an node to the list
void add(struct node **q,int num)
{
struct node *temp;
temp = *q;
if(*q==NULL)
{
*q=malloc(sizeof(struct node));
temp = *q;
}
else
{
while((temp->link)!=NULL)
{
temp=temp->link;
}
temp->link = malloc(sizeof(struct node));
temp=temp->link;
}
temp->data = num;
temp->link = NULL;
}


// display the elements in the list
void display(struct node *pt)
{
while(pt!=NULL)
{
printf(" %d\n",pt->data);
pt=pt->link;
}
}



// concatenation of lists
struct node *concat(struct node *p, struct node *q)
{
struct node *x,*r;
if(p==NULL)
r=q;
if(q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}
 
1
 

So you really believe that the person who posted this problem is still waiting for an answer even after TWO years and wants to look at crappy looking code?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article