Monday, May 24, 2010

Consider a linked list with n integers. Each node of the list is numbered?

Develop a program using ‘C’ language to split this list into 4 lists so that:





 first list contains nodes numbered 1,5, 9, 13- - -


 second list contains nodes numbered 2, 6, 10, 14- - -


 third list contains nodes numbered 3, 7, 11, 15- - -


 and fourth list contains nodes numbered 4,8, 12, 16- - -

Consider a linked list with n integers. Each node of the list is numbered?
You are using the c language so all you got to store the list is an array. Since its a linked list we shall assume the array has to be read in order from element 0 to element n (we can also start from element 1 if we a=want).





Logic : simply use a for loop (this is the best option to read data in order) and put each element in its place in the four diff arrays


as we go.





Method : source array : link, %26lt;size is n%26gt;


Dest link1,link2,link3,link4





Basic code : it works assuming data is stored from location 0 on the array.





int x=1,y=0;


for(int i=0;i%26lt;n;i++)


{


if (x==1) link1[y]=link[i];


else if(x==2) link2[y]=link[i];


else if(x==3) link3[y]=link[i];


else if(x==4) link4[y]=link[i];


x=x+1;


if (x==4)


{ x=0;y=y+1;}


}


No comments:

Post a Comment