linkedLists Flashcards
How do you create the Node for linkedlist?
struct Node {
struct Node *next;
int val;
};
How do you initialize the linkedlist?
struct Node * first = NULL;
How do you code a function to add an item to the linked list?
How would you code searching a linked list
struct node * search_list(struct node *list, int key){
while(list !=NULL && list->val != n)
list = list->next;
return list;
}
How would you code deleting a node from a linked list?
struct node * delete_from_list(struct node * list, int n){
struct node *cur, prev;
for(cur = list, prev = NULL; curr != NULL && cur->value != n;prev=curr, curr = curr->next){
if(curr == NULL)
return list;
if(prev == NULL)
list = list->next;
else
prev->next = curr->next;
free(cur);
return list;
}
How do you free the memory from an entire linked list?
void *free_list(struct node *list)
{
struct node *next;
while(list != NULL){
next = list->next;
free(list);
list = next;
}
}