# Class 28: Linked Lists

Back to Vectors. On to Lists, Concluded.

Held Friday, March 12

Summary

• Selecting a core set of list operations
• Lists in Java vs. Lists in Scheme
• Lists vs. nodes
• Dynamic-sized lists.
• Supporting additional list operations.
• Reading: Java Plus Data Structures, Chapter 6
• Reading: Java Plus Data Structures, Section 8.3
• Exam 2 distributed. Due Friday, March 19.

Contents

Handouts

Notes

• Some of you have failed to note that I have made an answer key available for the first exam. I do my best to create answer keys for every assignment and exam.
• I was sorry to see so few of you at convocation yesterday. It was pretty good, and focused on applications of Fibonacci numbers.

## Implementing Sorted Lists

• While there are a number of ways to implement sorted lists with arrays, it turns out to be easiest to implement sorted lists with sorted arrays.
• You could use other techniques; we may see others in the future.
• How do you specify the sort order?
• The individual objects could provide it.
• The sort order could be specified with the constructor.

### Comparable Objects

• One option in building sorted lists is to permit objects to compare themselves to each other. To do so, we'll need to create an appropriate `Comparable` interface>
```/**
* Objects that can sometimes be compared to each other.
*
* @author Samuel A. Rebelsky
* @aversion 1.0 of March 1999
*/
public interface Comparable {
/**
* Determine if this object is ``less than'' another object.
* If the other object can't be compared to this one, then
* throws an exception.
*/
public boolean lessThan(Comparable other)
throws Exception;

/**
* Determine if this object is equal to another object.
*/
} // interface Comparable
```
• We can then use this to develop particular kinds of comparable objects. For example, here is an extension to our `Fraction` class to support comparision of fractions.
```public class Fraction
implements Comparable
{
...
/**
* Determine if this fraction is less than another fraction.
* Precondition: Neither fraction has a denom. of 0.
* Precondition: Both fractions are simplified.
* Postcondition: Returns true if this fraction is smaller;
*   returns false otherwise.
*/
public boolean lessThan(Fraction other) {
// a/b < c/d if a*d < c*b.  Why? because (1) a/b = a*d/b*d,
// c/d = c*b/b*d.  Since the denominators are now the same,
// we need only compare numerators.
return this.num * other.denom < other.num * this.denom;
} // lessThan(Fraction)

/**
* Determine if this fraction is less than another comparable
* object.  Only successful if the other object is a Fraction.
* Otherwise, throws an exception.
* Precondition: This fraction does not have a denominator of 0.
* Precondition: The fraction is simplified.
* Precondition: If the parameter is a Fraction, then it has
*   a nonzero demoninator and is simplified.
* Postcondition: If the parameter is a Fraction, and this Fraction
*   is smaller than the parameter, returns true.  If the parameter
*   is a Fraction, and this Fraction is not smaller than the parameter,
*   returns false.  Otherwise, throws an exception.
*/
public boolean lessThan(Comparable other)
throws Exception
{
if (other instanceof Fraction) {
return lessThan((Fraction) other);
}
else
throw new Exception();
} // lessThan(Comparable)
...
} // class Fraction
```
• Why do we use the exceptions? So that it's clear when two objects can't be compared (so that we don't assume that just because x is not less than y, and x is not equal to y, then x is greater than y).

### Comparators

• Another option (and perhaps a more general option), is to create objects that can be used to compare pairs of other objects.
• That way, we can use different comparisons to acheive different sort orders.
• E.g., we could sort students by id or name.
• Here's a class. (It's a class, rather than an interface, because we want to define one of the methods.
```/**
* Things that know how to compare other things.
*
* @author Samuel A. Rebelsky
* @version 1.0 of March 1999
*/
public class Comparator {
/** Compare two objects for equality. */
public boolean equals(Object alpha, Object beta) {
return alpha.equals(beta);
} // equals(Object,Object)

/**
* See if one object is less than another.  Throws an
* exception if they can't be compared.
*/
public boolean lessThan(Object left, Object right)
throws Exception;
{
// Assume we can't compare them.
throw new Exception("Can't compare " + left.toString() +
" and " + right.toString());
} // lessThan(Object,Object)
} // class Comparator
```
• Here's a comparator for fractions.
```/**
* Something that can compare two fractions.
*/
public class FractionComparator
implements Comparator
{
/**
* Is one fraction less than another?
*/
public boolean lessThan(Fraction left, Fraction right) {
return left.num*right.denom < right.num*left.denom;
} // lessThan(Fraction,Fraction)

/**
* Is one object less than another?
*/
public boolean lessThan(Object left, Object right)
throws Exception
{
if ((left instanceof Fraction) && (right instanceof Fraction)) {
return lessThan((Fraction) left, (Fraction) right);
}
else {
return super(left,right);
}
} lessThan(Object,Object)
} // class FractionComparator
```

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Added some details on Thursday, March 11, 1999. Most were taken from outline 26 (and were not covered in that class).
• Added more details on Friday, March 12, 1999. A few were taken from outline 27 of CS152 98S and then modified. The discussion of comparable objects and comparators is new.
• Removed uncovered topics on Sunday, March 14, 1999.

Back to Vectors. On to Lists, Concluded.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.28.html

Source text last modified Sun Mar 14 13:55:46 1999.

This page generated on Sun Mar 14 14:29:13 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at rebelsky@math.grin.edu