Unit02B - Lists Organizing Data Linearly Linked Lists...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Lists Organizing Data Linearly Linked Lists 2B 2 Disadvantages of Array Lists If a data entry is added to or removed from an array-based list, data needs to be shifted to update the list. In the worst case, for an array-based list with n data entries, an add and a remove takes O(n) time. Also, all data in the array-based list must be stored sequentially in memory. Large lists will require significant contiguous blocks of memory. 3 Singly Linked Lists Each data entry is stored in its own node . Each node has a reference to the node that contains the next data entry. A reference named the head points to the node with the first data entry. The last node in the list contains a reference of null since it does not point to any other data. An empty list would have a head reference equal to null. 4 Visualization null head a data element (type E) a node data next 5 Inner classes Since a node is specific to a linked list, we will define the Node class to be an inner class of the SinglyLinkedList class. The inner class is only accessible by the class that enclosed it. Fields defined in the inner class are accessible by its enclosing class. (No accessors are needed.) 6 Node class private static class Node<E> { private E data; private Node<E> next; private Node(E element) { data = element; next = null; } private Node(E element, Node<E> nodeRef) { data = element; next = nodeRef; } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 7 Initializing an empty linked list public class SinglyLinkedList<E> { private Node<E> head; private int numElements; public SinglyLinkedList() {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 6

Unit02B - Lists Organizing Data Linearly Linked Lists...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online