Implementing Lists Using Linked-Lists

Implementing Lists Using Linked-Lists - Implementing Lists...

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

View Full Document Right Arrow Icon
I MPLEMENTING L ISTS U SING L INKED -L ISTS Contents z Introduction z Java Types { Test Yourself #1 z Intro to Linked Lists { Test Yourself #2 z Linked List Operations { Adding a node ± Test Yourself #3 { Removing a node { Using a header node z The LinkedList Class { add (to end of list) ± Test Yourself #4 { add (at a given position) ± Test Yourself #5 { The LinkedList constructor ± Test Yourself #6 z Comparison: Lists via Arrays versus via Linked Lists { Test Yourself #7 z Linked List Variations { Doubly linked lists { Circular linked lists ± Test Yourself #8 ± Test Yourself #8 { Comparisons Introduction The previous set of notes discussed how to implement the List interface using an array to store the items in the list. Here we discuss how to implement the List interface using a linked list to store the items. However, before talking about linked lists, we will review the difference between primitive and non-primitive types in Java. Java Types Java has two "categories" of types: 1. primitive types: byte, short, int, long, float, double, boolean, and char Page 1 of 20 Implementing Lists Using Linked-Lists 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Linked-Lists/
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. reference types: arrays and classes When you declare a variable with a primitive type, you get enough space to hold a value of that type. Here's some code involving a primitive type, and the corresponding conceptual picture: When you declare a variable with a reference type, you get space for a reference (pointer) to that type, not for the type itself. You must use "new" to get space for the type itself. This is illustrated below. Remember that class objects are also reference types. For example, if you declare a variable of type List , you only get space for a pointer to a list; no actual list exists until you use "new". This is illustrated below, assuming the array implementation of lists, and assuming that the SimpleArrayList constructor initializes the items array to be of size 3. Note that because it is an array of Objects, each array element is (automatically) initialized to null (shown using a diagonal line in the picture). An important consequence of the fact that non-primitive types are really pointers is that assigning from one variable to another can cause aliasing (two different names refer to the same object). For example: Page 2 of 20 Implementing Lists Using Linked-Lists 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Linked-Lists/
Background image of page 2
Note that in this example, the assignment to b[1] changed not only that value, but also the value in a [1] (because a and b were pointing to the same array)! However, an assignment to b itself (not to an element of the array pointed to by b) has no effect on a: A similar situation arises when a non-primitive value is passed as an argument to a method. For example, consider the following four statements, and the definition of method changeArray : Page 3 of 20 Implementing Lists Using Linked-Lists 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Linked-Lists/
Background image of page 3

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

View Full DocumentRight Arrow Icon
The picture below illustrates what happens when this code executes. Note that the method call causes
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Implementing Lists Using Linked-Lists - Implementing Lists...

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

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