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

Lect 08

The document discusses integer vectors and linked lists. It describes the integer vector class and some of its methods like get, set, and position. It then talks about problems with fixed sized vectors and introduces linked lists as a dynamic solution. The integer list class is described as having the same public interface but a different internal implementation using linked nodes.

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 08

The document discusses integer vectors and linked lists. It describes the integer vector class and some of its methods like get, set, and position. It then talks about problems with fixed sized vectors and introduces linked lists as a dynamic solution. The integer list class is described as having the same public interface but a different internal implementation using linked nodes.

Uploaded by

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

Thought for the Day

Our greatest glory is not in never falling,


but in rising every time we fall.
– Confucius
The IntegerVector Class
• Vector
– Technical/mathematical term for a list

• Class diagram (incomplete):


IntegerVector
data, numElements
add, get, set, position,
remove, length
Accessing Vectors
• Need two methods (accessors):
– get
– set
public int get (int index)
{ if (index < 0 || index >= numElements)
throw new IndexOutOfBoundsException(…);
return data[index];
} // get

public void set (int index, int item)


{ // Check index is in range. . .
data[index] = item;
} // set
Client View: Accessing Vectors
int x = v1.get(1);
...
v1.set(0, -385);
...
v1.set(2, v2.get(0) + 3);
// Like: v1[2] = v2[0] + 3

x 22 v1 -385 22 42

v2 39
Searching a Vector
public int position (int item)
{ int k;

for (k = 0; k < numElements; k++)


if (data[k] == item)
break; // Leave for loop

if (k >= numElements) // item was not found


return -1;
else
return k;
} // position
Class Diagram

IntegerVector
data, numElements
add, get, set, position,
remove, length, toString
Uses of the IntegerVector
Class

• Any program that needs to work with lists


of integers
– Indexing: lists of line numbers

• Same principles hold for lists of other types


Problems with the
IntegerVector Class
• The size is fixed when a list is created
– Too small: the program will crash
– Too big: memory is wasted

• Solutions?
– Reallocate the data array (Exercise 4.9)
– Different implementation
Linked Lists
• Elegant solution to space problem
– Space is allocated as it is needed
– Each element has its own memory location
– These are linked together using references
(pointers)

• Dynamic Data Structure


– Easily changed
Recap: Reference Variables
• Refer to an object
• Can easily be changed to refer to another
object
String r = ”Hi”; r
String s = ”Bye”;
Hi
String t = r;
t = s; t

s Bye
New Class: IntegerList
• Same public methods as IntegerVector
– Clients will see (almost) no difference

• Very different “internal” implementation


Class Diagram

IntegerList
first, numElements
add, get, set, position,
remove, length, toString
Internal Details
public class IntegerList
{ private class ListNode
{ public int data; An Inner Class
public ListNode next;
} // class ListNode

private ListNode first; // Pointer to the


// first ListNode in an IntegerList
private int numElements; // Number of
// elements in an IntegerList
. . .
} // class IntegerList
The Inner Class ListNode
public class IntegerList
{ private class ListNode
{ public int data;
public ListNode next;
} // class ListNode
. . .
} // class IntegerList

• ListNode is private, so can only be used


inside IntegerList
• data and next are public, so can be
accessed outside ListNode
Structure of a ListNode Object
• We can picture a single ListNode object
as follows:
ListNode
data -56
next

private class ListNode


{ public int data;
public ListNode next;
} // class ListNode
A Linked List
• Made up of many ListNode objects
“linked” together by the next fields
ListNode
data -56 19 3 -2
next
Locating the First Element in the
List
• We use the first member of the
IntegerList class
IntegerList
first

numElements 4
ListNode
data -56 19 3 -2
next

You might also like