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

Preview:

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

Linked Listsbased 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.

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.

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburger

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

What’s Wrong With an Array?ApplesBreadMilk

CarrotsHamburger

Add Chicken to the list

Chicken

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

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.

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.

What’s Wrong With an Array?ApplesBreadMilkCarrotsHamburgerCorn

Remove milkfrom the list

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Let’s Develop Some Code

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

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.

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.

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.

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.

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.

Lab Three: Design Your Linked List

Lab Three: Implement Your Linked List

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

Recommended