75
Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

  • View
    221

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Introduction to Computer Science

• Linked Lists

• Basic Link Operations

Unit 17Unit 17

Page 2: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 2

An Example that Motivates Pointers—a List of Items in Alphabetical Order

class DataItem {String data;int next;…

}

DataItem[ ] list = new DataItem[20];int first, last, current;

first = 0, last = 5, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

1

"FOB"next

data

2

"GOB"next

data

3

"HOB"next

data

4

"JOB"next

data

5

"MOB"next

data

6

"NOB"next

data

undefinednext

data

undefined

undefined undefined

Page 3: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 3

What happens when new items arrive?

• Currently, the physical and logical order are the same, so the “next” field doesn’t do much

• But as new items arrive, the identity of physical and logical order might break down

If "SOB" arrived, it would go here, but what if "LOB" arrived?

first = 0, last = 5, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

1

"FOB"next

data

2

"GOB"next

data

3

"HOB"next

data

4

"JOB"next

data

5

"MOB"next

data

6

"NOB"next

data

undefinednext

data

undefined

undefined undefined

Page 4: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 4

The next Field Keeps Things Ordered

• Items don’t need to be moved to keep the list alphabetically ordered; instead, the “next” field tells us what comes next—even when the items are jumbled:

first = 3, last = 1, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

5

"HOB"next

data

6

"NOB"next

data

0

"GOB"next

data

2

"FOB"next

data

1

"MOB"next

data

4

"JOB"next

data

undefinednext

data

undefined

undefined undefined

Page 5: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Three Independent Cases:How are Items Inserted?

New word Changes Inserts…"SOB" list[6].data = "SOB"; new highest word

list[6].next = 7;current++;last = 6;

"BOB" list[6].data = "BOB"; new lowest word

list[6].next = 3;current++;first = 6;list[1].next = 7;

first = 3, last = 1, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

5

"HOB"next

data

6

"NOB"next

data

0

"GOB"next

data

2

"FOB"next

data

1

"MOB"next

data

4

"JOB"next

data

undefinednext

data

undefined

undefined undefined

Page 6: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Three Independent Cases:How are Items Inserted?

New word Changes Inserts…"SOB" list[current].data = "SOB"; new highest word

list[current].next = current+1;last = current;current++;

"BOB" list[current].data = "BOB"; new lowest word

list[current].next = first;first = current; current++;list[last].next = current;

first = 3, last = 1, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

5

"HOB"next

data

6

"NOB"next

data

0

"GOB"next

data

2

"FOB"next

data

1

"MOB"next

data

4

"JOB"next

data

undefinednext

data

undefined

undefined undefined

Page 7: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Final Case:Item Inserted in Middle

New word Changes Inserts…"LOB" list[6].data = "LOB"; word in middle

list[6].next = 4;list[5].next = 6;list[1].next = 7;current++;

first = 3, last = 1, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

5

"HOB"next

data

6

"NOB"next

data

0

"GOB"next

data

2

"FOB"next

data

1

"MOB"next

data

4

"JOB"next

data

undefinednext

data

undefined

undefined undefined

Page 8: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

Final Case:Item Inserted in Middle

New word Changes Inserts…"LOB" list[current].data = "LOB"; word in middle

list[current].next = 4;list[5].next = current;list[last].next = current + 1;current++;

first = 3, last = 1, current = 6

[0] [1] [2] [3] [4] [5] [6] [7]

next

data

5

"HOB"next

data

6

"NOB"next

data

0

"GOB"next

data

2

"FOB"next

data

1

"MOB"next

data

4

"JOB"next

data

undefinednext

data

undefined

undefined undefined

Page 9: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 9

There’s only one problem…

• We now have a linked list of components, with the physical and logical order independent

• But what if words keep arriving? The array had to be declared at compile time, and its size is fixed. Eventually, we’ll run out of room. Declaring a very large array is wasteful, and even then won’t always work

Page 10: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 10

The Solution: Object Addresses

• We can dynamically allocate objects, creating them one at a time, as we need them, while the program is running

• An object can contain (a reference to) another object, i.e., pointing at it, the way we've seen object variables point to objects in the heap

• The object variable represents the ‘subscript’, in computer memory, of a dynamically allocated object

• This ‘subscript’ is called an address

Page 11: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 11

What’s All This I Hear About “Pointers”?

Time lunchtime = new Time(12, 0),

dinnertime = new Time(0, 0);

lunchtime

lunchtimeAttributes: _hour = 12 _minute = 0Methods: …

dinnertime

dinnertimeAttributes: _hour = 0 _minute = 0Methods: …

Page 12: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 12

Object Variable Contains Object’s Address in the Heap

• A object variable represents the address, in computer memory, of an object.

dinnertime

dinnertimeAttributes: _hour = 0 _minute = 0Methods: …

heap

Page 13: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 13

Object Variable Contains Object’s Address in the Heap

dinnertime

dinnertimeAttributes: _hour = 0 _minute = 0Methods: …

32476

32476heap

• A object variable represents the address, in computer memory, of an object.

Page 14: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 14

Dynamically Allocated Objects with Pointers to other Objects

class DataObject {int _myNumber;DataObject _next;…

}Objects of class DataObject can contain variables of type DataObject.The variable "next" is really just a reference to an object of type DataObject; it can also be the reference null (which marks the end of the linked list).

Page 15: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 15

class DataObject {int _myNumber;DataObject _next;…

}

Linked List

firstAttributes: _myNumber = 17 _next = Methods: …

secondAttributes: _myNumber = 42 _next = nullMethods: …

heap

Page 16: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 16

class DataObject {int _myNumber;DataObject _next;…

}

Linked List

32476

firstAttributes: _myNumber = 17 _next = 32476Methods: …

secondAttributes: _myNumber = 42 _next = nullMethods: …

29371 heap

Page 17: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 17

Set aside storage for an address, and create the object (putting its address in that storage)

Time lunchtime = new Time(12, 0),

dinnertime = new Time(0, 0);

lunchtime

lunchtimeAttributes: _hour = 12 _minute = 0Methods: …

dinnertime

dinnertimeAttributes: _hour = 0 _minute = 0Methods: …

I write the names here to help you identify the objects; in reality, the objects in the heap have no names attached

Page 18: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 18

A Clarification

I have used an object name (like "dinnertime" or "lunchtime") to refer both to the reference and to the

object itself. There's a reason for this…

lunchtime

lunchtimeAttributes:

_hour = 12_minute = 0

Methods:…

dinnertime

dinnertimeAttributes:

_hour = 0_minute = 0

Methods:…

Page 19: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 19

Sometimes we are speaking of attributes of the object itself

dinnertime._hour = lunchtime._hour;means that the _hour attribute inside the dinnertime object gets the value of the _hour attribute inside the lunchtime object:

lunchtime

lunchtimeAttributes:

_hour = 12_minute = 0

Methods:…

dinnertime

dinnertimeAttributes:

_hour = 12_minute = 0

Methods:…

Page 20: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 20

But sometimes we are referring to the address of the object

dinnertime = lunchtime;

means the contents of the lunchtime reference (an address) get put into the dinnertime reference location:

lunchtime

lunchtimeAttributes:

_hour = 12_minute = 0

Methods:…

dinnertime

dinnertimeAttributes:

_hour = 0_minute = 0

Methods:…

Page 21: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 21

The node has room for an address

What's inside the linked list node is a storage location for an address, just like the storage location "dinnertime" or "lunchtime":

firstAttributes: _myNumber = 17 _next = Methods: …

secondAttributes: _myNumber = 42 _next = nullMethods: …

heap

currentfirst second

Page 22: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 22

The node has room for an address

We can make an assignment from "first._next" into "current" and it means the contents of one (the address) go into the other:

firstAttributes: _myNumber = 17 _next = Methods: …

secondAttributes: _myNumber = 42 _next = nullMethods: …

heap

current

current = first._next;

first second

Page 23: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 23

Linked List Example (1)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: nullObject first: ---Address of object current: nullObject current: ---

currentfirst

null null

Page 24: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 24

Linked List Example (2)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: (null, null)Address of object current: nullObject current: ---

currentfirst

null

Attributes: _letter = null _next = nullMethods: …

Page 25: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 25

Linked List Example (3)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: (null, null)Address of object current: 748921Object current: (null, null)

currentfirst

Attributes: _letter = null _next = nullMethods: …

Page 26: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 26

Linked List Example (4)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: ('A', null)Address of object current: 748921Object current: ('A', null)

currentfirst

Attributes: _letter = 'A' _next = nullMethods: …

Page 27: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 27

Linked List Example (5)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: ('A', 647361)Address of object current: 748921Object current: ('A', 647361)

currentfirst

Attributes: _letter = 'A' _next = Methods: …

Attributes: _letter = null _next = nullMethods: …

Page 28: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 28

Linked List Example (6)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: ('A', 647361)Address of object current: 647361Object current: (null, null)

currentfirst

Attributes: _letter = 'A' _next = Methods: …

Attributes: _letter = null _next = nullMethods: …

Page 29: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 29

Linked List Example (7)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: ('A', 647361)Address of object current: 647361Object current: ('B', null)

currentfirst

Attributes: _letter = 'A' _next = Methods: …

Attributes: _letter = 'B' _next = nullMethods: …

Page 30: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 30

Linked List Example (8)

class Node {char _letter;Node _next;

…}

…Node first, current;

first = new Node( );current = first;first._letter = ‘A’;first._next = new Node( );current = current._next;current._letter = ‘B’;current._next = null;…

Address of object first: 748921Object first: ('A', 647361)Address of object current: 647361Object current: ('B', null)

currentfirst

Attributes: _letter = 'A' _next = Methods: …

Attributes: _letter = 'B' _next = nullMethods: …

Page 31: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 31

Linked List (9)

647361

firstAttributes: _letter = A _next = 647361Methods: …

currentAttributes: _letter = B _next = nullMethods: …

748921 heap

class Node {char _letter;Node _next;

…}

Address of object first: 748921Object first: (‘A’, 647361)Address of object current: 647361Object current: (‘B’, null)

Page 32: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 32

Basic Link Operations

• The individual objects in a linked list are called a cell or a node

• The reference to the next cell is called a link• The first cell in a list is called the head of the list• The remainder of the list is called its tail

next

data

null

0

firstNode

next

data

>>>>

0

firstNode

next

data0

next

data

null

0

>>>>

I'll often leave out the "box" picture for the object variable (e.g., "firstNode") from now

on

Page 33: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 33

The definition underlying the basic cell

class Node {int _data;Node _next;…

}

Node firstNode = new Node( );firstNode._next = new Node( );firstNode._next._next = new Node( );firstNode._next._next._next = new Node( );firstNode._next._next._next._next = null;

_next

_data

>>>>

0

firstNode_next

_data0

_next

_data0

>>>>_next

_data

null

0

>>>>

Every referenceshould havean address orbe null.

Page 34: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 34

What do the names refer to?

firstNode represents an address of an objectfirstNode._data one attribute of the object at that addressfirstNode._next represents an address of an objectfirstNode._next._data one attribute of the object at that address

class Node {int _data;Node _next;…

}

_next

_data

>>>>

0

firstNode_next

_data0

_next

_data0

>>>>_next

_data

null

0

>>>>

Page 35: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 35

Let's Make it More Encapsulated

class ListNode {private int _value;private ListNode _tail;

public ListNode (int v, ListNode next) {_value = v; _tail = next;

}

public int getValue ( ) {return _value;}

public ListNode getTail ( ) {return _tail;}}

Page 36: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 36

Build a Linked List

_tail

_value

>>>>

17

cell1 _tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

ListNode cell4 = new ListNode(-14, null);

ListNode cell3 = new ListNode(616, cell4);

ListNode cell2 = new ListNode(10945, cell3);

ListNode cell1 = new ListNode(17, cell2);

cell4cell3cell2

Why did we build the linked list in reverse order?

Page 37: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 37

We Don't Need Separate Variables for each Node

_tail

_value

>>>>

17

list_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

ListNode list = new ListNode(-14, null);

list = new ListNode(616, list);

list = new ListNode(10945, list);

list = new ListNode(17, list);

With each line, list points to one node further to the left

Page 38: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 38

You Can Even Embedthe Calls to new

list

ListNode list = new ListNode(17,

new ListNode(10945,

new ListNode(616,

new ListNode(-14, null))));

Notice that the order of the nodes now matches their final order --- but the

rightmost is still created first

_tail

_value

>>>>

17_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

Page 39: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 39

Questions

What is the effect of (independently):a) list = list.getTail( );

_tail

_value

>>>>

17_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>list

Page 40: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 40

Questions

What is the effect of (independently):a) list = list.getTail( );

a) Makes list point to second object (and thus removes first object from the list)

list_tail

_value

>>>>

17_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

Page 41: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 41

Questions

What is the effect of (independently):a) list = list.getTail( );b) list._tail = list; // Assume _tail is not private

a) Makes list point to second object (and thus removes first object from the list)

list_tail

_value

>>>>

17_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

Page 42: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 42

Questions

What is the effect of (independently):a) list = list.getTail( );b) list._tail = list; // Assume _tail is not private

a) Makes list point to second object (and thus removes first object from the list)

b) Makes the first object's _tail field point to the first object (and thus loses the rest of the list)

list_tail

_value

>>>>

17_tail

_value10945

_tail

_value616

>>>>_tail

_value

null

-14

>>>>

Page 43: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 43

Queues, Stacks, Trees

• Linked lists (such as above) are the main building blocks of a variety of data abstractions:–Queues: new records added to one end and taken

from the other end; first-in, first-out, FIFO

–Stacks: new records added to one end and taken off from the same end; last-in, first-out, LIFO

–Trees: can have more than one linked list attached to each node

Page 44: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 44

Basic Link Manipulation

• maintaining auxiliary pointers to different parts of a list (especially first and last nodes)

• adding to the end of a linked list

• traveling through an entire list

• searching for a particular node

• inserting into the middle of a linked list

• merging two lists together or inserting one list into another

• deleting individual nodes or sublists

Page 45: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 45

Auxiliary Pointers,and Extending a List

currentNode = firstNode._next._next._next;

_next

_data

>>>>

undefined

firstNode _next

_dataundefined

_next

_dataundefined

>>>>_next

_dataundefined

>>>>

currentNode

null

currentNode._next = new Node( );

currentNode = currentNode._next;

currentNode._next = null;

_next

_data

>>>>

undefined

firstNode _next

_dataundefined

_next

_dataundefined

>>>>_next

_dataundefined

>>>>

currentNode

null_next

_dataundefined

>>>>

Page 46: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 46

Questions

What is the effect of making the assignment:

currentNode._next = firstNode;

_next

_data

>>>>

undefined

firstNode_next

_dataundefined

_next

_dataundefined

>>>>_next

_dataundefined

>>>>

currentNode

null_next

_dataundefined

>>>>

Page 47: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 47

Questions

What is the effect of making the assignment:

currentNode._next = firstNode;

_next

_data

>>>>

undefined

firstNode_next

_dataundefined

_next

_dataundefined

>>>>_next

_dataundefined

>>>>

currentNode

null_next

_dataundefined

>>>>

_next

_data

>>>>

undefined

firstNode_next

_dataundefined

_next

_dataundefined

>>>>_next

_dataundefined

>>>>

currentNode

_next

_dataundefined

>>>>

We get a circular list.

>>>>

Page 48: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 48

Going to the End of a List

Goal: Have a pointer to the list’s last node.

Bound: The current node’s _next field is null.

Plan: Advance a pointer to the next node in the list.

// Precondition: currentNode addresses any nodewhile ( currentNode._next != null ) {

currentNode = currentNode._next;}// Postcondition: currentNode addresses the last node

Be careful about the bounds (looking for an item or the node before an item? bounded by the list’s last node or by the null value that ends the list?)

Page 49: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 49

Searching for a Node

Goal: Have a pointer to the node with a particular data field.Intentional bound: Current node’s _data field is the one we want.Necessary bound: The current node’s _next field is null.Plan: Advance a pointer to the next node in the list.

// Precondition: firstNode addresses any nodecurrentNode = firstNode; //start at the beginningwhile ( (currentNode._data != soughtValue) &&

(currentNode._next != null) ) {currentNode = currentNode._next;

}// Postcond.: If soughtValue is there, it’s in currentNodeif ( currentNode._data == soughtValue )

System.out.println("Value found.");else System.out.println("Value not found.");

Page 50: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 50

An Example: Creating and Printing a List

Read a sequence of positive numbers that ends with a zero or negative sentinel. Print the sentinel, then echo the sequence in the order in which it was typed in.

• Initialize

• Create the List

• Print the List

Page 51: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 51

The First Two Steps

initialize the list by allocating its first node, via firstNode;point an auxiliary pointer currentNode to the first node;read theNumber;while ( it isn’t the sentinel ) {

add a new node to the end of the list;advance the currentNode reference to it;

make its _next field null;save theNumber in it;read the next theNumber;

}// Postcondition: the first stored value is in the node that// follows firstNode; the last stored value is in the node// whose _next field is null

Page 52: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 52

Now Print the List’s Contents

// Remember the first node’s _data field is undefined

point currentNode to the first node again;

while (the current node’s _next field isn’t null) {

advance the currentNode reference to the _next node;

print the current node’s _data field;

}

// Postcondition: We’ve printed every defined// _data field in the list

Page 53: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

class LinkEcho { public static void main (String[ ] args) {

Node firstNode, currentNode;int theNumber;firstNode = new Node( );firstNode._next = null;currentNode = firstNode;SimpleInput sinp = new SimpleInput(System.in);

System.out.println("Enter numbers, zero/negative sentinel");theNumber = sinp.readInt( );while ( theNumber > 0 ) {

currentNode._next = new Node( );currentNode = currentNode._next;currentNode._next = null;currentNode._data = theNumber;theNumber = sinp.readInt( );

}currentNode = firstNode; // Its data field is emptywhile ( currentNode._next != null ) {

currentNode = currentNode._next;System.out.print(currentNode._data);

}}

}

class Node {int _data;Node _next;…

} different file

Page 54: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 54

Inserting Nodes

• Let’s say we want to insert a node right after the node pointed to by current

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

Page 55: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 55

Inserting Nodes

• Let’s say we want to insert a node right after the node pointed to by current

temp = new Node( );

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

_next

_data0

null

temp

Page 56: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 56

Inserting Nodes

• Let’s say we want to insert a node right after the node pointed to by current

temp = new Node( );temp._data = 3000;

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

_next3000

null

temp _data

Page 57: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 57

Inserting Nodes

• Let’s say we want to insert a node right after the node pointed to by current

temp = new Node( );temp._data = 3000;temp._next = current._next; // order is crucial

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

_data3000

>>>>

temp_next

Page 58: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 58

Inserting Nodes

• Let’s say we want to insert a node right after the node pointed to by current

temp = new Node( );temp._data = 3000;temp._next = current._next; // order is crucialcurrent._next = temp;

_next

_data

>>>>

1000

firstNode

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

_next

_data3000

>>>>

temp

_next

Page 59: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 59

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>>

current

null

Data

_next

_data3000

>>>>freshNode

Page 60: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 60

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>> null

_next

_data0

null

tempData

_next

_data3000

>>>>

temp = new Node( );

freshNodecurrent

Page 61: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 61

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>> null

_next

_data4000

null

tempData

_next

_data3000

>>>>

temp = new Node( );temp._data = current._data;

freshNodecurrent

Page 62: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 62

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>> null

_next

_data4000

>>>>

tempData

_next

_data3000

>>>>

temp = new Node( );temp._data = current._data;temp._next = current._next;

currentfreshNode

Page 63: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 63

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data4000

>>>>_next

_data5000

>>>> null

_next

_data4000

>>>>

tempData

_next

_data3000

>>>>

temp = new Node( );temp._data = current._data;temp._next = current._next;current._next = temp;

freshNodecurrent

Page 64: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 64

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data3000

>>>>_next

_data5000

>>>> null

_next

_data4000

>>>>

tempData

_next

_data3000

>>>>

temp = new Node( );temp._data = current._data;temp._next = current._next;current._next = temp;current._data = freshNode._data;

freshNodecurrent

Page 65: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 65

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data3000

>>>>_next

_data5000

>>>> null

_next

_data4000

>>>>

tempData

_next

_data3000

>>>>

freshNode

temp = new Node( );temp._data = current._data;temp._next = current._next;current._next = temp;current._data = freshNode._data;freshNode = current;

current

Page 66: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 66

Inserting a new Node Before the Current Pointer Position (!)

_next

_data

>>>>

1000

firstNode_next

_data2000

_next

_data3000

>>>>_next

_data5000

>>>>

current

null

_next

_data4000

>>>>

tempData

_next

_data3000

>>>>

temp = new Node( );temp._data = current._data;temp._next = current._next;current._next = temp;current._data = freshNode._data;freshNode = current;current = current._next;

freshNode

Page 67: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 67

Inserting a Sublist

_next

_data

>>>>

1000_next

_data2000

_next

_data6000

>>>>_next

_data7000

>>>>

_next

_data5000

null

newEndDatanewHead

_next

_data3000

>>>>

>>>>

currentNode

_next

_data4000

>>>>

Page 68: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 68

Inserting a Sublist

newEnd._next = currentNode._next;

_next

_data

>>>>

1000_next

_data2000

_next

_data6000

>>>>_next

_data7000

>>>>

_next

_data5000

>>>>

newEndDatanewHead

_next

_data3000

>>>>

>>>>

currentNode

_next

_data4000

>>>>

Page 69: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 69

Inserting a Sublist

newEnd._next = currentNode._next;currentNode._next = newHead;

_next

_data

>>>>

1000_next

_data2000

_next

_data6000

>>>>_next

_data7000

>>>>

_next

_data5000

>>>>

newEndDatanewHead

_next

_data3000

>>>>

>>>>

currentNode

_next

_data4000

>>>>

Page 70: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 70

Deleting a Sublist

_next

_data

>>>>

1000_next

_data2000

_next

_data2668

>>>>_next

_data3000

>>>>

currentNode

_next

_data2667

>>>> >>>>

lastBadNode

Page 71: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 71

Deleting a Sublist

currentNode._next = lastBadNode._next;

_next

_data

>>>>

1000_next

_data2000

_next

_data2668

>>>>_next

_data3000

>>>>

currentNode

_next

_data2667

>>>> >>>>

lastBadNode

Page 72: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 72

Deleting a Sublist

What does this do?currentNode._next = lastBadNode;

_next

_data

>>>>

1000_next

_data2000

_next

_data2668

>>>>_next

_data3000

>>>>

currentNode

_next

_data2667

>>>> >>>>

lastBadNode

Page 73: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 73

Deleting a Sublist (not)

What does this do?currentNode._next = lastBadNode;

_next

_data

>>>>

1000_next

_data2000

_next

_data2668

>>>>_next

_data3000

>>>>

currentNode

_next

_data2667

>>>> >>>>

lastBadNode

Page 74: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 74

Another (2-class) Encapsulation of a Linked List (here's the node)

class IntNode {private int _value;private IntNode _next;

public IntNode(int val, IntNode n) {_value = val;_next = n;

}

public IntNode getNext( ) {return _next;}

public void setNext(IntNode node){_next = node;}

}

Page 75: Introduction to Computer Science Linked Lists Basic Link Operations Unit 17

17- 75

The Actual List Class

class IntList {private IntNode _head;IntList( ) { _head = null; }boolean empty( ) { return _head == null; }

// add a link at the end of the listadd(IntNode node) {

if (empty( ))_head = node;

else { IntNode ptr = _head;while (ptr.getNext( ) != null)

ptr = ptr.getNext( );ptr.setNext(node); }

}}