Classes and their Members

A constructor is designed to provide initial values to a class’ data. It initializes instance variables.

Methods

A method does computations that access the classes’ instance variables. Classes tend to have two kinds of methods.

  1. Accessor methods. Give information about an object without altering the object.
  2. Modification methods. May change the status of an object.

Creating Objects

Once you have created the class (constructor, instance variables and methods) you may create objects of this class.

Use the word "new", and give it a name

ObjectClass objectName = new ObjectClass(arguments);

Calling a method always involves 4 steps.

  1. Name of the newly created object. This will be the reference to it.
  2. Place a single period.
  3. Name of the method.
  4. Parameter list.

When a program has several objects of the same type, each object has its own copies of the instance variables.

 

Linked lists (our text page 70 – 90)

Again, lets take a look at memory. It is like a pool.

Look at the array, once again.

Look at the vector

Linked lists have

 

Building and Manipulating linked lists

Null Reference

The null reference and linked lists

Null Pointer Exception

Null Pointer exception in linked lists.

In both cases the result will be a NullPointerException.

Page 70 in the text.


The Node Class:

The following are examples given by your book, however the book assumes that the Node Class and the List class are in the same JAVA file. In our class examples/exercises we won't assume this. You won't do this for your homework either. However these examples from the book are very understandable and make it easier for the student to understand linked lists for the first time.

Constructor

Public Node(classOfTheObject initialData, Node initialLink)

initialData contains the initial data of this new node

initialLink is a reference to the node after this new node. The reference may be null to indicate that there is no node after this node.

Special names in this methods:

Because the link refers to a node, we can also use the names link.data and link.link.

link.data means: Go to the node that links refers to and use the data instance variable.

Link.link means: Go to the node that link refers to and use the link instance variable.

getData()

Accessor method to get the data of this node.

getLink()

Accessor method to get a reference to the next node after this node.

setData()

Modification method to set the data in this node.

setLink();

Modification made to set the reference to the next node after this node.


Adding a New Node at the Head of the Linked List

Suppose that the head is the head reference of a linked list. Then this statement adds a new node at the front of the list with the specified data:

head = new Node(newData, head);

This statement works correctly even if we start with an empty list(in which case the reference is null) Explain with drawings.

Removing a Node from the head of a Linked List

Suppose that head is the head reference of a linked list. Then this statement removes a node from the front of the list:

head = head.getLink();

This statement works correctly even when the list has just one node (in which case the head reference becomes null).

 

Adding a new node that is not the head

Suppose that selection is a reference to a node of a linked list. Activating the following method adds a new node after the cursor node with element as the new data:

selection.addNodeAfter(element);

The implementation of addNodeAfter needs only one statement to accomplish its work:

link = new Node(element, link);

 

Removing a Node that is not the head

Suppose that selection is a reference to a node of a linked list. Activating the following method removes the node after the selection node:

Selection.removeNodeAfter();

The implementation of removeNodeAfter needs only one statement to accomplish its work.

link = link.link;

 

Watch out for the possible NullPointerException with removeNodeAfter. This may happen if the tail node activates removeNodeAfter method. How can we solve this?

Things that we can do with linked lists: