38
Linked Lists

Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Embed Size (px)

Citation preview

Page 1: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Linked Lists

Page 2: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

Page 3: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

The Linked List is an important data structurethat every computer scientist should understand.

Page 4: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Motivation

Suppose you wanted to write this application …

Page 5: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

To keep a grocery list like this you probably needa data structure that

* is easy to keep in “some” order (by aisle) * 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

Page 6: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

What’s Wrong With an Array?

Apples

Bread

Milk

Carrots

Hamburger

Page 7: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

Milk

Carrots

Hamburger

Add Chicken to the list

Chicken

What’s Wrong With an Array?

Page 8: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

Milk

Carrots

Hamburger

Chicken

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

What’s Wrong With an Array?

Page 9: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

Milk

Carrots

Hamburger

Chicken

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

What’s Wrong With an Array?

Page 10: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

MilkCarrotsHamburger

Corn

Remove milkfrom the list (your spouse bought someon the way home from work)

What’s Wrong With an Array?

Page 11: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

CarrotsHamburger

Corn

Remove milkfrom the list (your spouse bought someon the way home from work)

What’s Wrong With an Array?

Can’t have “empty”slots in an array

Page 12: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Apples

Bread

MilkCarrotsHamburger

CornAdd popcornto the list

An array is fixed in size. Once it is full we cannot add more.

What’s Wrong With an Array?

Page 13: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

We need a data structure ...

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

Page 14: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

The ideal data structure is a linked list.

Linked lists (and variations) are used inall kinds of computer science problems.

I good understanding of how a linked listworks is essential for every CS student.

Page 15: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

A linked list is an ideal candidate!

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

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 nullptr.

A linked list hasa “head” that pointsto the first node inthe list.

Page 16: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

null

1. Create a new node using the new operator

milk

2 gals

nullptr

newNode

nnnnnxxxxx

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

Page 17: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

newNode

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

xxxxx

nnnnnnnnnn

Page 18: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

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

newNode

xxxxx

nnnnn

xxxxx

Page 19: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

nnnnn

newNode

xxxxx

Page 20: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

1. Create another new node using the new operator

soda

12 cans

nullptr

newNode

Page 21: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

soda

12 cans

nullptr

newNode

2. Locate the node you want to insert after.

Page 22: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

soda

12 cans

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

nnnnn

newNode

Page 23: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

soda

12 cans

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

nnnnn

newNodexxxxxxxxxx

Page 24: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

milk

2 gals

soda

12 cans

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

nnnnn

newNodexxxxxxxxxx

Page 25: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Delete an item from the front of the grocery list!

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

nnnnn

xxxxx

Page 26: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Delete an item from the front of the grocery list!

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

nnnnn

oldHead

1. Save the address stored in head.

xxxxx

Page 27: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Delete an item from the front of the grocery list!

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

nnnnntemp

2. Get the pointer contained in the first node.

xxxxx

oldHead

Page 28: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Delete an item from the front of the grocery list!

head

rolls

24

butter

1 lb

eggs

1 dz

nullptr

nnnnntemp

3. Store it in head.

xxxxx

xxxxxxxxxx

oldHead

Page 29: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Delete an item from the front of the grocery list!

head

butter

1 lb

eggs

1 dz

nullptr

nnnnntemp

4. Delete the dynamically allocated node object.

rolls

24

xxxxx

xxxxxxxxxx

oldHead

Page 30: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Let’s Develop Some Code

Page 31: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

The Node Class

Node

- item: string- quantity: string

- next: Node*

what are thedata members?

what are theoperations?

+Node(:string, :int)+setQuantity(:string) :void+getQuantity( ) :string+setItem(:string) :void+getItem( ) :string+setNext(:Node*) :void+getNext( ) :Node*

Page 32: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

The Head Class

Head

- first: Node*what are thedata members?

what are theoperations?

+Head( )+push_front(:Node*) :void+pop_front( ): void

Page 33: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

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;}

Page 34: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

head

Delete an item from the front of the list

size

first rolls

24

item

quantity

next

node

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

milk

1 gal

item

quantity

next

node

Page 35: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

Lab One: Design Your Linked List

Project Five: Implement Your Linked List

Page 36: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

A string to contain a description of a grocery item, for example, "bread".

A string to contain a quantity for this item, for example "2 loaves".

A pointer to the next Node in the list. Initially this pointer should be set to nullptr.

Constructor, getters and setters

Your Node Class

Page 37: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

A Node* that points to the first node in the list. Initially this pointer should be set to nullptr.

A Node* that points to the last node in the list. Initially this pointer should be set to nullptr.

Your List Class

Page 38: Linked Lists. Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting

A constructor that creates an empty list object.

A push_back(Node*) function

A push_front(Node*) function

A pop_back( ) function

A pop_front( ) function

Your List Class