Java Lists: Arrays, Linked Lists, and References - Prof. Debopam Acharya, Study notes of Data Structures and Algorithms

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

Pre 2010

Uploaded on 11/02/2009

eagleboy-1
eagleboy-1 🇺🇸

19 documents

1 / 84

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lists
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54

Partial preview of the text

Download Java Lists: Arrays, Linked Lists, and References - Prof. Debopam Acharya and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Lists

Preliminaries

 A list is a collection of items accessible one after another beginning

at the head and ending at the tail.

 It is a widely used data structure for applications which do not need

random access.

 It differs from the stack and queue data structures in that additions

and removals can be made at any position in the list.

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.

Preliminaries

Figure 1 Figure 1

a) A linked list of integers; b) insertion; c) deletion

Review of Object References

Figure 2 Figure 2

A reference to an Integer object

Review of Object References

 (^) 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.

Figure 4 Figure 4

e) allocating an object; f) assigning null to a reference variable; g) assigning a reference with a null value

Review of Object References

 (^) 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 …

Review of Object References

Resizable Arrays

 (^) 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

Reference-Based Linked Lists

 (^) 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);

Figure 6 Figure 6

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

Figure 7 Figure 7

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

Figure 9 Figure 9

The effect of the assignment curr = curr.getNext( )