1 linked lists it’s a conspiracy!. 2 linked list: a data structure used to represent an ordered...

Post on 22-Dec-2015

215 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Linked ListsLinked Lists

It’s a conspiracy!

2

Linked list: a data structure used Linked list: a data structure used to represent an ordered listto represent an ordered list

• Consists of a sequence of nodes

• A node consists of a data item and a reference to the next node -- the connecting reference is called a link– Links keep nodes in sequence– Last node’s link is assigned null

• Entire list is accessed via the first node in the list, usually called the head node

3

Node structureNode structure

public class Node

{

private int data;

private Node link;

}

• Data type is for data member declared as int as an example – could be any type (and soon will be)

• Node contains a reference to itself -- actual use is as reference to another Node

Portrait of a linked listPortrait of a linked list

• List begins with head node – keeps track of list front– may or may not contain data; if it doesn’t, it’s a

dummy node

• A list may also have a tail node to keep track of the end of the list – could also be a dummy

4

Portrait of a linked listPortrait of a linked list

5

data

link

10 data

link

15 data

link

7head

NULL

tail

Head and tail nodes illustrated here are dummies;they have link members that point to other nodes, contain no data

6

• Every node has a link through which it can be accessed – this is the link member of the previousprevious node

• For example, in the list from the previous slide, head.link refers to the first node in the list

– the data in the first node is head.link.data

– head.link.link is a reference to the second node

Accessing list membersAccessing list members

Operations on NodesOperations on Nodes

• Constructor: creates Node with specified data and link

• Accessors & mutators for data and link

7

ConstructorsConstructors

8

public Node(int initialData, Node initialLink) { data = initialData; link = initialLink; }

public Node (){

data = 0;link = null;

}

Accessors & MutatorsAccessors & Mutators

public int getData () {

return data;

}

public Node getLink() {

return link;

}

public void setData (int newData) {

data = newData;

}

public void setLink (Node newLink) {

link = newLink;

}

9

10

Operations on Linked ListsOperations on Linked Lists

• Inserting a node– at the front– at any position other than the front

• Removing a node– from the front– from any other position

• Removing all nodes

11

Operations on Linked ListsOperations on Linked Lists

• Finding the length of a list

• Finding a target value in a list

• Finding a value at a specified position in a list

• Making a copy of a list

• Making a copy of part of a list

12

Inserting a Node at the FrontInserting a Node at the FrontInserting a Node at the FrontInserting a Node at the Front

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

10

15

7

nullhead

entry

13

13

Inserting a Node at the FrontInserting a Node at the FrontInserting a Node at the FrontInserting a Node at the Front

head = new Node(13, head);– first argument is data for new node– second argument is link for new

node; points to node that used to be at front of list

10

15

7

nullhead

13

When the function returns, thelinked list has a new node at thefront, containing 13.

14

Caution!Caution!Caution!Caution!

• Always make sure that your linked list methods work correctly with an empty list.

EMPTY LIST

15

Pseudocode for Removing NodesPseudocode for Removing NodesPseudocode for Removing NodesPseudocode for Removing Nodes

• Nodes 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 pseudocode for removing a node from the front of a linked list.

16

Removing the Head NodeRemoving the Head NodeRemoving the Head NodeRemoving the Head Node

10 15

7

nullhead

13

Removal of the first node is easily accomplished with the following instruction:

head = head.getLink();

17

Removing the Head NodeRemoving the Head NodeRemoving the Head NodeRemoving the Head Node

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

Automatic garbage collection takes care of removing the node that is no longer part of the list.

10 15 7

nullhead_ptr

Inserting nodes elsewhere in the Inserting nodes elsewhere in the listlist

• The head node is the only node to which we have direct access, unless we also maintain a tail node

• Even if this is the case, however, the task of adding a node anywhere besides the front is a bit complicated, since we need access to the node that precedes the spot where the new node is to be added

18

19

Pseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting Nodes

15

10

7

null

head

We begin with the assumption that we already have a reference to the Node just before the one we want to add (labelled previous_ptr in the illustration)

In this example, thenew node will bethe second node

In this example, thenew node will bethe second node

previous_ptr

20

Pseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting Nodes

15

10

7

nullhead

Look at the pointerwhich is in the node

referenced by previous_ptr

Look at the pointerwhich is in the node

referenced by previous_ptr

previous_ptr

This pointer is calledprevious_ptr.link

This pointer is calledprevious_ptr.link

21

Pseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting Nodes

15

10

7

nullhead

previous_ptr.linkpoints to the headof a small linked

list, with 10 and 7

previous_ptr.linkpoints to the headof a small linked

list, with 10 and 7

previous_ptr

22

previous_ptr.setLink (new Node(13, previous_ptr.getLink));

Pseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting NodesPseudocode for Inserting Nodes

15

10

7

nullhead

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

previous_ptr

Node insertion & the previous Node insertion & the previous exampleexample

• The code on the previous slide isn’t part of the Node class described in the textbook, although the principle is discussed there

• The previous slide’s approach to adding a Node in the middle of the list is an example of recursive thinking about a linked list:– every linked list has a head node

– every node is the head node of a linked list

– the very last node in the list is the head node of an empty list

23

Same approach, different codeSame approach, different code

24

Here is the code provided in the textbook’s Node class; it’s the same approach, wrapped in a method:

public void addNodeAfter(int item) { link = new Node(item, link);}

Here, the calling object (of type Node) takes the place of previous_ptr.link in the first example

25

Removing a Node from elsewhere in Removing a Node from elsewhere in the listthe list

• If we want to remove a node from somewhere besides the front of the list, the process is a bit more complicated

• As with list insertion, the key idea is to make sure that we never lose our grip on the rest of the list while we’re making changes to a particular node

• Again, we will assume for the moment that we already have a reference to the node previous to the one we want to remove

Removing a node that is not at Removing a node that is not at the headthe head

• As with insertion, we could accomplish this task with an instruction like the following:previous_ptr.setLink

(previous_ptr.getLink().getLink());

or by using the method in the Node class:

public void removeNodeAfter( ) { link = link.link;

}

26

Methods that operate on an entire Methods that operate on an entire listlist

• This set of methods includes finding the length of list, searching for a value in the list, and copying all or part of a list

• These methods are declared static because they operate on lists, not on Nodes

• Static methods can operate on any linked list, including an empty list

27

linklist2 28

Code for listLengthCode for listLength

public static int listLength(Node head) { Node cursor; int answer; answer = 0; for (cursor = head; cursor != null; cursor = cursor.link) answer++; return answer;

}

linklist2 29

Finding a target value in a listFinding a target value in a list

• Traverse list, using same strategy as in listLength function

• If target is found, return pointer to its node

• If target is not found and cursor reaches end of list, return null

linklist2 30

Code for list_searchCode for list_search

public static Node listSearch(Node head, int target) { Node cursor; for (cursor = head; cursor != null; cursor = cursor.link) if (target == cursor.data) return cursor; return null; }

linklist2 31

Finding value at specific locationFinding value at specific location

• Traverse list to current position, or until cursor is null

• Return value of cursor once list has been traversed

linklist2 32

Code for list_locateCode for list_locate

public static Node listPosition(Node head, int position) { Node cursor; int i; if (position <= 0) throw new IllegalArgumentException

("position is not positive"); cursor = head; for (i = 1; (i < position) && (cursor != null); i++) cursor = cursor.link; return cursor; }

33

Linked ListsLinked Lists

It’s a conspiracy!

top related