1 / 35

Linked Lists...

Linked Lists. An introduction to creating dynamic data structures. Post-conditions. To extend understanding of pointers by using them to create dynamic data structures Understand when to use a Linked List Be able to create a Linked List in C understand internals be able to program them!.

weylin
Download Presentation

Linked Lists...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists... An introduction to creating dynamic data structures

  2. Post-conditions • To extend understanding of pointers by using them to create dynamic data structures • Understand when to use a Linked List • Be able to create a Linked List in C • understand internals • be able to program them!

  3. Outline • Linked Lists introduction • Characteristics of Linked Lists • Programming Linked Lists

  4. Linked Lists definition • Abstract common use of “list” • TODO: • Read task description • Design test data • Create makefile • Collect other files needed • Begin working on code • Type in first version • Test it • …

  5. Linked Lists and pointers • The word “list” suggests an ordered collection • The word “linked” hints at pointers to where the next element in the list is located • This is different from arrays, which use contiguous memory locations • Linked lists may use multiple links to link elements according to different interpretations of “order”. • We will only consider “singly linked lists” for now.

  6. Linked Lists memory diagrams • A list of the numbers [0, 12, -4, 22] • An array of the numbers [0, 12, -4, 22] • Where are arrays stored? • Global/static/extern/const? • Stack? • Heap? • Where are linked lists stored?

  7. Linked Lists: Internals • The dynamic nature of the linked list data structure means we must allocate and free memory on demand • Some languages do this for you. • Not so for C - you have to do it explicitly using malloc/calloc/realloc/free.

  8. Pointer Linked Lists: Internals Here is a high-level schematic of a linked list. A pointer enables us to reference its 1st element. Element 0 Element 1 Element 2

  9. Pointer Things to consider in test design Three distinctive parts of the list - what are they? Element 0 Element 1 Element 2

  10. Data Link Linked Lists: Internals Each element in our singly-linked list has two components: • the data component • anything you want... • the link component • a pointer, of course!

  11. Data 0 Data 1 Data 2 Pointer Pointer Pointer Linked Lists: Internals We now expose the internals of the previous schematic. Q: How might a Linked List be terminated? Pointer A: By our “old friend”, the NULL pointer... Element 0 Element 1 Element 2 NULL

  12. Element 0 Data Data 0 Data 1 Pointer Pointer Pointer Linked Lists: Internals Adding an element to the front of the list. Pointer Element 1 Element 2 NULL

  13. Element 0 Data 0 Data 2 Data 1 Pointer Pointer Pointer Linked Lists: Internals Adding an element to the front of the list. Pointer Element 1 Element 2 NULL

  14. Element 0 Data 0 Data 2 Data 1 Pointer Pointer Pointer Linked Lists: Internals New links are forged by simple assignment operations. Q: What happens if we change the order? Pointer A: Ooops! ? Element 1 Element 2 NULL

  15. Linked Lists: Internals Using “boxes and arrows” to show what we are doing with pointers is very helpful… But it is easy to lose sight of one simple thing: One pointer can only point to one thing! Every time we “draw an arrow” we effectively erase any existing arrow coming from that box. In the previous example we “lost” the linked list because we neglected this!

  16. Element 1 Data Data 0 Data 1 Pointer Pointer Pointer Linked Lists: Internals Adding an element elsewhere. Pointer Element 0 Element 2 NULL

  17. Data 0 Data 2 Pointer Pointer Linked Lists: Internals Adding an element elsewhere. Pointer Element 1 Data 1 Element 0 Element 2 Pointer NULL

  18. Characteristics of Linked Lists What is the worst-case situation for altering the n-th element of: • an array? • a linked list? Altering an element anywhere in the array has the same cost. Arrays elements are referred to by their address offset and have O(1) cost. Altering an element to the end of the linked list is more costly as the list has to be traversed. The cost is therefore O(n).

  19. Characteristics of Linked Lists Advantages of linked lists: • Dynamic nature of the data structure • Flexible structure • singly linked lists • doubly linked • circular lists • etc., etc.

  20. Characteristics of Linked Lists Disadvantages of linked lists: • Linear worst-case cost of access • The absence of a Linked List implementation in standard C libraries

  21. Programming Linked Lists The dynamic nature of the data structure means we must allocate and free memory • malloc() • to allocate memory when we add an element • free() • to de-allocate memory when we remove an element

  22. ”Hello” char * List * Programming Linked Lists The link element we will use for the example: typedef struct List List; struct List { char *content; List *next; } Could be anything, of course!! Note the self-referential nature of the pointer; it points to one of these structures.

  23. Programming Linked Lists Adding an element to the front of the list. List *addToFront(List *listp, List *newp) { newp->next = listp; return newp; } Draw memory diagrams for: empty list; list with one element; list with three elements.

  24. List *newp char * char * char * List * List * NULL List *addToFront(List *listp, List *newp) { newp->next = listp; return newp; } Return this pointer List *listp

  25. Programming Linked Lists Adding an element to the end of the list. List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; /* null statement */ p->next = newp; return listp; }

  26. Programming Linked Lists Draw pictures of case of empty list *listp List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; /* null statement */ p->next = newp; return listp; }

  27. List *newp char * NULL List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; p->next = newp; return listp; } Return this pointer NULL

  28. Programming Linked Lists Draw pictures of case of list *listp containing 2 elements List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; /* null statement */ p->next = newp; return listp; }

  29. List *newp char * char * char * NULL List * NULL List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; p->next = newp; return listp; } List *p List *listp List *

  30. Programming Linked Lists De-allocating a complete list void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content); /* the string */ free(listp); } }

  31. Programming Linked Lists Draw the memory diagram for a 5 element list. void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content); /* the string */ free(listp); } }

  32. char * char * char * NULL List * List * void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content); free(listp); } } List *p NULL List *listp NULL

  33. Programming Linked Lists List * deleteFirst(List *listp) { List *p; if (listp != NULL) { p = listp; listp = listp->next; free(p); } return listp; } Write a function that deletes the first element in a list.

  34. Programming Linked Lists Write a function that counts the number of elements in a list. int count(List *listp) { int cnt = 0; for ( ; listp != NULL; cnt++) listp = listp->next; return cnt; }

  35. Post-conditions • To extend understanding of pointers by using them to create dynamic data structures • Understand when to use a Linked List • Be able to create a Linked List in C • understand internals • be able to program them!

More Related