Linked Lists in C and C CS2303 System

  • Slides: 18
Download presentation
Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from

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) CS-2303, C-Term 2010 Linked Lists in C and C++ 1

Common Data Structures in C and C++ • Linked lists – D&D § 12.

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 CS-2303, C-Term 2010 Linked Lists in C and C++ 2

he t y l ual s). s u are t alway s t en

he t y l ual s). s u are t alway s t en ut no m e l e: e type (b t o N e sam 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 CS-2303, C-Term 2010 Linked Lists in C and C++ 3

Linked List e b y a dm. a o l ay mbers p :

Linked List e b y a dm. a o l ay mbers p : e Not iple me t mul struct list. Item { type payload; struct list. Item *next; }; payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 4

Linked List (continued) • Items of list are usually same type • Generally obtained

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) CS-2303, C-Term 2010 Linked Lists in C and C++ 5

Usage of Linked Lists • Not massive amounts of data • Linear search is

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” CS-2303, C-Term 2010 Linked Lists in C and C++ 6

Linked List (continued) struct list. Item { type payload; struct list. Item *next; };

Linked List (continued) struct list. Item { type payload; struct list. Item *next; }; struct list. Item *head; payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 7

Adding an Item to a List struct list. Item *p, *q; • Add an

Adding an Item to a List struct list. Item *p, *q; • Add an item pointed to by q after item pointed to by p – Neither p nor q is NULL payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 8

Adding an Item to a List list. Item *add. After(list. Item *p, list. Item

Adding an Item to a List list. Item *add. After(list. Item *p, list. Item *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 9

Adding an Item to a List list. Item *add. After(list. Item *p, list. Item

Adding an Item to a List list. Item *add. After(list. Item *p, list. Item *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 10

Adding an Item to. What a List Question: to do if we cannot guarantee

Adding an Item to. What a List Question: to do if we cannot guarantee that p and q are non-NULL? list. Item *add. After(list. Item *p, list. Item *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 11

nd lpa q Adding an Item to naon-List (continued) nul r e Not fo

nd lpa q Adding an Item to naon-List (continued) nul r e Not fo t s e t list. Item *add. After(list. Item *p, list. Item *q){ if (p && q) { q -> next = p -> next; p -> next = q; } return p; } payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 12

What about Adding an Item before another Item? struct list. Item *p; • Add

What about Adding an Item before another Item? struct list. Item *p; • Add an item before item pointed to by p (p != NULL) payload next payload next CS-2303, C-Term 2010 Linked Lists in C and C++ 13

What about Adding an Item before another Item? • Answer: – – Need to

What about Adding an Item before 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 CS-2303, C-Term 2010 Linked Lists in C and C++ 14

Doubly-Linked List struct list. Item { type payload; list. Item *prev; list. Item *next;

Doubly-Linked List struct list. Item { type payload; list. Item *prev; list. Item *next; }; struct list. Item *head, *tail; payload prev next to t w ho a lis – e: fter s i rc q a e x e tem s s a ew i l c In- a n add p item payload prev next CS-2303, C-Term 2010 Linked Lists in C and C++ 15 next

Other Kinds of List Structures • Queue — FIFO (First In, First Out) •

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 CS-2303, C-Term 2010 Linked Lists in C and C++ 16

Optional: – struct list. Item *head; Circular List struct list. Item *tail; payload next

Optional: – struct list. Item *head; Circular List struct list. Item *tail; payload next list. Item *add. After (list. Item *p, list. Item *tail){ if (p && tail) { p -> next = tail -> next; tail = p; } else if (p) { tail p -> next = p; } return tail; } CS-2303, C-Term 2010 Linked Lists in C and C++ 17

Questions? CS-2303, C-Term 2010 Linked Lists in C and C++ 18

Questions? CS-2303, C-Term 2010 Linked Lists in C and C++ 18