












































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture notes on lists in java, covering the use of arrays and dynamic data structures like linked lists. Topics include object references, arrays of objects, resizable arrays, and implementing linked lists using the node class. Learn about creating, inserting, deleting, and traversing nodes in a linked list.
Typology: Study notes
1 / 84
This page cannot be seen from the preview
Don't miss anything!













































































Storing a list in a dynamic data structure (Linked List) (^) The Linked List is stored as a sequence of linked nodes. As in the case of the stack, each node in a linked list contains data AND a reference to the next node. The Linked List has the following properties: (^) The list can grow and shrink as needed. (^) The position of each element is given by an index from 0 to n-1, where n is the number of elements. (^) Given any index, the time taken to access an element with that index depends on the index. This is because each element of the list must be traversed until the required index is found. (^) The time taken to add an element at any point in the list does not depend on the size of the list, as no shifts are required. It does, however, depend on the index. Additions near the end of the list take longer than additions near the middle or start. The same applies to the time taken to remove an element.
a) A linked list of integers; b) insertion; c) deletion
A reference to an Integer object
(^) When one reference variable is assigned to another reference variable, both references then refer to the same object. Integer p, q; p = new Integer(6); q = p; (^) A reference variable that no longer references any object is marked for garbage collection.
e) allocating an object; f) assigning null to a reference variable; g) assigning a reference with a null value
(^) An array of objects (^) Is actually an array of references to the objects (^) Example Integer[] scores = new Integer[30]; (^) Instantiating Integer objects for each array reference scores[0] = new Integer(7); scores[1] = new Integer(9); // and so on …
(^) The number of references in a Java array is of fixed size (^) Resizable array (^) An array that grows and shrinks as the program executes (^) java.util.Vector class (^) Uses a similar technique to implement a grow able array of objects
(^) Linked list (^) Contains nodes that are linked to one another (^) A node (^) Contains both data and a “link” to the next item (^) Can be implemented as an object (^) Example: public class Node { private Object item; private Node next; // constructors, accessors, // getter and setter methods.. } // end class Node Figure 5 A node
Node Class and its Methods (^) Using the Node class Node n = new Node (new Integer(6)); Node first = new Node (new Integer(9), n);
Using the Node constructor to initialize a data field and a link value
Head Reference in Linked Lists (^) Data field next in the last node is set to null (^) head reference variable (^) References the list’s first node (^) Always exists even when the list is empty
A head reference to a linked list
Programming with Linked Lists: Displaying the Contents of a Linked List (^) curr reference variable (^) References the current node (^) Initially references the first node (^) To display the data portion of the current node System.out.println(curr.getItem()); (^) To advance the current position to the next node curr = curr.getNext();
Displaying the Contents of a Linked List
The effect of the assignment curr = curr.getNext( )