Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Similar presentations


Presentation on theme: "Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior."— Presentation transcript:

1

2 Linked Lists CSC 172 SPRING 2002 LECTURE 3

3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior  Dictionary, Stack, Queue  Data Model – organization  List, Tree, Graph  Data Structure – implementation  Array, Linked list, BST, Adjacency Matrix

4 The Dictionary ADT  Computer programs commonly maintain a set of values where we wish to  Insert elements into the set  Delete Elements from the set  Look up elements (see if they are currently in the set)  There are lots of ways to implement this  Some are more efficient than others

5 The Dictionary Interface public interface Dictionary { public abstract void insert(Object o); public abstract void delete(Object o); public abstract boolean lookup(Object o); }

6 List Data Model  A list is a finite sequence of zero or more elements  Grocery list  Laundry list  (a 1,a 2,…,a n )  Formally, we can think of a list as either  Empty, or  An element followed by a (possibly empty) list

7 Lists  Empty (no elements)  An element (head), followed by a list (tail)  Head sometimes called CAR  Tail sometimes called CDR  Length of a list is  0 for an empty list  1 + length(tail) for a non-empty list

8 How can we implement this?  We can implement a Dictionary ADT... using a list data model... with an array data structure... or a linked-list data structure.

9 Arrays  An array is a collection of data items of the same type  Every element of the collection can be accessed separately. Object [] data = new Object[10];

10 Array Implementation public class myInfo implements Dictionary{ private int defaultCapacity = 100, length = 0; private Object[] datum; public myInfo() { datum = new Object[defaultCapacity]; } public myInfo(int initCapacity) { datum = new Object[initCapacity]; }

11 Implementation public class myInfo implements Dictionary{ private int defaultCapacity = 100, length = 0; private Object[] datum; public myInfo() { datum = new Object[defaultCapacity]; } public myInfo(int initCapacity) { datum = new Object[initCapacity]; }

12 Insert Is this ok? public void insert (Object o) { datum[length++] = o; } What is the run time? What if I have more than 100 elements?

13 Expand private void expand(){ Object[] tempData = new Object[datum.length * 2]; for (int j = 0 ; j<length;j++) tempData[j] = datum[j]; datum = tempData; } So, what is the runtime of this

14 Insert Better? public void insert (Object o) { if (length >= (datum.length –1)) expand(); datum[length++] = o; } This is what the Java class Vector gets you What is the (worst case) run time, now?

15 Self-referential data types class Node { private Object data; // the “data” private Node next; // the “link” } datanext

16 Linked List  A linked list “has-a” reference to a node  The “head” of the list  The ending node has “null” for the next datanext dataNext head dataNext null

17 Linked-List Implementation public class myInfo implements Dictionary{ private int length = 0; private Node head = null;

18 Adding a first node

19 public void insert(Object obj){ Node newLink = new Node(); newLink.data = obj; newLink.next = head; head = newLink; } What is the run time of this?

20 Lookup: Array public boolean lookup(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) return true; return false; } Run time?

21 Lookup: Linked List public boolean lookup(Object o){ Object temp = head; while (temp != null){ if ((temp.data).equals(o)) return true; temp = temp.next; } return false; } Run time?

22 Lookup: Linked List, recursive public boolean lookup(Object o){ return lookupNode(o,head); } public boolean lookupNode(Object o, Node n){ if (n == null) return false; else if((n.data).equals(o) return true; else return lookupNode(o,n.next); } Run time?

23 Deleting a node from a List

24 Delete: Linked List, recursive public boolean delete(Object o){ return deleteNode(o,head); }

25 Delete: Linked List, recursive public void deleteNode(Object o, Node n){ if (n == null) return ; if (n.next == null) return; else if(((n.next).data).equals(o)) { n.next = (n.next).next return ; } else return deleteNode(o,n.next); } Run time?

26 Delete: Array  We have to look it up O(n)  Deletion is setting it to null  But, we have to “shift” all the remaining elements.

27 Delete: Array public void delete(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) { for (int k = j; k<(length-1);k++) datum[k] = datum[k+1]; return; }; } Run time?

28 Compare (worst case) Run Time (what about space?) ARRAYLINKED-LIST InsertN (in case of expansion) 1 LookupNN DeleteN (lookup + shift)N (for lookup)

29 Workshop sing-up. The coefficient for the workshop differential is positive and significant. On average, each additional workshop attended in CSC 172 increases the final grade point by 0.19. In substantive terms, this means that a student who attends five additional workshops in CSC 172 should expect his/her grade to increase by almost one full letter grade.


Download ppt "Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior."

Similar presentations


Ads by Google