New version page

Penn CIS 121 - Linked lists

Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

Linked ListsAnatomy of a linked listMore terminologySingly-linked listsBuilding a linked listThe LinkList classAbout headersCreating a simple listTraversing a SLLTraversing a SLL (animation)Inserting a node into a SLLInserting as a new first elementInserting a node after a given valueInserting after (animation)Deleting a node from a SLLDeleting an element from a SLLDeleting from a SLLDoubly-linked listsDLLs compared to SLLsDeleting a node from a DLLOther operations on linked listsThe EndLinked ListsAnatomy of a linked list•A linked list consists of:–A sequence of nodesa b c dEach node contains a valueand a link (pointer or reference) to some other nodeThe last node contains a null linkThe list is accessed via a header (which contains a reference to the first node in the list)myListMore 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):•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)a b c dmyListBuilding 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”–LinkList, 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 LinkedList, in order to avoid conflicts with the class java.util.LinkedList•The user refers to LinkList 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 LinkList class•The most important thing in the LinkList class is a reference to the first node in the list:–public class LinkList { private Link first;•The class has a constructor:–LinkList() { first = null; }•And some methods:–public boolean isEmpty() { return first == null; }–public void insertFirst(int data) {...}–public Link deleteFirst() {...}–public Link find(int key) {...}–public Link delete(int key) {...}–...and several othersAbout headers•Since the header of a list is what the user sees, it is usually given a name like LinkedList (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 LinkedList, so that our reference can be of type LinkedList•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 listCreating a simple list•To create the list [1, 2, 3]:321numbersLinkList numbers = new LinkList();numbers.insertFirst(3);numbers.insertFirst(2);numbers.insertFirst(1);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 LinkList classTraversing a SLL (animation)321numberscurrInserting 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 difficultyInserting as a new first element•This is probably the easiest method to implement•public void insertFirst(int data) { Link newLink = new Link(data); newLink.next = first; first = newLink;}•Notice that this method works correctly when inserting into a previously empty listInserting a node after a given valuepublic void insertAfter(int oldData, int newData) { for (Link current = first; current != null; current = current.next) { if (current.data == oldData) { Link newLink = new Link(newData); newLink.next = current.next; current.next = newLink; return; } } System.out.print("Not found");}Inserting after (animation)421numbers3nodeFind the node you want to insert afterFirst, copy the link from the node that's already in the listThen, change the link in the node that's already in the listDeleting 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 headerDeleting an element from a SLL321numbers321numbers• To delete the first element, change the link in the header• To delete some other element, change the link in its predecessor• Deleted nodes will eventually be garbage collectedDeleting from a SLL public void delete(int badData) { if (first == null) return; // not in list (list is empty) if (first.data == badData) { first = first.next; return; } for (Link current = first; current.next != null; current = current.next) { if (current.next.data == badData) { current.next = current.next.next; return; } } // not in list}Doubly-linked lists•Here is a doubly-linked list (DLL):•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)myDLLa bcDLLs 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


View Full Document
Download Linked lists
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Linked lists and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Linked lists 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?