hey everyone,
im just trying to add a new node to the end of this list and print the list out...can someone tell me what i am doing wrong..thanks alot
Code:#include <stdio.h> #include <stdlib.h> struct listNode { char data; struct listNode *nextPtr; }; typedef struct listNode LISTNODE; typedef LISTNODE *LISTNODEPTR; /*prototypes*/ void insert(LISTNODEPTR *, char); void printList(LISTNODEPTR); int main(void) { LISTNODEPTR startPtr = NULL; char nodedata; printf("\nEnter the letter you would like to add to the list\n"); while(scanf("%d",&nodedata)) { printf("\nSorry thats not a letter or string\n"); } insert(&startPtr, nodedata); printList(startPtr); } void insert(LISTNODEPTR *sPtr, char value) { LISTNODEPTR newPtr; LISTNODEPTR previousPtr; LISTNODEPTR currentPtr; if((newPtr = malloc(sizeof(LISTNODE))) == NULL) { printf("NOT ENOUGH MEMORY TO CREATE A NEW NODE OF %d SIZE", sizeof(LISTNODE)); exit(1); } newPtr->data = value; newPtr->nextPtr = NULL; previousPtr = NULL; currentPtr = *sPtr; while(currentPtr != NULL) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if(previousPtr == NULL) { *sPtr->nextPtr = newPtr; } else { previousPtr->nextPtr = newPtr; } } void printList(LISTNODEPTR currentPtr) { if(currentPtr == NULL) { printf("List is empty"); exit(1); } else{ printf("\nList Contains:"); while(currentPtr != NULL) { printf("%s -->", currentPtr->data); currentPtr = currentPtr->nextPtr; } } }