Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.

Similar presentations


Presentation on theme: "CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers."— Presentation transcript:

1 CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers

2 Reference in Java A must read: http://stackoverflow.com/questions/40480/is-java-pass-by-reference http://stackoverflow.com/questions/40480/is-java-pass-by-reference Study all the examples there!

3 Storing a sequence of records Oftentimes, we need to store a sequence of data records. E.g.: »The list of students: ID, names, grades, etc. »All the emails, sorted by arrival time »Finishing times of marathon runners »Bank transactions

4 Storing a sequence of records Need to support these operations »get the i-th record »delete the i-th record, »check if a record exists (get the index, if any) »insert a record to the i-th place Using an array to store such data records. »Pros and cons? »Fast random access (get the i-th record) »Slow insert/delete »Need to know the maximum size beforehand Overflow risk vs. waste of storage

5 A new data structure: Linked Lists List of integers: [ 2 3 5 2 16 4 ] Key differences to array? »Array: random access (directly goes to i-th record) »List: sequential access (follows the references) 2 3 5 2 16 4

6 Inserting and Deleting Inserting a record Removing a record 2 3 5 2 16 4 16 2 3 5 4 7 2

7 Linked Lists List of integers: [ 2 3 5 2 16 4 ] public class ListItem{ public int number; public ListItem next; public ListItem(int value, ListItem p){ number = value; next = p; } } Building lists with the constructor »ListItem p = new ListItem(3,null); »ListItem q = new ListItem(7,p); »ListItem r = new ListItem(2,new ListItem(5,null)); »What happens when q=p;? when ListItem d = r.next; ? 2 3 5 2 16 4 p 3 7 q r 2 5

8 toString for the ListItem class public class ListItem{ public int number; public ListItem next; public ListItem(int value, ListItem p){ number = value; next = p; } public String toString() { if (next == null) return number + “ “; else return number + “ “ + next.toString(); } }

9 Traversing a List Pass over list by advancing object reference Task: get the sum of all ListItem values reachable from a ListItem p 2 3 5 2 16 4 p // recursive traversal int sum(ListItem p) { if (p == null) return 0; else return p.number + sum(p.next); } // iterative traversal int sum(ListItem p) { int result = 0; while (p != null) { result += p.number; p = p.next; } return result; }

10 Implementing a LinkedIntList Class List of integer values with the following operations »addFirst(int x) – add the value x at the front of the list insert new ListItem before current head »addLast(int x) – add the value x at the end of the list add new ListItem after last item »indexOf(int x) – return the index of first item with value x scan the list for first match »remove(int x) – delete the first item with value x scan to the first item and re-direct previous item’s next pointer »size() – return the number of elements in the list uses a stored value Two instance variables »ListItem head – reference to first ListItem (or null) »int size – number of items in the list

11 Adding Elements // Create new list item and insert before current first element void addFirst(int x) { head = new ListItem(x, head); size++; } // Create new list item and insert it at the end of the list void addLast(int x) { if (head == null) { addFirst(x); } else { // scan to end of list, then insert item ListItem p; for (p = head; p.next != null; p = p.next) {} p.next = new ListItem(x,null); size++; }

12 Finding and Removing Items in a List // Return index of first item with value x (or -1 if none) int indexOf(int x) { int i = 0; for (ListItem p = head; p != null; p = p.next) { if (p.number == x) return i; i++; } return -1; } // Remove first item with given value void remove(int x) { if (head == null) return; if (head.number == x) { head = head.next; size--; return; } for (ListItem p = head; p.next != null; p = p.next) { if (p.next.number == x) { p.next = p.next.next; size--; return; }}

13 Doubly Linked Lists Lists with forward and reverse links »public class ListItem{ public int number; public ListItem next; public ListItem prev; } »makes it easy to traverse a list in reverse (by following p.prev) »makes it easier to delete arbitrary elements (e.g. delete the previous item) 2 3 5 2 16 4 head size=6 tail

14 Doubly Linked Lists and Sentinels Lists are sometimes implemented with sentinel nodes at the beginning and end »makes implementation more symmetric »reduces number of special cases in methods »list object includes references to both head and tail nodes »explore it in your studio tomorrow 2 3 5 2 16 4 - - head size=6 tail


Download ppt "CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers."

Similar presentations


Ads by Google