1 / 18

Linked Lists in C and C++

Linked Lists in C and C++. CS-2303 System Programming Concepts (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Common Data Structures in C and C++.

saxon
Download Presentation

Linked Lists in C and C++

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 in C and C++ CS-2303System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Linked Lists in C and C++

  2. Common Data Structures in C and C++ • Linked lists – D&D §12.4–12.6 (not in K&R) • One-way • Doubly-linked • Circular • Trees – D&D §12.7, K&R §6.5 • Binary • Multiple branches • Hash Tables – K&R §6.6 (not in D&D) • Combine arrays and linked list • Especially for searching for objects by value Linked Lists in C and C++

  3. Note: elements are usually thesame type (but not always). Definitions • Linked List • A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship • Singly- or doubly-linked • Stack, queue, circular list • Tree • A data structure in which each element is dynamically allocated and in which each element has more than one potential successor • Defines a partial order Linked Lists in C and C++

  4. Note: payload may bemultiple members. payload payload payload payload next next next next Linked List struct listItem {type payload;struct listItem *next; }; Linked Lists in C and C++

  5. Linked List (continued) • Items of list are usually same type • Generally obtained from malloc() • Each item points to next item • Last item points to null • Need “head” to point to first item! • “Payload” of item may be almost anything • A single member or multiple members • Any type of object whose size is known at compile time • Including struct, union, char* or other pointers • Also arrays of fixed size at compile time (see p. 214) Linked Lists in C and C++

  6. Usage of Linked Lists • Not massive amounts of data • Linear search is okay • Sorting not necessary • or sometimes not possible • Need to add and delete data “on the fly” • Even from middle of list • Items often need to be added to or deleted from the “ends” Linked Lists in C and C++

  7. payload payload payload payload next next next next Linked List (continued) struct listItem {type payload;struct listItem *next; }; struct listItem *head; Linked Lists in C and C++

  8. payload payload payload payload payload next next next next next Adding an Item to a List struct listItem *p, *q; • Add an item pointed to by qafter item pointed to by p • Neither p nor q is NULL Linked Lists in C and C++

  9. payload payload payload payload payload next next next next next Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p; } Linked Lists in C and C++

  10. payload payload payload payload payload next next next next next Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p; } Linked Lists in C and C++

  11. payload payload payload payload payload next next next next next Adding an Item to a List Question: What to do if we cannotguarantee that p and q are non-NULL? listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p; } Linked Lists in C and C++

  12. Note test for non-null p and q payload payload payload payload payload next next next next next Adding an Item to a List (continued) listItem *addAfter(listItem *p, listItem *q){if (p && q) { q -> next = p -> next; p -> next = q;}return p; } Linked Lists in C and C++

  13. payload payload payload payload payload next next next next next What about Adding an Itembefore another Item? struct listItem *p; • Add an item before item pointed to by p (p != NULL) Linked Lists in C and C++

  14. What about Adding an Itembefore another Item? • Answer:– • Need to search list from beginning to find previous item • Add new item after previous item • This is needed in PA#3 • Insert item after earlier event times and before later ones • Need to search the list Linked Lists in C and C++

  15. payload payload payload payload prev prev prev prev next next next next In-class exercise:– how to add a new item q after a list item p Doubly-Linked List struct listItem {type payload;listItem *prev;listItem *next; }; struct listItem *head, *tail; Linked Lists in C and C++

  16. Other Kinds of List Structures • Queue — FIFO (First In, First Out) • Items added at end • Items removed from beginning • Stack — LIFO (Last In, First Out) • Items added at beginning, removed from beginning • Circular list • Last item points to first item • Head may point to first or last item • Items added to end, removed from beginning Linked Lists in C and C++

  17. payload payload payload payload next next next next Optional:– struct listItem *head; Circular List struct listItem *tail; listItem *addAfter (listItem *p, listItem *tail){if (p && tail) { p -> next = tail -> next; tail = p;} else if (p) { tail p -> next = p;} return tail; } Linked Lists in C and C++

  18. Questions? Linked Lists in C and C++

More Related