my full code as follows...
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 **x, charNode **y);
int main()
{
charNode *start = NULL;
getString(start);
return 0;
}
void getString(charNode *start)
{
addElement(&start, "(1)Cat");
addElement(&start, "(2)Dog");
addElement(&start, "(3)Horse");
swap(&start->next, &start);
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 **x, charNode **y)
{
charNode *temp;
temp = *x; // save x temporarly.
x = y; // swap y and x position
*y = temp; // place x after y
}