📄 lvector.java
字号:
/*
Netwar
Copyright (C) 2002 Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
This file is part of Netwar.
Netwar is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Netwar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Netwar; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package netwar.utils;
/** Linked list version of Vector
* @author Kyle Kakligian
*/
public class LVector implements Vector {
private Node head; // Pointer to list header
private Node tail; // Pointer to last Object in list
protected Node curr; // Pointer to current Object
/** Constructs an <CODE>LVector</CODE>.
* @param sz The size of the new list.
* @see #resize resize()
*/
public LVector(int sz) { setup(); } // Constructor -- Ignore sz
/** Constructs an <CODE>LVector</CODE>.
*/
public LVector() { setup(); } // Constructor
private void setup() // Do initialization
{ tail = head = curr = new Node(null); } // Create header node
/** Remove all Objects from the list.
*/
public void clear() { // Remove all Objects from list
head.setNext(null); // Drop access to rest of links
curr = tail = head; // Reinitialize
}
/** Insert an Object at the current position.
* @param it Object to insert.
*/
public void insert(Object it) {
curr.setNext(new Node(it, curr.next()));
if (tail == curr) // Appended new Object
tail = curr.next();
}
/** Insert an Object at the tail of the list.
* @param it Object to append.
*/
public void append(Object it) {
tail.setNext(new Node(it, null));
tail = tail.next();
}
/** Removes and returns the current Object of the list.
* @return Object just removed.
*/
public Object remove() { // Remove and return current Object
if (!isInList()) return null;
Object it = curr.next().element(); // Remember value
if (tail == curr.next()) tail = curr; // Removed last: set tail
curr.setNext(curr.next().next()); // Remove from list
return it; // Return value removed
}
/** Sets the current Object to the first one in the list.
*/
public void goFirst() // Set curr to first position
{ curr = head; }
/** Sets the current Object to the next one in the list.
*/
public void goNext() // Move curr to next position
{ if (curr != null) curr = curr.next(); }
/** Sets the current Object to the previous one in the list.
*/
public void goPrev() { // Move curr to previous position
if ((curr == null) || (curr == head)) // No previous Object
{ curr = null; return; } // so just return
Node temp = head; // Start at front of list
while ((temp != null) && (temp.next() != curr))
temp = temp.next();
curr = temp; // Found previous link
}
/** Returns the number of Objects in the list.
* @return The number of Objects in the list.
*/
public int getLength() { // Return current length of list
int cnt = 0;
for (Node temp = head.next(); temp != null; temp = temp.next())
cnt++; // Count the number of Objects
return cnt;
}
/** Sets the current Object to the given index.
* @param pos The index to move to.
*/
public void go(int pos) { // Set curr to specified position
curr = head;
for(int i=0; (curr!=null) && (i<pos); i++) {
Assert.notNull(curr, "Index bigger than Vector size.");
curr = curr.next();
}
}
/** Sets the current Object's value: a reference.
* @param it The reference to set this list entre to.
*/
public void set(Object it) // Set current Object's value
{ Assert.notFalse(isInList()); curr.next().setElement(it); }
/** Gets the current Object's value.
* @return The reference for the current Object. Note, you will probably need to cast.
*/
public Object get() { // Return value of current Object
if (!isInList()) return null;
return curr.next().element();
}
/** Returns true if the current pointer is valid.
* @return true if the current pointer points within the list.
*/
public boolean isEmpty() // Return true if list is empty
{ return head.next() == null; }
/** Returns true if the current pointer is valid.
* @return true if the current pointer points within the list.
*/
public boolean isInList() // True if curr is within list
{ return (curr != null) && (curr.next() != null); }
/** Resizes the internal stucture to hold at least the given numer of objects.
* @param sz The minumum number of Objects to hold.
*/
public void resize(int sz) {} // no effect
/** Resizes the internal stucture to use the least amount or memory.
*/
public void shrink(){} // no effect
/** Single listed list node.
*/
class Node {
private Object element; // Object for this node
private Node next; // Pointer to next node in list
Node(Object it, Node n) // Constructor 1
{ element = it; next = n;} // Given Object
Node(Node n) { next = n; } // Constructor 2
Node next() { return next; }
Node setNext(Node nextval) { return next = nextval; }
Object element() { return element; }
Object setElement(Object it) { return element = it; }
} // class Node
} // class LVector
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -