-
Inserting in linked list
I'm trying to make a function that lets me pass an index and a string and will insert the string at that index in the linkedlist... any help would be appreciated
Code:
typedef struct node {
char* value;
struct node* next;
} LinkedList;
void llAddAtIndex(LinkedList** ll, char* value, int index) {
LinkedList* newNode =(LinkedList*)malloc(sizeof(LinkedList));
newNode->value = value;
LinkedList* prevNode = NULL;
LinkedList p;
p = ?????
for(int i = 0; i < index; i++){
prevNode = p;
p = p->next;
}
if (prevNode) {
prevNode->next = newNode;
newNode->next = p;
} else {
head = newNode;
newNode->next = p;
}
}
-
Double-posting your question doesn't make answers come any quicker.
-
-
-
-
Do you have a question?
You have a variable called prevNode. So what is p for?
Where should prevNode point to start iterating through the list?
-
Quote:
Originally Posted by
johngoodman
I oppose you
How To Ask Questions The Smart Way
Try to imagine a board where everyone behaved like you do, posted every question twice - and then someone upped the ante by posting 3 times.
How quickly do you think the board would become a spam-fest and all the people who help would bugger off and find something better to do with their time.
Oh, and this is what happens to duplicate threads - CLOSED!