CS 1723,
A List of ints
Here are an Interface and two class files.
At the end comes a class with a main method.
// List: a simple list of ints
public interface List {
// isEmpty: is the list empty? (initially true)
public boolean isEmpty();
// isFull: is the list full? (initially false)
public boolean isFull();
// add: add x to the list. No check for duplicates
public boolean add(int x);
// contains: is x in the list?
public boolean contains(int x);
// size: return number of elements in the list
public int size();
// remove: remove the parameter
public boolean remove(int x);
// clear: make the list empty
public void clear();
// removeMin: return the minimum and remove it
public int removeMin();
// removeMax: return the minimum and remove it
public int removeMax();
}
// IntList: a simple list of ints
public class IntList implements List{
// fields
private int[] listArray; // the actual list
private int currentSize; // current number of elements
private final int INITIAL_SIZE = 6; // initial maximum size
// IntList: constructor
public IntList () {
currentSize = 0; // not needed (already done)
listArray = new int[INITIAL_SIZE]; // allocate array
}
// isEmpty: is the list empty? (initially true)
public boolean isEmpty() {
return size() == 0;
}
// isFull: is the list full? (initially false)
public boolean isFull() {
return size() == listArray.length;
}
// add: add x to the list. No check for duplicates
public boolean add(int x) {
if (currentSize >= listArray.length)
doubleArray();
listArray[currentSize++] = x;
return true;
}
// contains: is x in the list?
public boolean contains(int x) {
for (int i = 0; i < currentSize; i++)
if (listArray[i] == x) return true;
return false;
}
// size: return number of elements in the list
public int size() {
return currentSize;
}
// remove: remove the parameter
public boolean remove(int x) {
for (int i = 0; i < currentSize; i++)
if (listArray[i] == x) {
for (int j = i; j < currentSize - 1; j++)
listArray[j] = listArray[j+1];
currentSize--;
return true;
}
return false;
}
// clear: make the list empty
public void clear() {
currentSize = 0;
}
// removeMin: return the minimum and remove it
public int removeMin() {
if (size() == 0) return 0;
int min = listArray[0];
for (int i = 1; i < currentSize; i++)
if (listArray[i] < min) min = listArray[i];
remove(min);
return min;
}
// removeMax: return the maximum and remove it
public int removeMax() {
if (size() == 0) return 0;
int max = listArray[0];
for (int i = 1; i < currentSize; i++)
if (listArray[i] > max) max = listArray[i];
remove(max);
return max;
}
// doubleArray: double the size of the array
private void doubleArray() {
int[] tempArray = new int[listArray.length * 2];
for(int k = 0; k < currentSize; k++)
tempArray[k] = listArray[k];
listArray = tempArray;
}
// dump: print data for debugging
public void dump() {
System.out.println("Dump of List, Current size: " + size() +
", Maximum size: " + listArray.length +
", Empty? " + isEmpty() +
", Full? " + isFull());
for (int i = 0; i < currentSize; i++)
System.out.print(" " + listArray[i]);
System.out.println(" End of Dump\n");
}
}
// GetNext: fetch next char or unsigned integer
import java.io.*;
public class GetNext {
private Reader in; // internal file name for input stream
// GetNext: constructor
public GetNext () {
in = new InputStreamReader(System.in);
}
// getNextChar: fetches next char
private char getNextChar() {
char ch = ' '; // = ' ' to keep compiler happy
try {
ch = (char)in.read();
} catch (IOException e) {
System.out.println("Exception reading character");
}
return ch;
}
// getNextInt: fetch unsigned int
public int getNextInt() {
String s ;
char ch = getNextChar();
while (ch == ' ' || ch == '\n')
ch = getNextChar();
s = "" + ch;
while (Character.isDigit(ch = getNextChar()))
s += ch;
return Integer.parseInt(s);
}
}
Finally, here is a main method, along
with a sample runs:
// ListMain: make use of the IntList class
public class ListMain {
public static void main(String[] args) {
IntList list1 = new IntList();
IntList list2 = new IntList();
GetNext getNext = new GetNext();
int num;
while(true) {
num = getNext.getNextInt();
if (num == 0) break;
list1.add(num);
list2.add(num);
}
list1.dump();
while (!list1.isEmpty())
System.out.print(list1.removeMin() + " ");
System.out.println();
while (!list2.isEmpty())
System.out.print(list2.removeMax() + " ");
System.out.println();
}
}
% java IntListMain
34 26 89 74 15 58 42 66 0
Dump of List, Current size: 8, Maximum size: 12, Empty? false, Full? false
34 26 89 74 15 58 42 66 End of Dump
15 26 34 42 58 66 74 89
89 74 66 58 42 34 26 15
Revision date: 2001-09-02.
(Please use ISO 8601,
the International Standard.)