CS261 Data Structures Linked Lists - Introduction Dynamic Arrays - - PowerPoint PPT Presentation

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

CS261 Data Structures Linked Lists - Introduction Dynamic Arrays - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS261 Data Structures

Linked Lists - Introduction

slide-2
SLIDE 2

Dynamic Arrays Revisited

  • Dynamic array can sometimes be slow

– When? – Why?

slide-3
SLIDE 3

Linked Lists to the Rescue

Alan Newell 1956 b c Dynamic Array in memory data Size = 3 Capacity = 5 da a c b Linked List in memory head Size = 3 LL

a

What can we now do…and not do …quickly?

slide-4
SLIDE 4
  • Data elements held in structures called “links”
  • Like a chain: each link is tied to the next
  • Links are 1 – 1 with elements, allocated and

released as necessary

data link data link … next link next link

Linked Lists - Characteristics

slide-5
SLIDE 5

struct Link { /* Single link. */ TYPE val; /* Data contained by this link. */ struct Link *next; /* Pointer to next link. */ };

val next

Typical Link Structure (Singly Linked)

slide-6
SLIDE 6

All linked lists consists of links … but there are

  • ther 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?

Linked List Variations

Link … List Link

next

backSent frontSent

next next next prev prev prev prev next prev

slide-7
SLIDE 7

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 val: 2 next: val: 7 next: val: 4 next: null

Linked List Stack

firstLink

slide-8
SLIDE 8

struct linkedListStack { struct Link *firstLink; /* Initialize routine sets to zero/NULL. */ }; void linkedListStackInit (structlinkedListStack s) { s->firstLink = 0; }

Linked List Stack

List Stack

firstLink

slide-9
SLIDE 9

void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this:

  • 1. Allocate (malloc) a new link (check that it works!).

*/ }

Linked List Stack

List Stack val: 2 next: val: 4 next: null

firstLink

val:? next:?

slide-10
SLIDE 10

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. */

}

Linked List Stack

List Stack val: 2 next: val: 4 next: null

firstLink

val:20 next:

slide-11
SLIDE 11

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. */

}

Linked List Stack

List Stack val:20 next: val: 2 next: val: 4 next: null

firstLink

slide-12
SLIDE 12

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?

slide-13
SLIDE 13
  • How do you tell if stack is empty?
  • How do you return first element (i.e.,

firstLink)?

  • How do you remove an element?

Other Linked List Operations

slide-14
SLIDE 14
  • Complete Worksheet 17: Linked List

Introduction, List Stack

Your Turn

slide-15
SLIDE 15

void popListStack(struct ListStack *s, TYPE d) { struct Link *first; assert(s->firstLink); first = s->firstLink; s->firstLink = first->next; free(first); }

Linked List Stack

List Stack val:20 next: val: 2 next: val: 4 next: null

firstLink

struct ListStack { struct Link *firstLink; } struct Link { TYPE val; struct Link *next; };