87 lines
1.9 KiB
Java
87 lines
1.9 KiB
Java
/**
|
|
* This is a class containing functios
|
|
* for the Elements in the list.
|
|
*
|
|
*
|
|
* @author Christian Ohlsson, Karlstad university 1999
|
|
* @version 1.0.0
|
|
*/
|
|
public class Element {
|
|
Element suc;
|
|
|
|
/**
|
|
* This is just the constructor.
|
|
*/
|
|
public Element() { suc = null; }
|
|
|
|
/**
|
|
* Returns the current Element in the list
|
|
*/
|
|
public Element suc() { return suc; }
|
|
|
|
/**
|
|
* This function inserts a new Element
|
|
* at the end of the List.
|
|
* It first checks out if the List is empty,
|
|
* i.e the first Element is null, and then
|
|
* the first Element is this Element.
|
|
* Otherwise, it inserts the new Element
|
|
* at the end of the List.
|
|
*/
|
|
public void into(List theList) {
|
|
if (theList.first==null)
|
|
theList.first = theList.last = this;
|
|
else {
|
|
theList.last.suc = this;
|
|
theList.last = this;
|
|
}
|
|
suc = null;
|
|
}
|
|
|
|
/**
|
|
* This function inserts a new Element at
|
|
* the beginning of the list.
|
|
* It first checks out if the List is empty,
|
|
* i.e the first Element is null, and then
|
|
* the first Element is this Element.
|
|
* Otherwise, the Element that were in the
|
|
* beginning, now is in second place and
|
|
* the new Element is in front of the List.
|
|
*/
|
|
public void intoAsFirst(List theList) {
|
|
if (theList.first==null) {
|
|
theList.first = theList.last = this;
|
|
suc = null;
|
|
}
|
|
else {
|
|
suc = theList.first;
|
|
theList.first = this;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This function deletes an Element in
|
|
* the List by traversing through the
|
|
* List until it gets to the Element
|
|
* that should be deleted.
|
|
*/
|
|
public void out(List theList) {
|
|
Element prev = null;
|
|
Element e = theList.first;
|
|
while (e!=null && e!=this) {
|
|
prev = e;
|
|
e = e.suc;
|
|
}
|
|
if (e!=null) {
|
|
if (theList.last==this)
|
|
theList.last = prev;
|
|
if (theList.first==this)
|
|
theList.first = suc;
|
|
else
|
|
prev.suc = suc;
|
|
suc = null;
|
|
}
|
|
}
|
|
}
|
|
|