71 lines
1.5 KiB
Java
71 lines
1.5 KiB
Java
/**
|
|
* This class contains the linked list.
|
|
* It also contains the constructor, wich
|
|
* generates a new list called myList
|
|
*
|
|
* @author Christian Ohlsson, Karlstad university 1999
|
|
* @version 1.0.0
|
|
*/
|
|
public class List {
|
|
Element first, last;
|
|
|
|
/**
|
|
* This is the constructor of the list.
|
|
* What it basically do is to set
|
|
* the two Elements constructed before
|
|
* to null.
|
|
*/
|
|
public List() { first = last = null; }
|
|
|
|
/**
|
|
* This function returns the first Element
|
|
* in the list.
|
|
*/
|
|
public Element first() { return first; }
|
|
|
|
/**
|
|
* This function returns the last Element
|
|
* in the list.
|
|
*/
|
|
public Element last() { return last; }
|
|
|
|
/**
|
|
* If the first element in the List is null,
|
|
* it returns true, that is, there's no Elements
|
|
* in the List.
|
|
*/
|
|
public boolean empty() { return first==null; }
|
|
|
|
/**
|
|
* This function recives a number and it traverses
|
|
* though the List until that number is reached
|
|
* and then returns the Element on that location
|
|
*/
|
|
public Element getElement(int number) {
|
|
Element e = first;
|
|
int antal = 0;
|
|
while (antal<number) {
|
|
antal++;
|
|
e = e.suc;
|
|
}
|
|
return e;
|
|
}
|
|
|
|
/**
|
|
* This function initialize a number to zero
|
|
* and the traverses though the List until
|
|
* it reaches the end and than returns
|
|
* the number of elements.
|
|
*/
|
|
public int cardinal() {
|
|
int antal = 0;
|
|
Element e = first;
|
|
while (e!=null) {
|
|
antal++;
|
|
e = e.suc;
|
|
}
|
|
return antal;
|
|
}
|
|
}
|
|
|