cs261 data structures linked lists - introduction

12
CS261 Data Structures Linked Lists - Introduction

Upload: betty-watts

Post on 18-Jan-2018

226 views

Category:

Documents


0 download

DESCRIPTION

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?

TRANSCRIPT

Page 1: CS261 Data Structures Linked Lists - Introduction

CS261 Data Structures

Linked Lists - Introduction

Page 2: CS261 Data Structures Linked Lists - Introduction

Dynamic Arrays Revisited

• Dynamic array can sometimes be slow– When?– Why?

Page 3: CS261 Data Structures Linked Lists - Introduction

Linked Lists to the Rescue

Alan Newell1956b

c

Dynamic Array in memory

dataSize = 3Capacity = 5

da

a

c

b

Linked Listin memory

headSize = 3

LL

a

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

Page 4: CS261 Data Structures Linked Lists - Introduction

• 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

datalink

datalink

…nextlink

nextlink

Linked Lists - Characteristics

Page 5: CS261 Data Structures Linked Lists - Introduction

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)

Page 6: CS261 Data Structures Linked Lists - Introduction

Linked List Variations

next

8

List

5 13

8

List

5 13

8

List

5 13

Page 7: CS261 Data Structures Linked Lists - Introduction

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????– Only access first element

Linked List Stack

8

firstLink

5 13

listStack

Page 8: CS261 Data Structures Linked Lists - Introduction

struct linkedListStack { struct Link *firstLink; /* Initialize routine sets to zero/NULL. */};

void linkedListStackInit (structlinkedListStack s) {s->firstLink = 0;

}

Linked List Stack

firstLinklistStack

Page 9: CS261 Data Structures Linked Lists - Introduction

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

Page 10: CS261 Data Structures Linked Lists - Introduction

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?

Page 11: CS261 Data Structures Linked Lists - Introduction

• 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

Page 12: CS261 Data Structures Linked Lists - Introduction

Worksheet 17

• pushLinkedListStack

• popLinkedListStack

Your Turn: