attendance question 1 topic 14 li k d li t linked lists
play

Attendance Question 1 Topic 14 Li k d Li t Linked Lists What is - PowerPoint PPT Presentation

Attendance Question 1 Topic 14 Li k d Li t Linked Lists What is output by the following code? ArrayList<Integer> a1 = new ArrayList<Integer>(); "All the kids who did great in high school writing g g g


  1. Attendance Question 1 Topic 14 Li k d Li t Linked Lists � What is output by the following code? ArrayList<Integer> a1 = new ArrayList<Integer>(); "All the kids who did great in high school writing g g g ArrayList<Integer> a2 = new ArrayList<Integer>(); ArrayList<Integer> a2 = new ArrayList<Integer>(); pong games in BASIC for their Apple II would get to a1.add(12); college, take CompSci 101, a data structures a2.add(12); course and when they hit the pointers business their course, and when they hit the pointers business their System.out.println( a1 == a2 ); brains would just totally explode, and the next thing A. No output due to syntax error you knew, they were majoring in Political Science because law school seemed like a better idea." B. No output due to runtime error - Joel Spolsky p y C. false D. true Thanks to Don Slater of CMU for use of his slides. CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Linked Lists Computer Science Linked Lists Dynamic Data Structures Object References � Dynamic data structures � Dynamic data structures � Recall that an object reference is a variable – They grow and shrink one element at a time, that stores the address of an object normally without some of the inefficiencies of normally without some of the inefficiencies of arrays � A reference can also be called a pointer – as opposed to a static container like an array pp y � Big O of Array Manipulations � They are often depicted graphically: – Access the kth element – Add or delete an element in the middle of the student John Smith array while maintaining relative order 40725 – adding element at the end of array? space 3.57 avail? no space avail? – add element at beginning of an array add element at beginning of an array CS 307 Fundamentals of 3 4 Computer Science Linked Lists Linked Lists

  2. References as Links References as Links � Object references can be used to create � References can be used to create a variety links between objects of linked structures, such as a linked list : � Suppose a Student class contained a studentList reference to another Student object f t th bj t d John Smith Jane Jones 40725 58821 3.57 3.72 5 6 Linked Lists Linked Lists Linked Lists Advantages of linked lists � A li � A linear collection of self-referential objects, called ll ti f lf f ti l bj t ll d � Linked lists are dynamic, they can grow or shrink � Li k d li d i h h i k nodes, connected by other links as necessary – linear: for every node in the list, there is one and only one node y , y that precedes it (except for possibly the first node, which may have no predecessor,) and there is one and only one node that � Linked lists can be maintained in sorted order succeeds it, (except for possibly the last node, which may have no successor) no successor) simply by inserting each new element at the proper simply by inserting each new element at the proper point in the list. Existing list elements do not need – self-referential: a node that has the ability to refer to another to be moved to be moved node of the same type, or even to refer to itself node of the same type or even to refer to itself – node: contains data of any type, including a reference to another � Linked lists are non-contiguous; the logical node of the same data type, or to nodes of different data types node of the same data type, or to nodes of different data types Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled – Usually a list will have a beginning and an end; the first element from any physical ordering in memory y p y g y in the list is accessed by a reference to that class, and the last y , node in the list will have a reference that is set to null CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Linked Lists Computer Science Linked Lists

  3. Nodes and Lists A Node Class public class Node<E> { public class Node<E> { � A different way of implementing a list private E myData; private Node myNext; � Each element of a Linked List is a separate public Node() public Node() Node object. { myData = null; myNext = null; } public Node(E data, Node<E> next) � Each Node tracks a single piece of data plus ac ode t ac s a s g e p ece o data p us { { myData = data; myNext = next; } y ; y ; } a reference (pointer) to the next public E getData() { return myData; } � Create a new Node very time we add Create a new Node very time we add public Node<E> getNext() { return myNext; } something to the List public void setData(Et data) � Remove nodes when item removed from list � Remove nodes when item removed from list { { myData = data; D t d t } } and allow garbage collector to reclaim that public void setNext(Node<E> next) { myNext = next; } memory memory } } CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Linked Lists Computer Science Linked Lists Interfaces and Standard Java One Implementation of a Linked List � Th � The Nodes show on the previous slide are N d h th i lid � Finally, an alternate singly linked implementation to an ADT – a node refers only to the next node in the d f l t th t d i th � Specify a List interface structure – Java has this – it is also possible to have doubly linked nodes it is also possible to have doubly linked nodes. C Cookie ki � Implement in multiple ways – The node has a reference to the next node in the structure and the previous node in the structure p – ArrayList ArrayList as well – LinkedList � How is the end of the list indicated � Which is better? � Which is better? – myNext = null for last node – a separate dummy node class / object CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Linked Lists Computer Science Linked Lists

  4. A Linked List Implementation Writing Methods public class LinkedList<E> implements Ilist<E> public class LinkedList<E> implements Ilist<E> � When trying to code methods for Linked private Node<E> head; private Node<E> tail; Lists draw pictures! private int size; – If you don't draw pictures of what you are trying public LinkedList(){ to do it is very easy to make mistakes! head = null; tail = null; tail = null; size = 0; } } LinkedList<String> list = new LinkedList<String>(); LinkedList myHead iMySize null 0 null ll myTail CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Linked Lists Computer Science Linked Lists add method Add Element - List Empty (Before) � add to the end of list head tail size � special case if empty null ll null ll 0 0 � steps on following slides � public void add(Object obj) public void add(Object obj) Object item CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Linked Lists Computer Science Linked Lists

  5. Add Element - List Empty (After) Add Element - List Not Empty (Before) head h d tail il size i head tail size 1 1 Node Node Node myData myNext String null null myData myNext null null String String item CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Linked Lists Computer Science Linked Lists Code for default add Add Element - List Not Empty (After) � public void add(Object obj) h head d tail il size i 2 Node Node Node Node myData myNext myData myNext null ll String String CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Linked Lists Computer Science Linked Lists

  6. Attendance Question 2 Code for addFront � What is the worst case Big O for adding to � add to front of list the end of an array based list and a linked � public void addFront(Object obj) list? The lists already contains N items. � How does this compare to adding at the front Array based Linked of an array based list? o a a ay based st A. O(1) O(1) B. O(N) B O(N) O(N) O(N) C. O(logN) O(1) D. O(1) D O(1) O(N) O(N) E. O(N) O(1) CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Linked Lists Computer Science Linked Lists Attendance Question 3 Code for Insert � What is the Big O for adding to the front of � public void insert(int pos, Object obj) an array based list and a linked list? The lists � Must be careful not to break the chain! already contains N items. � Where do we need to go? Array based Linked � Special cases? Special cases? A. O(1) O(1) B O(N) B. O(N) O(1) O(1) C. O(logN) O(1) D. O(1) D O(1) O(N) O(N) E. O(N) O(N) CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Linked Lists Computer Science Linked Lists

Recommend


More recommend