0% found this document useful (0 votes)
9 views

Lect 09

The document discusses an IntegerList class which implements a linked list to store integer values. It describes the structure of the class including a first node and number of elements field. It provides examples of adding nodes to the front of the list and inserting nodes at any position by traversing the linked list to find the correct insertion point.

Uploaded by

Arnold
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lect 09

The document discusses an IntegerList class which implements a linked list to store integer values. It describes the structure of the class including a first node and number of elements field. It provides examples of adding nodes to the front of the list and inserting nodes at any position by traversing the linked list to find the correct insertion point.

Uploaded by

Arnold
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Thought for the Day

Greatness is not in where we stand, but in


what direction we are moving. We must sail
sometimes with the wind and sometimes
against it — but sail we must and not drift,
nor lie at anchor.
– Oliver Wendell Holmes
New Class: IntegerList
• Same public methods as IntegerVector
– Clients will see almost no difference

• Very different “internal” implementation


– Linked list
– Dynamic data structure
Structure of the IntegerList
Class
IntegerList
first
numElements 4
ListNode
data -56 19 3 -2
next
Implementation of the Class
• Constructor
– Must create an “empty” list
public IntegerList ()
// Constructor
{ first = null;
numElements = 0;
} // constructor

IntegerList
first

numElements 0
Adding a New Element
• Simple Case: add as first element
• Need to
– Create a new list node
– Link it into the list
public void add (int item)
// Place the new item into a list
{ ListNode node = new ListNode();
node.data = item;
node.next = first;
first = node;
numElements++;
} // add
public void add (int item) Adding 17
{ ListNode node = new ListNode();
node.data = item;
node.next = first;
first = node;
numElements++;
} // add
IntegerList
node
first

numElements 32
ListNode
data 3 -8 170
next
More Flexibility: Adding Nodes
at Any Position
• Need to work through the linked list to find
the correct position
Finding the Position
public void add (int item, int position)
{ if (position < 0)
throw new …Exception(…);
ListNode node = new ListNode();
node.data = item;
ListNode curr = first,
prev = null;
for (int k = 0;
k < position && curr != null;
k++)
// Find position
{ prev = curr;
curr = curr.next;
}
...
Finding the Position (cont.)
add(-4, 2)
IntegerList
first Position for
new node
numElements 4
ListNode
data -56 19 3 -2
next

prev curr
Linking in the New Node

...
node.next = curr;
if (prev != null)
prev.next = node;
else
first = node;
numElements++;
} // add

You might also like