CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? Linked lists –Finish the delete a node code from last time –Change the Deck class to store a linked list of CardNodes instead of an ArrayList of Cards. –Doubly linked lists
Linked lists A linked list is a data structure where every node contains data and reference(s) to other node(s.) Let's look at drawings on the board. Let's figure out how to – Add to beginning of linked list – Add to end of linked list Need to find last node – Insert after a particular node – Delete a particular node Other options – Storing link to last node – Doubly linked lists (and their operations.) – Circular linked lists (where the “last” node refers to the “head”).
Linked lists What's deficient in singly linked lists?
Linked lists What's deficient in singly linked lists? –can't add to the end of the list efficiently (add tail) what gets easier? How to do it; what needs to change? –can't “back up” if necessary, so we can't say, traverse the linked list from the tail to the head because we only have a next (one way) create a doubly linked list = add links that go both ways (next and prev)
Linked lists A doubly linked list is a data structure where every node contains – data – a reference to previous node – a reference to next node The value of the next node for the last element of a linked list is null. The value of the previous node for the first element of a linked list is also null.
Linked lists Let's consider operations for a doubly linked list on the board. A doubly linked list is a data structure where every node has – data – a reference to previous node (prev) – a reference to next node (next) We maintain a head node and a tail node. The value of the next node for the last (tail) element of a linked list is null. The value of the previous node for the first (head) element of a linked list is also null. When you construct a new node, set the data and next and prev default to null