DOC PREVIEW
Penn CIT 594 - Linked lists

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Linked ListsAnatomy of a linked listMore terminologySingly-linked listsSingly-linked lists in Java (p. 69)SLL nodes in Java (p. 69)Creating a simple listTraversing a SLL (p. 70)Traversing 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 SLL (p. 84)Doubly-linked listsDLLs compared to SLLsConstructing SLLs and DLLs (p. 74)DLL nodes in Java (p. 75)Deleting 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 may have a headermyListMore 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 dmyListSingly-linked lists in Java (p. 69) public class SLL { private SLLNode first; public SLL() { this.first = null;} // method s... }•This class actually describes the header of a singly-linked list•However, the entire list is accessible from this header•Users can think of the SLL as being the list–Users shouldn’t have to worry about the actual implementationSLL nodes in Java (p. 69) public class SLLNode { protected Object element; protected SLLNode succ; protected SLLNode(Object elem, SLLNode succ) { this.element = elem; this.succ = succ; }}Creating a simple list•To create the list ("one", "two", "three"):•SLL numerals = new SLL();•numerals.first = new SLLNode("one", new SLLNode("two", new SLLNode("three", null)));threetwoonenumeralsTraversing a SLL (p. 70)•The following method traverses a list (and prints its elements): public void printFirstToLast() { for (SLLNode curr = first; curr != null; curr = curr.succ) { System.out.print(curr.element + " "); }}•You would write this as an instance method of the SLL classTraversing a SLL (animation)threetwoonenumeralscurrInserting 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 void insertAtFront(SLLNode node) {node.succ = this.first;this.first = node;}•Notice that this method works correctly when inserting into a previously empty listInserting a node after a given valuevoid insertAfter(Object obj, SLLNode node) { for (SLLNode here = this.first; here != null; here = here.succ) { if (here.element.equals(obj)) { node.succ = here.succ; here.succ = node; return; } // if } // for // Couldn't insert--do something reasonable!}Inserting after (animation)threetwoonenumerals2.5nodeFind 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 SLLthreetwoonenumeralsthreetwoonenumerals• 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 (p. 84) public void delete(SLLNode del) { SLLNode succ = del.succ; // If del is first node, change link in header if (del == first) first = succ; else { // find predecessor and change its link SLLNode pred = first; while (pred.succ != del) pred = pred.succ; pred.succ = succ; } }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)Constructing SLLs and DLLs (p. 74) public class SLL { private SLLNode first; public SLL() { this.first = null;} // method s... } public class DLL { private DLLNode first; private DLLNode last; public DLL() { this.first = null; this.last = null;} // method s... }DLL nodes in Java (p. 75) public class DLLNode { protected Object element; protected DLLNode pred, succ; protected DLLNode(Object elem, DLLNode pred, DLLNode succ) { this.element = elem; this.pred = pred; this.succ = succ; }}Deleting a node from a DLL•Node deletion from a DLL involves changing two links•Deletion of the first node or the last node is a special case•Garbage collection will take care of deleted nodesmyDLLa bcOther 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 elementsThe


View Full Document

Penn CIT 594 - Linked lists

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
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?