Hmm, let me see if I understand what you're doing. Is it taking a single linked list and splitting it into an array of linked lists based on a certain criteria?
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *push_front ( struct node *head, int data )
{
struct node *nn = malloc ( sizeof *nn );
if ( nn != NULL ) {
nn->data = data;
nn->next = head;
}
return nn;
}
int main ( void )
{
struct node *head = NULL;
struct node *a[3] = {0};
struct node *last;
struct node *next;
struct node *it;
int i;
for ( i = 0; i < 10; i++ )
head = push_front ( head, i );
for ( it = head; it != NULL; it = it->next )
printf ( "%d ", it->data );
printf ( "\n" );
/* Let's start building the array of lists */
while ( head != NULL ) {
int index = 0;
next = head->next;
if ( head->data >= 0 && head->data < 3 )
index = 0;
else if ( head->data >= 3 && head->data < 7 )
index = 1;
else
index = 2;
head->next = a[index];
a[index] = head;
head = next;
}
for ( i = 0; i < 3; i++ ) {
printf ( "a[%d]: ", i );
for ( it = a[i]; it != NULL; it = it->next )
printf ( "%d ", it->data );
printf ( "\n" );
}
return 0;
} Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401