Webber: A Website Construction Application


LinkList.java


// An implementation of a linked-list structure

package library;

import java.util.*;

public class LinkList {
	private int elementCount;			// Number of elements in list
	private Link elementData;			// Root link for object list

	// Constructor
	public LinkList() { removeAllElements(); }

	// Return the number of elements in this list
	public final int size() { return elementCount; }

	// Return the root link of this object list
	final Link objects() { return elementData; }

	// Return the link at index in this list
	private Link linkAt(int index) {
		Link l;
		for (l=elementData; index>0; --index) l = l.getLink();
		return l;
	}

	// Return the object at index in this list
	public final synchronized Object elementAt(int index) {
		return ((index < 0) || (index >= elementCount)) ?
			null : linkAt(index).getObject();
	}

	// Add an element containing object at the specified position in this list
	public final synchronized void addElementAt(Object object, int index) {
		if ((index < 0) || (index > elementCount)) return;
		Link l = new Link(object);		  // Create new object link
		if (index==0) {				  // New list-head
			l.setLink(elementData);		  // Move head into link
			elementData = l;		  // Make link into head
		} else {				  // New list-tail member
			if (index < elementCount)	  // Any following elements?
				l.setLink(linkAt(index)); // Point object at next
			linkAt(index-1).setLink(l);	  // Point tail at object
		}
		++elementCount;				  // Increment element count
	}

	// Add an element containing object to the end of this list
	public final synchronized void addElement(Object object) {
		addElementAt(object, elementCount);
	}

	// Merge another LinkList into this list at the specified index
	public final synchronized void addLinksAt(LinkList list, int index) {
		int size = list.size();
		if (size < 1) return;			// New list is empty?
		if ((index < 0) || (index > elementCount)) return;
		if (index==0) {				// Add to start of list
			list.linkAt(size - 1).setLink(elementData);
			elementData = list.objects();
		} else if (index==elementCount) {	// Add to end of list
			linkAt(elementCount - 1).setLink(list.objects());
		} else {				// Insert into list
			list.linkAt(size - 1).setLink(linkAt(index));
			linkAt(index - 1).setLink(list.objects());
		}
		elementCount += size;			// Set total list size
	}

	// Remove the element at index in this list
	public final synchronized void removeElementAt(int index) {
		if ((index < 0) || (index>=elementCount)) return;
		if (index==0) elementData = elementData.getLink();
		else {
			Link l = linkAt(index-1);
			l.setLink(l.getLink().getLink());
		}
		--elementCount;
	}

	// Remove the first element containing object from this list
	public final synchronized void removeElement(Object object) {
		Enumeration e = elements();
		for (int i=0; e.hasMoreElements(); ++i)
			if (e.nextElement()==object) {
				removeElementAt(i);
				break;
			}
	}

	// Remove num links from this list at position index
	public final synchronized void removeLinksAt(int index, int num) {
		if ((index < 0) || (index+num > elementCount)) return;
		if (index==0) elementData = linkAt(num);
		else linkAt(index-1).setLink(linkAt(index+num));
		elementCount -= num;
	}

	// Remove all elements from this list
	public final synchronized void removeAllElements() { 
		elementCount = 0;	elementData = null;
	}

	// Returns an enumeration of the elements of this list
	public final synchronized Enumeration elements() {
		return new LinkEnumeration(this);
	}


//	***  Nested Classes  ***

// Object-link class used by LinkList objects
private static class Link {
	private Link link;				// Next link in the chain
	private Object object;				// Object contained by link

	// Constructors
	public Link() { this(null); }
	public Link(Object object) { link = null; this.object = object; }

	// State Query
	public Link getLink() { return link; }
	public Object getObject() { return object; }

	// State Change
	public void setLink(Link link) { this.link = link; }
	public void setObject(Object object) { this.object = object; }
}

// Enumeration implementation for LinkList objects
private static class LinkEnumeration implements Enumeration {
	private int elementCount;
	private Link nextElement;

	// Constructor
	public LinkEnumeration(LinkList list) {
		elementCount = list.size();
		nextElement = list.objects();
	}

	// Returns true if this enumeration has any more elements
	public boolean hasMoreElements() { return (nextElement!=null); }

	// Returns the object contained by the next element
	public Object nextElement() {
		Object object;
		if (nextElement!=null) {
			object = nextElement.getObject();
			nextElement = nextElement.getLink();
		} else object = null;
		return object;
	}
}
}

Go To: Source Code