linked list. p the every usage of the term list refers to a linear collection of data item. e.g....

43
Linked List

Upload: aaron-williamson

Post on 26-Mar-2015

234 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Linked ListLinked List

Page 2: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

The every usage of the term “list” refers to a linear collection of data item. e.g. Shopping List.

A Shopping List contains a first element, a second element……and a last element.

Frequently, we want to add items to or delete items from a list

IntroductionIntroduction

Page 3: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

A Shopping ListA Shopping List

MilkEggsButterTomatoesApplesOrangesBread

MilkEggsButterTomatoesApplesOrangesBread

MilkEggsButterTomatoesApplesOrangesBreadChickenCornLettuce

MilkEggsButterTomatoesApplesOrangesBreadChickenCornLettuce

Page 4: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

A linked lists, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers.

Each node is divided into 2 parts:

1. the first part contains the information of the element, and

2. the second part, called the link field or nextpointer field, contains the address of the next node in the list.

Linked ListsLinked Lists

Page 5: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Linked listsLinked lists

A A linked listlinked list consists of a consists of a sequence of sequence of nodesnodes connected by connected by linkslinks, , plus a plus a headerheader..

Each node (except the last) has a Each node (except the last) has a successorsuccessor,, and each node and each node (except the first) has a (except the first) has a predecessorpredecessor..

Each node contains a single Each node contains a single elementelement (object or value), plus links (object or value), plus links to its successor and/or predecessor.to its successor and/or predecessor.

ant bat cat

header null linknode element link

Page 6: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Linked lists (2)Linked lists (2)

The The lengthlength of a linked list is the of a linked list is the number of nodesnumber of nodes.. An An emptyempty linked list has linked list has no nodes.no nodes. In a linked list:In a linked list:

We can manipulate the individual elements.We can manipulate the individual elements. We can manipulate the links, thus changing the linked We can manipulate the links, thus changing the linked

list’s very structure! (This is impossible in an array.)list’s very structure! (This is impossible in an array.)

Page 7: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Singly-linked lists (1)Singly-linked lists (1)

A A singly-linked listsingly-linked list ( (SLLSLL) consists of a sequence of nodes, ) consists of a sequence of nodes, connected by links in one direction only.connected by links in one direction only.

Each SLL node contains a single element, plus a link to the Each SLL node contains a single element, plus a link to the node’s successor (or a null link if the node has no successor).node’s successor (or a null link if the node has no successor).

An SLL header contains a link to the SLL’s first node (or a null An SLL header contains a link to the SLL’s first node (or a null link if the SLL is empty).link if the SLL is empty).

pig dog ratcat

dog

Page 8: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

ant bat catfirst

Example: SLL traversal

Instance method (in class Instance method (in class SLLSLL) to traverse an SLL:) to traverse an SLL:publicpublic voidvoid printFirstToLast () { printFirstToLast () {// // Print all elements in this SLL, in first-to-last order.Print all elements in this SLL, in first-to-last order.

forfor (SLLNode curr = (SLLNode curr = thisthis.first;.first;curr != curr != nullnull; curr = curr.succ); curr = curr.succ)

System.out.println(curr.element);System.out.println(curr.element);}}

Animation:Animation:

ant bat catfirst

curr

ant bat catfirst

curr

ant bat catfirst

curr

ant bat catfirst

curr

Page 9: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Bed Bed NumberNumber

PatientPatient

11 KirkKirk

22

33 DeanDean

44 MaxwellMaxwell

55 AdamsAdams

66

77 LaneLane

88 GreenGreen

99 SamuelsSamuels

1010

1111 FieldsFields

1212 NelsonsNelsons

NextNext

77

1111

1212

33

44

11

00

88

99

A hospital ward contains 12 A hospital ward contains 12 beds, of which 9 are occupied. beds, of which 9 are occupied. Suppose we want an Suppose we want an alphabetical listing of the alphabetical listing of the patients. patients.

Questions

55STARTSTART

Page 10: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Bed Bed NumberNumber

PatientPatient

11 KirkKirk

22

33 DeanDean

44 MaxwellMaxwell

55 AdamsAdams

66

77 LaneLane

88 GreenGreen

99 SamuelsSamuels

1010

1111 FieldsFields

1212 NelsonsNelsons

NextNext

77

1111

1212

33

44

11

00

88

99

Answer!

PatientPatient BedBed

AdamAdam 55

DeanDean 33

FieldsFields 1111

GreenGreen 88

KirkKirk 11

LaneLane 77

MaxwellMaxwell 44

NelsonNelson 1212

SamuelsSamuels 99

Page 11: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

INFOINFO

11

22

33 OO

44 TT

55

66 SPACESPACE

77 XX

88

99 NN

1010 II

1111 EE

1212

LINKLINK

66

00

1111

1010

33

44

77

What is the actual word based on What is the actual word based on the list?the list?

Questions

99STARTSTART

Page 12: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Answer!

START = 9, so INFO[9]=NSTART = 9, so INFO[9]=N

LINK[9]=3, so INFO[3]=OLINK[9]=3, so INFO[3]=O

LINK[3]=6, so INFO[6]=SPACELINK[3]=6, so INFO[6]=SPACE

LINK[6]=11, so INFO[11]=ELINK[6]=11, so INFO[11]=E

LINK[11]=7, so INFO[7]=XLINK[11]=7, so INFO[7]=X

LINK[7]=10, so INFO[10]=ILINK[7]=10, so INFO[10]=I

LINK[10]=4, so INFO[4]=TLINK[10]=4, so INFO[4]=T

LINK[4]=0 – a NULL ValueLINK[4]=0 – a NULL Value

INFOINFO

11

22

33 OO

44 TT

55

66 SPACESPACE

77 XX

88

99 NN

1010 II

1111 EE

1212

LINKLINK

66

00

1111

1010

33

44

7799STARTSTART

Page 13: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

TESTTEST

11 7474

22 8282

33 8484

44 7878

55 7474

66 100100

77

88 8888

99 6262

1010 7474

1111 9393

1212

LINKLINK

1111

00

99

00

6 6

1010

11

55

44

33

What is the score of both What is the score of both SCIENCE & ART?SCIENCE & ART?

Questions

33SCIENCESCIENCE

88ARTART

Page 14: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

TESTTEST

11 7474

22 8282

33 8484

44 7878

55 7474

66 100100

77

88 8888

99 6262

1010 7474

1111 9393

1212

LINKLINK

1111

00

99

00

6 6

1010

11

55

44

22

SCIENCESCIENCE

84, 62, 74, 100, 74 7884, 62, 74, 100, 74 78

ARTART

88, 74, 93, 8288, 74, 93, 82

Answer:

33SCIENCESCIENCE

88ARTART

Page 15: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

CustomerCustomer

11 VitoVito

22 HunterHunter

33 KatzKatz

44 EvansEvans

55 RogersRogers

66 TellersTellers

77 JonesJones

88 GrantGrant

99 McBrideMcBride

1010 WestonWeston

1111 ScottScott

1212 AdamsAdams

LINKLINK

33

99

00

00

1010

77

1212

1111

44

00

11

55

Suppose a brokerage firm has 4 Suppose a brokerage firm has 4 broker and each broker broker and each broker

Questions

BrokerBroker PointerPointer

11 BondBond 1212

22 KellyKelly 33

33 HallHall 00

44 NelsonNelson 99

Page 16: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

CustomerCustomer

11 VitoVito

22 HunterHunter

33 KatzKatz

44 EvansEvans

55 RogersRogers

66 TellersTellers

77 JonesJones

88 GrantGrant

99 McBrideMcBride

1010 WestonWeston

1111 ScottScott

1212 AdamsAdams

LINKLINK

33

99

00

00

1010

77

1212

1111

44

00

11

55

1.1. Bond’s list of Customer would Bond’s list of Customer would be:be:Grant, Scott, Vito, KatzGrant, Scott, Vito, Katz

2.2. Kelly’s list of customer would be: Kelly’s list of customer would be: Hunter, McBridge, EvansHunter, McBridge, Evans

3.3. Nelson’s list of customer would be: Nelson’s list of customer would be: Tellers, Jones, Adams, Rogers, Tellers, Jones, Adams, Rogers, WestonWeston

Answer

BrokerBroker PointerPointer

11 BondBond 88

22 KellyKelly 22

33 HallHall 00

44 NelsonNelson 66

Page 17: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

NameName SSNSSN SEXSEX SALARYSALARY

11 DAVISDAVIS 192-38-3782192-38-3782 FemaleFemale 22,80022,800

22 KELLYKELLY 165-64-3351165-64-3351 MaleMale 19,00019,000

33 GREENGREEN 175-56-2251175-56-2251 MaleMale 27,20027,200

44 BROWNBROWN 178-52-1065178-52-1065 FemaleFemale 14,70014,700

55 LEWISLEWIS 181-58-9939181-58-9939 FemaleFemale 16,40016,400

66 COHENCOHEN 177-44-4557177-44-4557 MaleMale 19,00019,000

77 RUBINRUBIN 135-46-6262135-46-6262 FemaleFemale 15,50015,500

88 EVANEVAN 168-56-8113168-56-8113 MaleMale 34,20034,200

99 HARRISHARRIS 208-56-1654208-56-1654 FemaleFemale 22,80022,800

1010

1111

1212

LINKLINK

88

55

99

66

77

22

00

44

33

Question

44StartStart

Page 18: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Each node in the linked list is a class, as shown here.

data

link

10

data

link

15

data

link

7

null

Declarations for Linked ListsDeclarations for Linked Lists

Page 19: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

data

link

7

The data portion of each node is an int.

link

null

public class IntNode{ private int data; ...}

data

link

15

Declarations for Linked ListsDeclarations for Linked Lists

data10

Page 20: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Each IntNode also contains a link which refers to another IntNode.

data15

data7

public class IntNode{ private int data; private IntNode link; ...}

Declarations for Linked ListsDeclarations for Linked Lists

data10

link

link

null

link

Page 21: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Declarations for Linked ListsDeclarations for Linked Lists

A program can keep track of the front node by using a variable such as head in this example.

Notice that head is not an IntNode -- it is a reference to an IntNode. data

link

10

data

link

15

data

link

7

nullhead

Page 22: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Declarations for Linked ListsDeclarations for Linked Lists

A program can keep track of the front node by using an IntNode reference variable such as head.

Notice that head is not an IntNode -- it is a reference to an IntNode.

We represent the empty list by storing null in the head reference.

head

null

Page 23: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

We want to add a new entry, 13, We want to add a new entry, 13, to the to the frontfront of the linked list of the linked list shown here.shown here.

10

15

7

nullhead

Page 24: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

Create a new node...

10

15

77

nullhead

Page 25: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

Create a new node... Place the data in the new

node's data field.

10

15

7

nullhead

13

Page 26: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new

node's data field.... Connect the new node to the

front of the list.

Page 27: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new node's

data field.... Connect the new node to the

front of the list. Make the head refer to the new

head of the linked list.

Page 28: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new node's

data field.... Connect the new node to the

front of the list. Make the head refer to the new

head of the linked list.

Page 29: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

IntNodes are often inserted at places other than the front of a linked list.

There is a general pseudocode that you can follow for any insertion function. . .

Page 30: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

head = new IntNode(newEntry, head);

Page 31: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

Page 32: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

In this example, thenew node will bethe second node

In this example, thenew node will bethe second node

previous

Page 33: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

What is the name of this link?

Look at the linkwhich is in the node

previous

Look at the linkwhich is in the node

previous

previous

Page 34: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

This link is calledprevious.link

This link is calledprevious.link

What is the name of What is the name of this link ?this link ?

previous

Page 35: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first):Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

previous.linkrefers to the headof a small linked

list, with 10 and 7

previous.linkrefers to the headof a small linked

list, with 10 and 7

previous

Page 36: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

The new node mustbe inserted at thefront of this small

linked list.

The new node mustbe inserted at thefront of this small

linked list.

13

Write one Java statement which will do the insertion.

previous

Page 37: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.13

Write one Java statement which will do the insertion.

previousprevious.link = new IntNode(newEntry, previous.link);

Page 38: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Pseudocode for Removing IntNodesPseudocode for Removing IntNodes

IntNodes often need to be removed from a linked list.

As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere.

We’ll look at the technique for removing a node from the front of a linked list.

Page 39: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Removing the Head IntNodeRemoving the Head IntNode

10 15

7

nullhead

13

Draw the change that this statement will make to the linked list.

Page 40: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Removing the Head IntNodeRemoving the Head IntNode

10 15

7

nullhead

13

Page 41: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Removing the Head IntNodeRemoving the Head IntNode

10 15

7

nullhead

13

Page 42: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Removing the Head IntNodeRemoving the Head IntNode

Here’s what the linked list looks like after the removal finishes.

10 15

7

nullhead

Page 43: Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

It is easy to insert or remove a node at the front of a list.

You also need a technique for inserting or removing a node elsewhere

Summary Summary