CS261 Data Structures Linked Lists - Introduction
Dynamic Arrays Revisited • Dynamic array can sometimes be slow – When? – Why?
Linked Lists to the Rescue Dynamic Array Linked List in memory in memory da LL Alan Newell data head a a 1956 Size = 3 b Size = 3 c Capacity = 5 c b What can we now do…and not do …quickly?
Linked Lists - Characteristics • Data elements held in structures called “ links ” • Like a chain: each link is tied to the next link link … data data next next link link • Links are 1 – 1 with elements, allocated and released as necessary
Typical Link Structure (Singly Linked) struct Link { /* Single link. */ TYPE val; /* Data contained by this link. */ struct Link *next; /* Pointer to next link. */ }; next val
Linked List Variations All linked lists consists of links … but there are other design decisions: – Header (special value to point to start) or no header? – Use null as terminator, or special value (sentinel) for end? – Use single or double links? – Pointer to first element only, or pointer to first and last? backSent List frontSent prev prev prev prev prev … Link Link next next next next next
Linked List Stack Implementing a stack interface with a linked list: – Header with head reference only: null if empty – Null terminated – Singly linked – Where should the ‘top’ of the stack be???? • Answer: First element is easy to access List Stack firstLink val: 2 val: 7 val: 4 next: next: next: null
Linked List Stack struct linkedListStack { struct Link *firstLink; /* Initialize routine sets to zero/ NULL . */ }; void linkedListStackInit (structlinkedListStack s) { s->firstLink = 0; } List Stack firstLink
Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). */ } val:? firstLink next:? List Stack val: 2 val: 4 next: next: null
Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). 2. Set data fields in the new link. 3. Change head to point to new link. */ } val:20 firstLink next: List Stack val: 2 val: 4 next: next: null
Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). 2. Set data fields in the new link. 3. Change head to point to new link. */ } List Stack firstLink val:20 val: 2 val: 4 next: next: next: null
Linked List Tips… • Draw the diagram! • Go through the steps visually, labeling each step • Convert each step to C code • Try the boundary cases: – Empty list? – List with several items?
Other Linked List Operations • How do you tell if stack is empty? • How do you return first element (i.e., firstLink )? • How do you remove an element?
Your Turn • Complete Worksheet 17: Linked List Introduction, List Stack
Linked List Stack void popListStack(struct ListStack *s, TYPE d) { struct Link *first; assert(s->firstLink); first = s->firstLink; s->firstLink = first->next; free(first); struct ListStack { } struct Link *firstLink; } struct Link { TYPE val; struct Link *next; List Stack }; firstLink val:20 val: 2 val: 4 next: next: next: null
Recommend
More recommend