Help with pointers and linked lists
Hello,
I'm doing my university assignment which is about radix sort for strings using linked lists. Actually, I'm kind of new with pointers and linked lists.
Each string has a maximum size of 15. In each iteration the index of character in the string moves backwards until zero. But I think there's a problem in the code that has to do with NULL pointers. Here's part of the code where the problem exists:
Code:
typedef struct Node
{
char word[MAXSIZ];
struct Node *next;
}Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;
<some code>
//This is part of the Sort function
// arrays A and B are of type List and they have the header nodes added to them earlier in the code
int k;
int index =15;
for(k=1;k<=index;k++)
{
for (i=0; i<number_of_alpha;i++) // number _of_alpha is 53
{
p=A[i]->next; //p points to the first node under A[i]
while(p!=NULL)
{
A[i]->next=p->next; // Link the header node with the node that is after the previous node
if (strlen(p->word) < (index-k))
{
add(B[0],p);
}
else
{
if ((p->word[index-k] >= 'A') && (p->word[index-k] <= 'Z'))
add(B[(p->word[index-k] - 64)],p);
else if ((p->word[index-k] >= 'a') && (p->word[index-k] <= 'z'))
add(B[(p->word[index-k] -70)],p);
}
p=A[i]->next; //Move p to point to the next node(which is now linked to the header node)
}
}
if(++k > index)
break;
for (j=0;j<number_of_alpha;j++)
{
p=B[j]->next;
while (p!= NULL)
{
B[j]->next=p->next;
if (strlen(p->word) < (index-k) ) // If strlen is less than 15 add to the first linked list in the array (under A[0])
{
add(A[0],p);
}
else
{
if ((p->word[index-k] >= 'A') && (p->word[index-k] <= 'Z'))
add(A[(int)(p->word[index-k] - 64)],p);
else if ((p->word[index-k] >= 'a') && (p->word[index-k] <= 'z'))
add(A[(int)(p->word[index-k] -70)],p);
}
p=B[j]->next;
}
}
<some code>
// This function adds each node to the beginning of the linked list
void add(List L, Position p) // L points to header , p points to the Node
{
p->next = L->next;
L->next = p;
}
If one of the strings I enter is "Jane" then the array that represent this string must be filled to only sub4(including '\0'), so it's length is less than 15. After a number of iterations, (index-k) will equal 3. This is where it begins to check the first letter from the right. But eveytime it comes to this stage somehow the pointer p loses the data or points to NULL. And ends up giving me two empty arrays A and B without the original data.
Is there anything that I missed in the code?
Any help would be appreciated.