Here’s my code so far:
struct stockRecord {
int code;
char name[MAXNAMELEN];
struct stockRecord* next;
};
struct stockRecord* temp = NULL;
struct stockRecord* head = NULL;
struct stockRecord* prevptr = NULL;
struct stockRecord* resfun(struct stockRecord* list)
{
temp = list;
if (head == NULL) head = list;
if (temp == NULL) {
return head;
} else {
if (prevptr == NULL) { //first node
if (strstr(temp->name, "ABC-") != NULL) {
temp = temp->next; //remove the current node
}
}
prevptr = list;
if (temp->next == NULL) {
return head;
} else {
return resfun(temp);
}
}
}
I don’t know how to remove a node, and re-link the neighbour nodes. Then I’ll need to return the head node to the main function.
Please can anyone help?
Thanks.
Vega,
To remove the first element from a single-linked-list, all you really need to do is “forget” the first element (the head).
The general procedure for removing the first node is:
(which they almost allways are in a
linked-list) then free the memory
allocated to this node.
The general procedure for removing a node in the middle of a list is:
The general procedure for removing the last node is (I bet you can guess):
Recursion has nothing to do with it.
Good luck with it. BTW, linked lists are STILL “stock in trade” for ansi-c programmers. I still work with them far too often 😉
Cheers. Keith.