[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Held Tuesday, November 14, 2000
Summary
Today we consider techniques for implementing the various linear structures.
Notes
Overview
public class ListBasedQueue
implements Queue
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
List implementation;
ListIterator helper;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
public ListBasedQueue() {
this.implementation = new WhateverKindOfListYouWant();
this.helper = implementation.getIterator();
} // ListBasedQueue()
// +---------+-------------------------------------------------
// | Methods |
// +---------+
public Object get() {
return this.implementation.deleteFirst();
}
public Object peek() {
this.helper.toFront();
return this.helper.getCurrent();
}
public void put(Object val) {
this.implementation.addToEnd(val);
}
public boolean isEmpty() {
return (this.implementation.length() == 0);
}
public boolean isFull() {
return this.implementation.isFull();
}
} // public class ListBasedQueue
public class ArrayBasedPriorityQueue
implements PriorityQueue
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/**
* All the values in the priority queue. There may be gaps
* in this array, in which case null represents "no element
* here".
*/
Object[] values;
/**
* The number of values in the priority queue.
*/
int size;
/**
* The comparator used to find the highest-priority (largest)
* value in the pqueue.
*/
Comparator order;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/**
* Create a new priority queue that orders values with a
* particular comparator and that holds up to capacity
* values.
*/
public ArrayBasedPriorityQueue(Comparator c; int capacity) {
this.order = c;
this.size = 0;
this.values = new Object[capacity];
// Note that no values are in the queue.
// The following is probably done automatically, but we do
// it ourselves just to be sure.
for (int i = 0; i < capacity; i++) {
this.values[i] = null;
}
} // ArrayBasedPriorityQueue(Comparator, int)
// +---------+-------------------------------------------------
// | Methods |
// +---------+
/** Is it full? */
public boolean isFull() {
return this.size = this.values.length;
}
/** Add something. */
public void put(Object val) {
for (int i = 0; i < this.values.length; i++) {
// Is the current position available?
if (this.values[i] == null) {
// If so, fill it in.
this.values[i] = val;
// And stop
return;
} // if
} // for
} // put
/** Get something. */
public void get() {
int maxindex = this.findMax();
Object returnMe = this.values[maxindex];
this.values[maxindex] = 0;
return returnMe;
}
// +---------+-------------------------------------------------
// | Helpers |
// +---------+
/**
* Find the index of the maximum value.
*/
public int findMax() {
int guess = -1;
// Step through all the locations
for (int i = 0; i < this.values.length; i++) {
if (this.values[i] != null) {
if (guess == -1) {
guess = i;
}
else if (order.compare(this.values[i], this.values[guess]) > 0) {
guess = i;
} // if values[i] > this.values[guess]
} // if the current value exists
} // for each location
return guess;
} // findMax()
} // ArrayBasedPriorityQueue
Wednesday, 23 August 2000
Thursday, 24 August 2000
Tuesday, 14 November 2000
Back to Introduction to Linear Structures. On to Priority Queues, Heaps, and Heap Sort.
[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
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.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Outlines/outline.43.html
Source text last modified Tue Nov 14 08:58:01 2000.
This page generated on Tue Nov 14 08:59:40 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu