Code:
#include <stdio.h>
#include <stdlib.h>
/* Small program to explore using singly linked list. */
struct charRecord
{
char letter;
struct charRecord *next;
};
typedef struct charRecord charNode;
void copyArrayToList(charNode *start, char array[], int size);
void printList(charNode *start);
void freeList(charNode **start);
charNode* newNode(char letter);
void getString(char array[], int size);
int main()
{
char array[10];
charNode *start = NULL;
getString(array, 10);
copyArrayToList(&start, array, 10); // I compiled with gcc, and this is wrong according to gcc, i reckon you don't need the & sign
printList(start);
freeList(&start);
return 0;
}
void getString(char array[], int size)
{
printf("Please enter %d characters\n", size-1);
fgets(array, size, stdin);
}
void copyArrayToList(charNode *start, char array[], int size)
{
/* Create a new linked list and copy the contents of
array into it. Make sure the list is in order
*/
int i;
charNode *temp, *prev, *loc;
for (i=0; i<size; i++)
{
temp = newNode(array[i]);
if (*start = NULL) // maybe change it to start == NULL?
{
/* add 1st item to list */
*start = *temp; // here you say this
}
else
{
/* find correct location for item */
loc = *start;
prev = NULL;
while (loc != NULL && loc->letter < array[i])
{
prev = loc; // different?
loc = loc->next;
}
/* insert item into list just before loc */
temp->next = loc;
if (prev == NULL)
{
*start = *temp;
}
else
{
prev->next = temp;
}
}
}
}
try correcting those