ok:
Code:
charNode *sort(charNode *start)
{
charNode *prev, *temp = NULL;
charNode *current = start;
while (current != NULL)
{
printf("%p %p %p %p\n", prev, current, temp, start);
if ( strcmp(current->petName, prev->petName) <0)
{
prev->next = current->next;
temp = prev;
prev = current;
}
else
prev = current;
current = current->next;
}
return start;
}
result:
003D2C90 003D2C60 00000000 003D2C60
003D2C60 003D2C78 00000000 003D2C60
003D2C78 003D2C90 003D2C60 003D2C60
Dog
Bat
and this is the complete code for the swap function earlier (i can't get it to work properly...the end node is missing??)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct charNode
{
char petName[12];
struct charNode *next;
};
typedef struct charNode charNode;
void addElement(charNode **start, char *petName);
void printList(charNode *start);
charNode* newNode(char *petName);
void getString(charNode *start);
void swap(charNode **a, charNode **b);
int main()
{
charNode *start = NULL;
getString(start);
return 0;
}
void getString(charNode *start)
{
addElement(&start, "Dog");
addElement(&start, "Androd");
addElement(&start, "Bat");
swap(&start, &start->next);
printList(start);
}
void addElement(charNode **start, char *petName)
{
charNode *temp,*prev, *loc;
int i;
temp = newNode(petName);
if (*start == NULL)
{
*start = temp;
}
else
{
loc = *start;
prev = NULL;
for (i=0; loc != NULL; i++)
{
prev = loc;
loc = loc->next;
}
if (prev == NULL)
{
*start = temp;
}
else
{
prev->next = temp;
}
}
}
charNode* newNode(char *petName)
{
charNode *temp;
temp = (charNode *) malloc(sizeof(charNode));
if (temp == NULL)
{
printf("WARNING - Memory allocation error\n");
exit(EXIT_FAILURE);
}
strcpy(temp->petName, petName);
temp->next = NULL;
return temp;
}
void printList(charNode *start)
{
while (start != NULL)
{
printf("%s\n", start->petName);
start = start->next;
}
}
void swap(charNode **a, charNode **b) {
charNode *temp;
temp = *a;
*a = *b;
*b = temp;
}
any idaes?