Linked Lists Anatomy of a linked list A

  • Slides: 22
Download presentation
Linked Lists

Linked Lists

Anatomy of a linked list • A linked list consists of: – A sequence

Anatomy of a linked list • A linked list consists of: – A sequence of nodes my. List a b c Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list is accessed via a header (which contains a reference to the first node in the list) d

More terminology • A node’s successor is the next node in the sequence –

More terminology • A node’s successor is the next node in the sequence – The last node has no successor • A node’s predecessor is the previous node in the sequence – The first node has no predecessor • A list’s length is the number of elements in it – A list may be empty (contain no elements)

Singly-linked lists • Here is a singly-linked list (SLL): my. List a b c

Singly-linked lists • Here is a singly-linked list (SLL): my. List a b c d • Each node contains a value and a link to its successor (the last node has no successor) • The header points to the first node in the list (or contains the null link if the list is empty)

Building a linked list • The textbook by Lafore defines two classes: – Link,

Building a linked list • The textbook by Lafore defines two classes: – Link, which holds a data value and a pointer • I would have called this class “Node” or maybe “Cell” – Link. List, which is the header for a list, and just holds a reference (first) to the first Link in the list • Lafore is avoiding the better name Linked. List, in order to avoid conflicts with the class java. util. Linked. List • The user refers to Link. List and (if the ADT is done properly) neither knows nor cares about the Link class • Lafore does a poor job of hiding the Link class from the user, probably for clarity (better information hiding requires some slightly tricky Java programming)

The Link. List class • The most important thing in the Link. List class

The Link. List class • The most important thing in the Link. List class is a reference to the first node in the list: – public class Link. List { private Link first; • The class has a constructor: – Link. List() { first = null; } • And some methods: – – – public boolean is. Empty() { return first == null; } public void insert. First(int data) {. . . } public Link delete. First() {. . . } public Link find(int key) {. . . } public Link delete(int key) {. . . }. . . and several others

About headers • Since the header of a list is what the user sees,

About headers • Since the header of a list is what the user sees, it is usually given a name like Linked. List (as if it were the entire thing), while the actual nodes in the list are given less impressive-sounding names (like Link, or Node) • A list header always contains a reference to the first element of the list – If this is all it contains, we don’t really need it; just keep a reference to the first element of the list • In this case, the list nodes would have a name like Linked. List, so that our reference can be of type Linked. List • Typically, a list header contains other information as well, such as how many nodes are in the list, or a reference to the last node in the list

Creating a simple list • To create the list [1, 2, 3]: Link. List

Creating a simple list • To create the list [1, 2, 3]: Link. List numbers = new Link. List(); numbers. insert. First(3); numbers. insert. First(2); numbers. insert. First(1); numbers 1 2 3

Traversing a SLL • The following method traverses a list (and prints its elements):

Traversing a SLL • The following method traverses a list (and prints its elements): public void print() { for (Link curr = first; curr != null; curr = curr. next) { System. out. print(next. element + " "); } } • This would be an instance method of the Link. List class

Traversing a SLL (animation) curr numbers 1 2 3

Traversing a SLL (animation) curr numbers 1 2 3

Inserting a node into a SLL • There are many ways you might want

Inserting a node into a SLL • There are many ways you might want to insert a new node into a list: – – – As the new first element As the new last element Before a given node (specified by a reference) After a given node Before a given value After a given value • All are possible, but differ in difficulty

Inserting as a new first element • This is probably the easiest method to

Inserting as a new first element • This is probably the easiest method to implement • public void insert. First(int data) { Link new. Link = new Link(data); new. Link. next = first; first = new. Link; } • Notice that this method works correctly when inserting into a previously empty list

Inserting a node after a given value public void insert. After(int old. Data, int

Inserting a node after a given value public void insert. After(int old. Data, int new. Data) { for (Link current = first; current != null; current = current. next) { if (current. data == old. Data) { Link new. Link = new Link(new. Data); new. Link. next = current. next; current. next = new. Link; return; } } System. out. print("Not found"); }

Inserting after (animation) node 3 numbers 1 2 4 Find the node you want

Inserting after (animation) node 3 numbers 1 2 4 Find the node you want to insert after First, copy the link from the node that's already in the list Then, change the link in the node that's already in the list

Deleting a node from a SLL • In order to delete a node from

Deleting a node from a SLL • In order to delete a node from a SLL, you have to change the link in its predecessor • This is slightly tricky, because you can’t follow a pointer backwards • Deleting the first node in a list is a special case, because the node’s predecessor is the list header

Deleting an element from a SLL • To delete the first element, change the

Deleting an element from a SLL • To delete the first element, change the link in the header numbers 1 2 3 • To delete some other element, change the link in its predecessor numbers 1 2 3 • Deleted nodes will eventually be garbage collected

Deleting from a SLL public void delete(int bad. Data) { if (first == null)

Deleting from a SLL public void delete(int bad. Data) { if (first == null) return; // not in list (list is empty) if (first. data == bad. Data) { first = first. next; return; } for (Link current = first; current. next != null; current = current. next) { if (current. next. data == bad. Data) { current. next = current. next; return; } } // not in list }

Doubly-linked lists • Here is a doubly-linked list (DLL): my. DLL a b c

Doubly-linked lists • Here is a doubly-linked list (DLL): my. DLL a b c • Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any) • The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty)

DLLs compared to SLLs • Advantages: – Can be traversed in either direction (may

DLLs compared to SLLs • Advantages: – Can be traversed in either direction (may be essential for some programs) – Some operations, such as deletion and inserting before a node, become easier • Disadvantages: – Requires more space – List manipulations are slower (because more links must be changed) – Greater chance of having bugs (because more links must be manipulated)

Deleting a node from a DLL • Node deletion from a DLL involves changing

Deleting a node from a DLL • Node deletion from a DLL involves changing two links my. DLL a b c • Deletion of the first node or the last node is a special case • Garbage collection will take care of deleted nodes

Other operations on linked lists • Most “algorithms” on linked lists—such as insertion, deletion,

Other operations on linked lists • Most “algorithms” on linked lists—such as insertion, deletion, and searching—are pretty obvious; you just need to be careful • Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements

The End

The End