java list

This commit is contained in:
Aditya 2022-09-12 18:47:38 +05:30
parent c050979213
commit a9e63cfca3
6 changed files with 315 additions and 0 deletions

21
java/DList/DLink.java Normal file
View file

@ -0,0 +1,21 @@
/** Doubly linked list node */
class DLink<E> {
private E element; // Value for this node
private DLink<E> next; // Pointer to next node in list
private DLink<E> prev; // Pointer to previous node
/** Constructors */
DLink(E it, DLink<E> p,DLink<E> n)
{ element = it; prev = p; next = n; }
DLink(DLink<E> p, DLink<E> n) { prev = p; next = n; }
/** Get and set methods for the data members */
DLink<E> next() { return next; }
DLink<E> setNext(DLink<E> nextval)
{ return next = nextval; }
DLink<E> prev() { return prev; }
DLink<E> setPrev(DLink<E> prevval)
{ return prev = prevval; }
E element() { return element; }
E setElement(E it) { return element = it; }
}

76
java/SList/AList.java Normal file
View file

@ -0,0 +1,76 @@
/** Array-based list implementation */
class AList<T> implements List<T> {
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];
}
}

38
java/SList/Freelist.java Normal file
View file

@ -0,0 +1,38 @@
/** Singly linked list node with freelist support */
class Link<E> {
private E element; // Value for this node
private Link<E> next; // Point to next node in the list
/** Constructors */
Link (E it, Link<E> nextval)
{ element = it; next = nextval; }
Link (Link<E> nextval) { next = nextval; }
/** Get and set methods */
Link<E> next() { return next; }
Link<E> setNext(Link<E> 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 <E> Link<E> get(E it, Link<E> nextval) {
if (freelist == null)
return new Link<E> get(E it, Link<E> nextval); // Get from "new"
Link<E> 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;
}
}

93
java/SList/LList.java Normal file
View file

@ -0,0 +1,93 @@
/** Linked list implementation */
class LList<T> implements List<T> {
private Link<T> head; // Pointer to list header
private Link<T> tail; // Pointer to last element
protected Link<T> curr; // Access to current element
int cnt; // Size of list
/** Constructors */
LList(int size) { this(); } // Constructor -- Ignore size
LList() {
curr = tail = head = new Link<T>(null); // Create header
cnt = 0;
}
/** Remove all elements */
public void clear() {
head.setNext(null); // Drop access to links
curr = tail = head = new Link<E>(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<T> 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<T> 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<T> 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();
}
}

38
java/SList/Link.java Normal file
View file

@ -0,0 +1,38 @@
/** Singly linked list node */
class Link<T> {
private T element; // Value for this node
private Link<T> next; // Pointer to next node in list
// Constructors
Link(T it, Link<T> nextVal)
{ element = it; next = nextVal; }
Link(Link<T> nextVal) { next = nextVal; }
Link<T> next() {return next; } // Return nrxt field
Link<T> setNext(Link<T> 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 <T> Link<T> get(Tit, Link<T> nextVal) {
if (freelist == null)
return new Link<T>(it, nextVal); // Get from "new"
Link<T> 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;
}
}

49
java/SList/List.java Normal file
View file

@ -0,0 +1,49 @@
/** List ADT */
public interface List<T> {
/** 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<Integer> 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
}
}