I am having trouble with my insert to the front of the linked list fucntion in C
#define arrSIZE = 100;
struct listNode {
char data[arrSIZE];
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
void insertHead(ListNode *sPtr, char value[arrSIZE]){
ListNode *newPtr = (ListNode *)malloc(sizeof(ListNode));
strncpy(newPtr->data, value, arrSIZE);
if(sPtr ==NULL){
newPtr->nextPtr=NULL;
sPtr = newPtr;
}else{
newPtr->nextPtr=sPtr;
sPtr =newPtr;
}
}
I can see why. -You're setting sPtr, but sPtr is a local variable and is gone as soon as you exit insertHead. Instead, you would do this:
... Thus you now have a single linked list.
On the other hand, if you want to have more than one linked list, you'll need to add another '*' on the argument: