variations on linked lists
DESCRIPTION
Variations on Linked Lists. Ellen Walker CPSC 201 Data Structures Hiram College. Linked List (review). Each element contains two parts: Value of this element Pointer to the next element class string_node { Private: string val; string_node *next; }. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/1.jpg)
Variations on Linked Lists
Ellen WalkerCPSC 201 Data Structures
Hiram College
![Page 2: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/2.jpg)
Linked List (review)
• Each element contains two parts:– Value of this element– Pointer to the next element
class string_node {Private: string val; string_node *next;}
![Page 3: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/3.jpg)
Inserting into a Linked List (Review)
BarryAnn
Carol
Ellen
/* Assume item is a reference to the node that contains “Barry”*/
Node<string> newNode = new Node<string> (“Carol”);newNode.next = aNode.next;aNode.next = newitem;
![Page 4: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/4.jpg)
Printing a Linked List (Review)
//print elements, starting at the node called head;// Uses a for loop for(Node<String> current=head; current!=null;
current=current.next){ System.out.println(current.data); }
![Page 5: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/5.jpg)
Printing Using Iterator (Review)
//prints the list, one item per line//returns iterator pointing before the list…Iterator<String> itr = aList.iterator();while(itr.hasNext()){ String name = itr.next();
System.out.println(name);}
![Page 6: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/6.jpg)
LinkedList Implementation (Review)
public class LinkedList<E>{ private Node<E> head; //first element or null private int size; //list length … private static class Node<E>{ //inner class private E data; private node<E> next;
} //methods go here}
![Page 7: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/7.jpg)
Implementing toString
• Writes elements of list into a string• Use either traversal from before (iterator version
shown)String toString(){ StringBuilder result = new StringBuilder(); Iterator<E> itr = iterator(); while(itr.hasNext()){ result.append(itr.next());
result.append(‘\n’); }}
![Page 8: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/8.jpg)
Dummy Nodes simplify implementation
• An empty list has one node (the dummy node)• There is no longer a special case for adding the first
element of the node!• What changes need to be made to…
– Constructor?– Find / Retrieve ?– Insert / Delete?
• Sometimes a dummy node can have a useful value (e.g. smallest possible element for sorted list)
![Page 9: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/9.jpg)
Changes to the implementation for a dummy node
• Constructor– Head is a new (dummy) node instead of NULL
• indexOf / get– indexOf is unchanged (assume value not
otherwise in list)– get(k) will return value at node (k+1)
• Insert / Delete– Begin by locating the predecessor node– No special cases for first node (it has a
predecessor)
![Page 10: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/10.jpg)
Circular List
• Circular List: The tail of the list points back to the head
• There is no NULL pointer to “end” the list.
![Page 11: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/11.jpg)
Issues with circular list
• How do you know when you’re done?– Make sure you save the head pointer.– When (cur.next == head) you’ve reached the end
• How are insertion and deletion handled?– No special cases!– Predecessor to head node is the last node in the
list.
![Page 12: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/12.jpg)
The Josephus Problem
• (One of many variations…)The founder of a startup is forced to lay off all but one employee. Not having any better way to decide, he arranges all employees in a circle and has them count off. The 10th employee in the circle is laid off, and the count begins again. The last person is not laid off.
• If there are N employees, where should you sit to avoid being laid off?
![Page 13: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/13.jpg)
Solution
• Model the circle of employees as a circular linked list
• Implement the counting off process, and delete the 10th employee each time
• After N-1 people are deleted, there should be only one employee left.
• That employee’s original position number is the solution to the problem.
![Page 14: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/14.jpg)
Doubly Linked List
• Each node has prev and next pointer• List can be traversed forward or backward• To insert a node after “cur”
– Node<E> tmp = new Node<E>(newItem);– tmp.next = cur.next;– cur.next=tmp;– tmp.prev=cur;– tmp.next.prev = tmp;
• Reverse the process to delete!
![Page 15: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/15.jpg)
Which list implementation?
• Array– Can jump into the middle easily (random access)– Inserting & deleting can require time-consuming shifting– Must allocate block of memory at once (can resize later with new)
• Linked– No random access– Insert & delete are fixed cost, once you decide where.– Nodes allocated one at a time.
![Page 16: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/16.jpg)
Linked List Choices
• Plain vanilla– Simple, small
• Dummy header– Eliminates special cases (chance for error)
• Circular– No defined start position in sequence– Can find the node before, though it takes N steps
• Doubly linked– Easy to find the node before– Larger Node; twice as many special cases
• Circular and doubly linked
![Page 17: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/17.jpg)
Linked Lists and Recursion
• Recursive definition of a list– Null (empty list) is a list (base case)– A list consists of one item (the head) followed by a
list (next)• Example:
– A->B->C-| is a list– Head = A, next = B->C-|
![Page 18: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/18.jpg)
Recursive Function on Lists
• Base case for the recursive function is the empty list (base case for the definition)
• Other cases -– “Take a step” = do something to the head– Recurse = call function for the rest of the list– May or may not build up a solution
![Page 19: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/19.jpg)
Recursive List Search
Node<E> recFind (Node<E> head, E x){//x is not in an empty list!If (head == null) return null;//x is found at the beginningElse if (head.item == x) return head;//Recursive case: search the rest of the listReturn recFind(head.next, x);
}
![Page 20: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/20.jpg)
Recursive functions are private!
• A recursive function uses Node references.• Node references are private
– Implementation detail– Not every implementation of List has Nodes
• Most recursive functions have a public “starter” and a private internal function
![Page 21: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/21.jpg)
Public Caller for recFind
boolean isIn(E x){ //call Find on the head of the list //if Find does not return null, the item is in
// the list. return (recFind(head,x) != null);}
![Page 22: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/22.jpg)
Recursive CountItem
• This function counts the number of occurrences of the item x in the list
Int CountItem (Node * head, ItemType x){
}
![Page 23: Variations on Linked Lists](https://reader035.vdocuments.site/reader035/viewer/2022081505/56815ad9550346895dc8a5d0/html5/thumbnails/23.jpg)
Public Caller for CountItem
int List::howMany(ListItemType x){
}