-
Array List Insertion
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... here is so function i have so far, but am confused on whether or not im headed in the right direction...any help would be great!
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;
}
}
-
Take a piece of paper and a pencil. Draw the list and try to accomplish the same thing that you want your function to do. Figure out what you did on the paper and then do them in the list too ;)
-
Starting three threads for the same thing, will only achieve us wanting to say things to you that would probably see us banned for saying them.
So, do us a favour and imagine we just said such things, then either comform to the rules or find help elsewhere.
The amount of help you get is inversely proportional to the amount that you erm... 'annoy' people, to put it politely.
-
Some help:
struct node *insertleft(struct node *lst, int index, struct node *np);
This way you avoid unnecessary typedefs, you don't have to handle allocations in a function that needn't handle them, the call is more natural since you just pass the value of the list rather than a pointer to it, and the name of the function is more specific (it inserts np to the left of the index'th element in lst) and it doesNotUseThisAnnoyingFormat.
-
1 Attachment(s)
The engines cannae tek it captn
Attachment 12524
Three new threads in three hours - one subject.
Oh this isn't going to end well for someone if this keeps going...
Closed (again).