31
Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Embed Size (px)

DESCRIPTION

Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting into the list * Removing from the list * Iterating through the list Write code that implements a singly linked list copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Citation preview

Page 1: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Linked Listsbased on the original work of Dr. Roger deBry

Version 1.0

Page 2: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

ObjectivesAt the conclusion of this lesson, students should be able to:

Describe the basic operation and structure of a linked list * Inserting into the list * Removing from the list * Iterating through the list

Write code that implements a singly linked list

2copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 3: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Consider a shopping list that you want to keep onyour computer. To keep such a list you probably wanta data structure that

* is easy to keep in “some” order * is easy to insert items into the middle of * is easy to delete items from the middle of * is easy to iterate through * grows and shrinks as items are added and deleted

Motivation

3copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 4: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburger

4copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 5: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburger

Add Chicken to the list

Chicken

5copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 6: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburger

Chicken

But I really want Chicken by Hamburger (both in the meat dept)

6copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 7: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburgerChicken

If there is a lot of stuff in the array, moving elementsto free up a space in the middle is expensive!

7copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 8: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilkCarrotsHamburgerCorn

Remove milkfrom the list

8copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 9: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBread

CarrotsHamburgerCorn

Can’t have “empty”slots in an array

9copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 10: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

What’s Wrong With an Array?ApplesBreadMilkCarrotsHamburgerCorn

Add popcornto the list

An array is fixed in size. Once it is full we cannot add more.10copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 11: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

We need a data structure ...

That can grow and shrink as neededThat is not in contiguous memoryThat has fast insertion/deletion in the middle

11copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 12: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

A linked list is an ideal candidate!

headrolls

24

butter

1 lb

eggs

1 dz

null

this is called a node. Each node in thelist contains some data and a pointerto the next node. Nodes are dynamicallyallocated as new items are added tothe list.

Note that the pointer in thelast node in the list is set to null.

12copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 13: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item to the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

1. Create a new node using the new operator

milk2 galsnull

newNode

nnnnnxxxxx

Node* newNode = new Node(“milk”, “2 gals”);

13copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 14: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item to the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

newNode

2. copy the address stored in head into the pointer in the new node.

xxxxx

nnnnnnnnnn

14copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 15: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item to the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

3. Store the address of the new node in the head.

newNodexxxxx

nnnnn

xxxxx

15copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 16: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item to the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

3. Now we can throw away this pointer. Be careful, don’t write delete newNode;

nnnnn

newNodexxxxx

16copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 17: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item in the middle of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

1. Create another new node using the new operator

soda12 cans

null

newNode

17copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 18: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item in the middle of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

soda12 cans

null

newNode

2. Locate the node you want to insert after.

18copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 19: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item in the middle of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

soda12 cans

3. Store the pointer from this node in the new node.

nnnnn

newNode

19copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 20: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Adding a new item in the middle of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

milk2 gals

soda12 cans

4. Store the address of the new node in this node.

nnnnn

newNodexxxxxxxxxx

20copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 21: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Delete an item from the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

nnnnn

xxxxx

21copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 22: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Delete an item from the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

nnnnntemp

1. Get the pointer contained in the first node.

xxxxx

22copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 23: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Delete an item from the front of the grocery list!

headrolls24

butter1 lb

eggs1 dznull

nnnnntemp

2. Store it in head.

xxxxx

xxxxxxxxxx

23copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 24: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Delete an item from the front of the grocery list!

head

butter1 lb

eggs1 dznull

nnnnntemp

3. Delete the dynamically allocated node object.

rolls24

xxxxx

xxxxxxxxxx

24copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 25: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Let’s Develop Some Code

25copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 26: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

The Node Class

Node

- item: string- quantity: int- next: Node*

what are thedata members?

what are theoperations?

Node(:string, :int)setQuantity(:string) :voidgetQuantity( ) :stringsetItem(:string) :voidgetItem( ) :stringsetNext(:Node*) :voidgetNext( ) :Node*

26copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 27: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

The Head Class

Head

- size: int- first: Node*

what are thedata members?

what are theoperations?

Head( )push_front(:Node*) :voiddelete_front(:Node*)insertNode(:Node*, Node*): void

27copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 28: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

head

Add a new item to the front of the list

size

first rolls

24

item

quantity

next

node

void Head::push_front ( Node* n ){ n->setNext(first); first = n;}

28copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 29: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

head

Delete an item from the front of the list

size

first rolls

24

item

quantity

next

node

void Head::delete_front ( ){ Node* byeByeNode = first; first = first->getNext( ); delete byeByeNode;}

milk

1 gal

item

quantity

next

node

29copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 30: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

head

Insert an item into the middle of the list

size

first rolls

24

item

quantity

next

node

void Head::insertNode ( Node* here, Node* newNode ){ Node* tempPtr = here->getNext( ); newNode->setNext(tempPtr); here->setNext(newNode);}

milk

1 gal

item

quantity

next

node

30copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Page 31: Linked Lists based on the original work of Dr. Roger deBry Version 1.0

Lab Three: Design Your Linked List

Lab Three: Implement Your Linked List

31copyright (C) 2008 by Dennis A. Fairclough all rights reserved.