C Linked Lists Flashcards

(3 cards)

1
Q

Show me how to implement the add function of a linked list

A

// add at end of list
customer* add_customer(customer* list, int id, char* name){

// allocate space for the customer
customer* new_cust = malloc(sizeof(customer));

// assign the id
new_cust->id = id;

// assign the name and create space for the name
new_cust->name = malloc(strlen(name)+1);

// set its destination to NULL
new_cust->next = NULL;

// if the list is empty, create a new node and return that node
if(list==NULL){return(new_cust);}

// make a pointer to the list
customer* head = list;

// tranverse till the end
while(list->next != NULL){list=list->next;}

// glue in the new customer
list->next = new_cust;

// return the newly added linked list
return(head); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Header vs. C executable files

A

Header files hold functions and definitions of what they have access to while C executables are where the code is

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is deleting a node different?

A

You need two pointers, one to search for the node to be deleted and a pointer o its previous node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly