Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-19-datastructures

 ics212-19-datastructures

Avatar for William Albritton

William Albritton

November 03, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Linked Lists  Nodes  Insert into

    a Linked List  Display a Linked List  Delete from a Linked List
  2. What is a Linked List?  Collection of nodes (self-referential

    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
  3. Linked List Example Program  A program that stores a

    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
  4. Node “Self-Referencial” Structure  Each node structure stores one word

    (data), which is a string, and a pointer to the next node (next) #define MAX 20 struct node{ char data[MAX]; struct node *next; };
  5. Node and NodePointer typedef  We use typedef to make

    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;
  6. Function main()  Variable word is a string to store

    each word that the user inputs  Variable head is a pointer to the first node in the linked list char word[MAX]={‘\0’}; NodePointer head = NULL;
  7. While Loop to Insert into Linked List  Keep asking

    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); } }
  8. Program in Memory 1. Data segment contains global data 2.

    Code segment contains the program code 3. Stack segment contains runtime stack and heap Data Segment Code Segment Stack Segment
  9.  Top of stack segment contains runtime stack storing local

    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
  10. Runtime Stack  Implicit (automatic) allocation and deallocation  Each

    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
  11. Function Call insertIntoLinkedList()  Parameter word will send the address

    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);
  12. Function Definition insertIntoLinkedList()  Parameter word2[] points to the string

     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){
  13. New Node, Previous, Current  Declare and initialize three pointers

     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;
  14. Runtime Stack  Declare and initialize three pointers word head

    head2 word2 newNode previous current "A" NULL NULL NULL NULL
  15. Create a New Node on the Heap  Operator sizeof()

    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));
  16. Heap  Memory explicitly allocated and deallocated by the programmer

    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
  17. Dynamic Memory Allocation  Ability of a program to use

    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
  18. Runtime Stack and Heap  Use malloc() to allocate a

    new node to the heap word head head2 word2 newNode previous current "A" NULL NULL NULL data next ?? ??
  19. Error Checking and String Copy  If-statement to make sure

    there is room on the heap  Copy the user’s word to the new node on the heap if(NULL != newNode){ strcpy(newNode->data, word2); //. . . }
  20. Runtime Stack and Heap  Copy string "A" from word

    to newNode->data word head head2 word2 newNode previous current "A" NULL NULL NULL data next ?? ?? "A"
  21. Loop through Linked List  Need to figure out where

    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; }
  22. Insert at Beginning  If we did not loop through

    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; }
  23. Runtime Stack and Heap  Points head to newNode and

    next becomes NULL word head head2 word2 newNode previous current "A" NULL NULL data next "A" ?? NULL
  24. Insert in Middle or at End  If we did

    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; }
  25. Runtime Stack and Heap  After function call to insert

    "A" into linked list word head "A" data next "A" NULL
  26. Runtime Stack and Heap  After function call to insert

    "C" into linked list word head “C" data next “A" data next “C" NULL "A" "C"
  27. Runtime Stack and Heap  After function call to insert

    "B" into linked list word head “B" data next “A" data next “C" NULL "B" data next “B"
  28. Function Call displayLinkedList()  All we need is the head

    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);
  29. Function Definition displayLinkedList()  If the current node is NULL,

    the linked list is empty, so we display “empty list”, and quit the function if(NULL == current){ printf(“The linked list is empty!\n\n”); return; }
  30. Function Definition displayLinkedList()  Otherwise, we loop through the linked

    list  The last node in a linked list will have a NULL value while(NULL != current){ printf(“%s, ”, current->data); current = current->next; }
  31. Runtime Stack and Heap  Pointer current will point to

    each node in linked list head current data next “A" data next “C" NULL data next “B"
  32. Runtime Stack and Heap  Pointer current will point to

    each node in linked list head current data next “A" data next “C" NULL data next “B"
  33. Runtime Stack and Heap  Pointer current will point to

    each node in linked list head current data next “A" data next “C" NULL data next “B"
  34. Runtime Stack and Heap  The loop ends when pointer

    current becomes NULL head current NULL data next “A" data next “C" NULL data next “B"
  35. While Loop to Delete from Linked List  Keep asking

    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); } }
  36. Function Call and Definition  Again, we have parameter &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){
  37. Runtime Stack and Heap  User enters string "B" to

    delete from linked list word head head3 word3 “B" data next “A" data next “C" NULL data next “B"
  38. Temporary Node, Previous, Current  Declare and initialize three pointers

     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;
  39. Runtime Stack and Heap  User enters string "B" to

    delete from linked list word head head2 word2 tempNode previous current "B" NULL NULL data next “A" data next “C" NULL data next “B"
  40. Error Checking for Empty List  If list is empty,

    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; }
  41. Delete First Node  If the word is at the

    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); }
  42. Loop through Linked List  If not at the head

    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; }
  43. Runtime Stack and Heap  Locating a node to delete

    from linked list word head head2 word2 tempNode previous current "B" NULL NULL data next “A" data next “C" NULL data next "B"
  44. Runtime Stack and Heap  Locating a node to delete

    from linked list word head head2 word2 tempNode previous current "B" NULL data next “A" data next “C" NULL data next "B"
  45. Loop through Linked List  If we are not at

    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); }
  46. Runtime Stack and Heap  Use free() to delete a

    node and free up memory on the heap word head head2 word2 tempNode previous current "B" NULL data next “A" data next “C" NULL data next “B"
  47. Runtime Stack and Heap  Use free() to delete a

    node and free up memory on the heap word head head2 word2 tempNode previous current "B" data next “A" data next “C" NULL data next “B"
  48. Memory Management  Linked Lists  Nodes  Insert into

    a Linked List  Display a Linked List  Delete from a Linked List