Java实现泛型链表
2007-05-02 23:10
369 查看
import java.util.Iterator;
public class LinkList<V> implements Iterable<V> {
Data<V> first;
Data<V> last;
Data<V> current;
int count;
public LinkList() {
}
public boolean add(V obj)
{
boolean ret = false;
Data<V> data = new Data<V>(obj);
if(first == null)
{
first = data;
last = data;
current = data;
ret = true;
}
else
{
last.next = data;
data.previous = last;
last = data;
last.next = null;
ret = true;
}
count++;
return ret;
}
public V get(int index)
{
return getData(index).data;
}
public V getFirst()
{
return first.data;
}
public V getLast()
{
return last.data;
}
public int indexOfx(V obj)
{
int index = -1;
current = first;
while(count-- > 0)
{
if(current.data.equals(obj))
{
++index;
break;
}
current = current.next;
}
return index;
}
public boolean removeAt(int index)
{
boolean ret = false;
if(checkIndex(index))return ret;
if(index == 0)
{
first = first.next;
first.previous = null;
ret = true;
}
else if(index == count - 1)
{
last = last.previous;
last.next = null;
ret = true;
}
else
{
Data<V> _f,_l;
current = getData(index);
System.out.println(current.data);
_f = current.previous;
_l = current.next;
_f.next = _l;
_l.previous = _f;
ret = true;
}
count--;
return ret;
}
public V remove(V obj)
{
current = first;
int i = -1;
while(i < count)
{
i++;
if(current.data.equals(obj))break;
current = current.next;
}
removeAt(i);
return current.data;
}
public boolean insert(int index , V obj)
{
boolean ret = false;
Data<V> data = new Data<V>(obj);
if(index == 0)
{
current = first;
first = data;
first.next = current;
first.previous = null;
}
else if(index == count -1)
{
current = last;
last = data;
last.previous = current;
last.next = null;
}
else
{
Data<V> _u,_l;
current = getData(index);
_u = current.previous;
_l = current;
_u.next = data;
data.previous = _u;
_l.previous = data;
data.next = _l;
}
count++;
return ret;
}
public Data<V> getData(int index)
{
if(checkIndex(index))throw new ArrayIndexOutOfBoundsException("数组越界!");
current = first;
while(index-- > 0)
current = current.next;
return current;
}
public boolean checkIndex(int i)
{
boolean ret = false;
if(i < 0 || i > count - 1)ret = true;
return ret;
}
class Data<T>
{
T data;
Data<T> previous;
Data<T> next;
Data(T t)
{
this.data = t;
}
}
// 实现Iterable 接口可使用 for-each 类型循环厉遍。
@SuppressWarnings("unchecked")
public Iterator iterator() {
return new Itr();
}
class Itr implements Iterator
{
int cursor = 0;
int lastRet = -1;
public boolean hasNext() {
boolean ret = false;
if(cursor != count)ret = true;
return ret;
}
public Object next() {
return getData(cursor++).data;
}
public void remove() {
}
}
}
public class LinkList<V> implements Iterable<V> {
Data<V> first;
Data<V> last;
Data<V> current;
int count;
public LinkList() {
}
public boolean add(V obj)
{
boolean ret = false;
Data<V> data = new Data<V>(obj);
if(first == null)
{
first = data;
last = data;
current = data;
ret = true;
}
else
{
last.next = data;
data.previous = last;
last = data;
last.next = null;
ret = true;
}
count++;
return ret;
}
public V get(int index)
{
return getData(index).data;
}
public V getFirst()
{
return first.data;
}
public V getLast()
{
return last.data;
}
public int indexOfx(V obj)
{
int index = -1;
current = first;
while(count-- > 0)
{
if(current.data.equals(obj))
{
++index;
break;
}
current = current.next;
}
return index;
}
public boolean removeAt(int index)
{
boolean ret = false;
if(checkIndex(index))return ret;
if(index == 0)
{
first = first.next;
first.previous = null;
ret = true;
}
else if(index == count - 1)
{
last = last.previous;
last.next = null;
ret = true;
}
else
{
Data<V> _f,_l;
current = getData(index);
System.out.println(current.data);
_f = current.previous;
_l = current.next;
_f.next = _l;
_l.previous = _f;
ret = true;
}
count--;
return ret;
}
public V remove(V obj)
{
current = first;
int i = -1;
while(i < count)
{
i++;
if(current.data.equals(obj))break;
current = current.next;
}
removeAt(i);
return current.data;
}
public boolean insert(int index , V obj)
{
boolean ret = false;
Data<V> data = new Data<V>(obj);
if(index == 0)
{
current = first;
first = data;
first.next = current;
first.previous = null;
}
else if(index == count -1)
{
current = last;
last = data;
last.previous = current;
last.next = null;
}
else
{
Data<V> _u,_l;
current = getData(index);
_u = current.previous;
_l = current;
_u.next = data;
data.previous = _u;
_l.previous = data;
data.next = _l;
}
count++;
return ret;
}
public Data<V> getData(int index)
{
if(checkIndex(index))throw new ArrayIndexOutOfBoundsException("数组越界!");
current = first;
while(index-- > 0)
current = current.next;
return current;
}
public boolean checkIndex(int i)
{
boolean ret = false;
if(i < 0 || i > count - 1)ret = true;
return ret;
}
class Data<T>
{
T data;
Data<T> previous;
Data<T> next;
Data(T t)
{
this.data = t;
}
}
// 实现Iterable 接口可使用 for-each 类型循环厉遍。
@SuppressWarnings("unchecked")
public Iterator iterator() {
return new Itr();
}
class Itr implements Iterator
{
int cursor = 0;
int lastRet = -1;
public boolean hasNext() {
boolean ret = false;
if(cursor != count)ret = true;
return ret;
}
public Object next() {
return getData(cursor++).data;
}
public void remove() {
}
}
}
相关文章推荐
- Java学习笔记——链表的泛型实现和序列化读写
- 单链表翻转Java实现
- 数据结构——使用双端链表实现队列(java实现)
- java 用单链表实现队列
- 无头结点的单链表(java实现)
- 链表的java实现以及基本的增加,删除,排序操作
- 使用JAVA实现双向链表
- Java中双向链表的代码实现
- Java实现两个有序链表的合并
- 单链表和双向链表java实现
- Java语言解决约瑟夫环问题(链表实现)
- java实现数据结构之单向链表
- 剑指Offer 面试题22:链表中倒数第k个节点 Java代码实现
- Java模拟单向链表和双向链表的实现
- java详解 --- 集合之数组实现和链表实现
- java手动实现双向链表LinkedList
- java中利用泛型实现类
- Java实现简单的链表(钢铎的烽火)
- Java链表实现堆栈
- Java实现 双链表互换任意两个节点