LINKED LISTS Introduction to Systems Programming - COMP 1002, 1402
Outline Linked list Adding and removing data to a link list Linked list search
Common Data Structures in C and C++ 3 Linked lists One-way Doubly-linked Circular Trees Binary Multiple branches Hash Table Combine arrays and linked list Especially for searching for objects by value
Arrays Properties Elements are consecutive in memory Easy to determine location in memory Access a single element quickly But CANNOT change size declare new space Copy all elements (at times) May not correspond to amount of data
Alternative Properties Easily grows to an arbitrary size Correspond to amount of data Less rigid Possible alternative are lists Singly linked list Doubly linked list Trees (binary trees, 2-3 trees, finger trees, B-trees )
Lists Lists can be viewed as chains Each piece of information is a link Links are independent of each other Links are somehow connected to each other Properties Elements are in arbitrary location Must view all predecessors to find one Grows to arbitrary size Massive re-orderings possible quickly
Definitions 7 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
Processing a List Lists can be viewed as chains Begin at the beginning of the list (chain) firstelement is the first link second element is the second link thirdelement is the third link Could be arbitrary length
Singly Linked Lists
What is in a NODE? Each element (NODE) in a list contains: Data a pointer to another NODE How will we define the NODE type?
Single Link per Element
Linked List struct listitem { type ; struct listitem *; }; 12
Linked List (continued) 13 Items of list are usually same type Generally obtained from malloc() Each item points to 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
Usage of Linked Lists 14 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 List (continued) struct listitem { type ; struct listitem *; }; struct listitem *head; 15
Maintaining a List Access the list Keep a pointer to the first NODE (for now) NODE * plist; /* plist is a pointr to first node */ or NODE *head; /* head is a pointer to first node */ All functions modifying a list Must have access to the list (Take plist as a parameter) e.g.: NODE *insertnode(node *plist...);
Insert a Node 1. Allocate memory 2. Locate predecessor (A pointer, ppre) 3. Point new node to its successor 4. Point predecessor to new node
Meanings of ppre
Insert into Empty List
Insert Node at Beginning
Insert Node in Middle
Adding an Item to a List struct listitem *p, *q; Add an item pointed to by q after item pointed to by p Neither p nor q is NULL 22
Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 23
Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 24
Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } Question: What to do if we cannot guarantee that p and q are non-null? 25
Adding an Item to a List (continued) listitem *addafter(listitem *p, listitem *q){ if (p && q) { q -> = p -> ; p -> = q; } return p; } 26
What about Adding an Item before another Item? struct listitem *p; Add an item before item pointed to by p (p!= NULL) 27
What about Adding an Item before another Item? 28 Answer: Need to search list from beginning to find previous item Add new item after previous item This is needed Insert item after earlier event times and before later ones
Insert Node at End
Deleting a Node Still requires the predecessor s location! Remove the node Free the memory space!
Delete General Case
Searching Through a List Search should return: If a match exists A pointer to the node containing the key If there is no match NULL A pointer to the largest element that is smaller or equal to the key (for a sorted list)
Traverse Linked Lists printlist, averagelist,
searchlist int searchlist (NODE *plist, NODE **ppre, NODE **pcur, KEY_TYPE target) {... }
Inserting a Node NODE * insertnode(node *plist, NODE *ppre, DATA data) {... }
Deleting a Node NODE * deletenode(node *plist, NODE *ppre) {... }
Doubly-Linked List struct listitem { type ; listitem *prev; listitem *; }; struct listitem *head, *tail; prev prev prev prev 37
Other Kinds of List Structures 38 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
Circular List Optional: struct listitem *head; struct listitem *tail; listitem *addafter (listitem *p, listitem *tail){ if (p && tail) { p -> = tail -> ; tail = p; } else if (p) { tail p -> = p; } return tail; } 39
QUESTIONS? 40