structures) connected by links (pointer to next node) A pointer points to the first node Next pointer refers to next node Last node has a NULL value for next pointer Can insert and delete anywhere in the list
list of words (strings) Inserts words in the list in alphabetical order Deletes words from the list See example code at: linkedlist.c % make –f makefile-list % ./program
a Node and a NodePointer “class” to make our code easier to read We can use these in place of “struct node” and “struct node*” typedef struct node Node; typedef struct node* NodePointer;
user to input a word to insert and display while(0 != strcmp(word, QUIT)){ printf(“Enter a word:”); getline(word, MAX); if(0 != strcmp(word, QUIT)){ insertIntoLinkedList(word, &head); displayLinkedList(head); } }
environment of functions Bottom of stack segment contains heap storing data using malloc() and free() Data Segment Code Segment runtime stack heap Stack Segment Stack Segment
function call creates a local environment (return address, arguments, local variables, return value) Function call: push stack frame Function exit: pop stack frame Grows from top of stack segment downwards
of the string (character array) to the function definition Parameter &head will send the address of pointer head (NOT the address stored in pointer head), so that the function can change the address stored in pointer head insertIntoLinkedList(word, &head);
Parameter *head2 is a pointer to a pointer, as it contains the address of the head of the linked list Alternative code: struct node **head2 void insertIntoLinkedList(char word2[], NodePointer *head2){
Pointer newNode points to the node to be added to the list Pointer previous points to the previous node in list Pointer current pointers to the current node in list NodePointer newNode = NULL; NodePointer previous = NULL; NodePointer current = *head;
returns the size in bytes of a Node, which is struct node Function malloc() creates space on the heap, and returns this memory address If the heap is out of space, malloc() returns NULL newNode = malloc(sizeof(Node));
with functions malloc() and free() At bottom of stack segment and grows upwards When the runtime stack and the heap meet, your program is out of memory, which may occur if you don’t use function free() to deallocate memory
memory on the heap at execution time (while program is running) Create space for new nodes by using function malloc() to allocate memory Release space no longer needed by using function free() to deallocate memory Use #include <stdlib.h> header file for these functions
to insert the new node Keep looping to the end of list, or where word2 is less than or equal to current->data while(NULL != current && strcmp(word2, current->data)>0){ previous = current; current = current->next; }
the linked list, then either we are inserting into an empty linked list, or we are inserting at the beginning of a linked list if(NULL == previous){ newNode->next = current; *head2 = newNode; }
loop through the linked list, then either we are inserting into the middle of the linked list, or we are inserting at the end of a linked list else{ previous->next = newNode; newNode->next = current; }
of the linked list as a parameter We are not changing what the head points to, so we do NOT need to pass the address of head displayLinkedList(head);
user for a word to delete and display list while(0!=strcmp(word, QUIT)&&NULL!=head){ printf(“Enter a word: ”); getline(word, MAX); if(0 != strcmp(word, QUIT)){ deleteFromLinkedList(word, &head); displayLinkedList(head); } }
which will send the address of pointer head to *head3, so that the function can change the address stored in pointer head deleteFromLinkedList(word, &head); //code . . . void deleteFromLinkedList(char word3[], NodePointer *head3){
Pointer tempNode points to the node to be deleted from list Pointer previous points to the previous node in list Pointer current pointers to the current node in list NodePointer tempNode = NULL; NodePointer previous = NULL; NodePointer current = *head;
we cannot delete from it, so we display an error message and quit the function if(NULL == current){ printf(“Cannot delete from empty list!\n”); return; }
beginning of the linked list, then we need to point the head to the second node, and delete the first node if(0 == strcmp(word3, current->data)){ tempNode = current; *head3 = current->next; free(tempNode); }
of the list, we need to loop through the linked list and locate the node to delete while(NULL!=current && 0 != strcmp(word3, current->data)){ previous = current; current = current->next; }
the end of the list, we skip over the node by pointing the previous node’s next to the node after the current node, then delete the node if(NULL != current){ tempNode = current; previous->next = current->next; free(tempNode); }