Download presentation
Presentation is loading. Please wait.
1
1 Linked Lists It’s a conspiracy!
2
2 Linked list: a data structure used to 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
3 Node 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
4
Portrait 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
5
Portrait of a linked list 5 data link 10 data link 15 data link 7 head NULL tail Head and tail nodes illustrated here are dummies; they have link members that point to other nodes, contain no data
6
6 previousEvery node has a link through which it can be accessed – this is the link member of the previous 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 members
7
Operations on Nodes Constructor: creates Node with specified data and link Accessors & mutators for data and link 7
8
Constructors 8 public Node(int initialData, Node initialLink) { data = initialData; link = initialLink; } public Node () { data = 0; link = null; }
9
Accessors & 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
10 Operations 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
11 Operations 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
12 Inserting 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 null head entry 13
13
Inserting 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 1515 7 null head 13 When the function returns, the linked list has a new node at the front, containing 13.
14
14 Caution!Caution! Always make sure that your linked list methods work correctly with an empty list. EMPTY LIST
15
15 Pseudocode 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
16 Removing the Head Node 10 1515 7 null head 13 Removal of the first node is easily accomplished with the following instruction: head = head.getLink();
17
17 Removing 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 null head_ptr
18
Inserting nodes elsewhere in the list 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
19 Pseudocode 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, the new node will be the second node In this example, the new node will be the second node previous_ptr
20
20 Pseudocode for Inserting Nodes 15 10 7 null head Look at the pointer which is in the node referenced by previous_ptr Look at the pointer which is in the node referenced by previous_ptr previous_ptr This pointer is called previous_ptr.link This pointer is called previous_ptr.link
21
21 Pseudocode for Inserting Nodes 15 10 7 null head previous_ptr.link points to the head of a small linked list, with 10 and 7 previous_ptr.link points to the head of a small linked list, with 10 and 7 previous_ptr
22
22 previous_ptr.setLink (new Node(13, previous_ptr.getLink)); Pseudocode for Inserting Nodes 15 10 7 null head The new node must be inserted at the front of this small linked list. The new node must be inserted at the front of this small linked list. 13 previous_ptr
23
Node insertion & the previous example 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
24
Same 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
25 Removing a Node from elsewhere in the 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
26
Removing a node that is not at the 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
27
Methods that operate on an entire list 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
28
linklist228 Code 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; }
29
linklist229 Finding 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
30
linklist230 Code 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; }
31
linklist231 Finding value at specific location Traverse list to current position, or until cursor is null Return value of cursor once list has been traversed
32
linklist232 Code 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
33 Linked Lists It’s a conspiracy!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.