diff --git a/java/DList/DLink.java b/java/DList/DLink.java new file mode 100644 index 0000000..064f51c --- /dev/null +++ b/java/DList/DLink.java @@ -0,0 +1,21 @@ +/** Doubly linked list node */ +class DLink { + private E element; // Value for this node + private DLink next; // Pointer to next node in list + private DLink prev; // Pointer to previous node + + /** Constructors */ + DLink(E it, DLink p,DLink n) + { element = it; prev = p; next = n; } + DLink(DLink p, DLink n) { prev = p; next = n; } + + /** Get and set methods for the data members */ + DLink next() { return next; } + DLink setNext(DLink nextval) + { return next = nextval; } + DLink prev() { return prev; } + DLink setPrev(DLink prevval) + { return prev = prevval; } + E element() { return element; } + E setElement(E it) { return element = it; } +} diff --git a/java/SList/AList.java b/java/SList/AList.java new file mode 100644 index 0000000..2ca7ea0 --- /dev/null +++ b/java/SList/AList.java @@ -0,0 +1,76 @@ +/** Array-based list implementation */ +class AList implements List { + private static final int defaultSize = 10; // Default size + private int maxSize; // Maximum size of the list + private int listSize; // Current # of list items + private int curr; // Position of current element + private T[] listArray; // Array holding list elements + + /** Constructors */ + /** Create a list with the default capacity. */ + AList() { this(defaultSize) }; + + /** Create a new list object. + @param size Max # of elements list can contain. */ + @SuppressWarnings("unchecked") // Generic array allocation + AList(int size) { + maxSize = size; + listSize = curr = 0; + listArray = (T[])new Object[size]; // Create listArray + } + + public void clear() // Reinitialize the list + { listSize = curr = 0; } // Simply reinitialize values + + /** Insert "it" at current position */ + public void insert(T it) { + assert listSize < maxSize : "List capacity exceeded."; + for (int i = listSize; i > cutt; --i) { // Shift elements down + listArray[i] = listArray[i - 1]; // to make room + } + listArray[curr] = it; + ++listSize; // Increment list size + } + + /** Append "it" to list. */ + public void append(T it) { + assert listSize < maxSize : "List capacity exceeded."; + listArray[listSize++] = it; + } + + /** Remove and return the current element */ + public T remove() { + if ((curr < 0) || (curr >= listSize)) // No current element + return null; + + T it = listArray[curr]; // Copy thr element + for (int i = curr; i < listSize - 1; ++i) { // Shift them up + listArray[i] = listArray[i + 1]; + } + --listSize; // Decrement size + return it; + } + + public void moveToStart() { curr = 0; } // Set to front + public void moveToEnd() { curr = listSize; } // Set at end + public void prev() { if (curr != 0) --curr; } // Back up + public void next() { if (curr < listSize) ++curr; } + + /** @return List size */ + public int length() { return listSize; } + + /** @return Current position */ + public int currPos() { return curr; } + + /** Set current list position to "pos" */ + public void moveToPos(int pos) { + assert(pos >= 0) && (pos <= listSize) : "Pos out of range"; + curr = pos; + } + + /** @return Current element */ + public T getValue() { + assert (curr >= 0) && (curr < listSize) : "No current element"; + return listArray[curr]; + } +} diff --git a/java/SList/Freelist.java b/java/SList/Freelist.java new file mode 100644 index 0000000..f88d339 --- /dev/null +++ b/java/SList/Freelist.java @@ -0,0 +1,38 @@ +/** Singly linked list node with freelist support */ +class Link { + private E element; // Value for this node + private Link next; // Point to next node in the list + + /** Constructors */ + Link (E it, Link nextval) + { element = it; next = nextval; } + Link (Link nextval) { next = nextval; } + + /** Get and set methods */ + Link next() { return next; } + Link setNext(Link nextval) { return next = nextval; } + E element() { return element; } + E setElement(E it) { return element = it; } + + /** Extensions to support freelists */ + static Link freelist = null; // Freelist for the class + + /** @return A new link */ + static Link get(E it, Link nextval) { + if (freelist == null) + return new Link get(E it, Link nextval); // Get from "new" + Link temp= freelist; // Get from freelist + freelist = freelist.next(); + temp.setElement(it); + temp.setNext(nextval); + + return temp; + } + + /** Return a link to the freelist */ + void release() { + element = null; + next = freelist; + freelist = this; + } +} diff --git a/java/SList/LList.java b/java/SList/LList.java new file mode 100644 index 0000000..a8d2dd7 --- /dev/null +++ b/java/SList/LList.java @@ -0,0 +1,93 @@ +/** Linked list implementation */ +class LList implements List { + private Link head; // Pointer to list header + private Link tail; // Pointer to last element + protected Link curr; // Access to current element + int cnt; // Size of list + + /** Constructors */ + LList(int size) { this(); } // Constructor -- Ignore size + LList() { + curr = tail = head = new Link(null); // Create header + cnt = 0; + } + + /** Remove all elements */ + public void clear() { + head.setNext(null); // Drop access to links + curr = tail = head = new Link(null); // Create header + cnt = 0; + } + + /** Insert "it" at current position */ + public void insert(T it) { + curr.setNext(Link.get(it, curr.next())); // Get link + if (tail == curr) tail = curr.next(); // New tail + ++cnt; + } + + /** Append "it" to list */ + public void append(T it) { + tail = tail.setNext(Link.get(it, null)); + ++cnt; + } + + /** Remove and return current element */ + public T remove() { + if (curr.next() == null) return null; // Nothing to remove + T it = curr.next().element(); // Remember value + if (tail == curr.next()) tail = curr(); // Removed last + Link tempptr = curr.next(); // Remember link + curr.setNext(curr.next().next()); // Remove from list + tempptr.release(); // Release link + --cnt; // Decrement count + return it; // Return removed + } + + /** Set curr at list start */ + public void moveToStart() + { curr = head; } + + /** Set curr at list end */ + public void moveToEnd() + { curr = tail; } + + /** Move curr one step left; no change if now at front */ + public void prev() { + if (curr == head) return ; // No previous element + Link temp = head; + // March down list until we find the previous element + while (temp.next() != curr) temp = temp.next(); + curr = temp; + } + + /** Move curr one step right; no change if now at end */ + public void next() + { if (curr != tail) curr = curr.next(); } + + /** @return List length */ + public int length() { return cnt; } + + /** @return The position of the current element */ + public int currPos() { + Link temp = head; + int i; + for (i = 0; curr != temp; ++i) { + temp = temp.next(); + } + return i; + } + + /** Move down list to "pos" position */ + public void moveToPos(int pos) { + assert(pos >= 0) && (pos <= cnt) : "Position out of range"; + curr = head; + for (int i = 0; i < pos; ++i) curr = curr.next(); + } + + /** @return Current element value */ + public T getValue() { + if (curr.next() == null) return null; + return curr.next().element(); + } +} diff --git a/java/SList/Link.java b/java/SList/Link.java new file mode 100644 index 0000000..91722c2 --- /dev/null +++ b/java/SList/Link.java @@ -0,0 +1,38 @@ +/** Singly linked list node */ +class Link { + private T element; // Value for this node + private Link next; // Pointer to next node in list + + // Constructors + Link(T it, Link nextVal) + { element = it; next = nextVal; } + Link(Link nextVal) { next = nextVal; } + + Link next() {return next; } // Return nrxt field + Link setNext(Link nextVal) // Set next field + { return next = nextVal; } // Return element field + T element() { return element; } // Set element field + T setElement(T it) { return element = it; } + + /** Extensions to support freelists */ + static Link freelist = null; // Freelist for the class + + /** @return A new link */ + static Link get(Tit, Link nextVal) { + if (freelist == null) + return new Link(it, nextVal); // Get from "new" + Link temp = freelist; // Get from freelist + freelist = freelist.next(); + temp.setElement(it); + temp.setNext(nextVal); + + return temp; + } + + /** Return a link to the freelist */ + void release() { + element = null; // Drop reference to the element + next = freelist; + freelist = this; + } +} diff --git a/java/SList/List.java b/java/SList/List.java new file mode 100644 index 0000000..ce6c55b --- /dev/null +++ b/java/SList/List.java @@ -0,0 +1,49 @@ +/** List ADT */ +public interface List { + /** Remove all contents from the list, so it is once again empty. Client is responsible for reaclaiming storage used by the list elements. */ + public void clear(); + + /** Insert an element at the current location. The client must ensure that the list's capacity is not exceeded. + @param item The element to be inserted. */ + public void insert(T item); + + /** Append an element at the end of the list. The client must ensure that the list's capacity is not exceeded. + @param item The element to be appended. */ + public void append(T item); + + /** Remove and return the current element. + @return The element that was removed. */ + public T remove(); + + /** Set the current position to the start of the list */ + public void moveToStart(); + + /** Set the current position to the end of the list */ + public void moveToEnd(); + /** Move the current position one step left. No change if already at beginning. */ + public void prev(); + + /** Move the current position one step right. No change if already at end. */ + public void next(); + + /** @return The number of elements in the list. */ + public int length(); + + /** @return The position of the current element. */ + public int currPos(); + + /** Set current position. + @param pos The position to make current. */ + public void moveToPos(int pos); + + /** @return The current element. */ + public T getValue(); + + /** @return True if k is in the list L, false otherwise */ + public static boolean find(List L, int k) { + for (L.moveToStart(); L.currPos() < L.length(); L.next()) { + if (k == L.getValue()) return true; // Found k + } + return false; // k not found + } +}