[code="java"]package com.read.List;
public class DbLinkedList {
//定义内部类,用作链表的节点
private class Node
{
Node pre; //指向前一个节点
Node next; //指向后一个节点
T value; //当前节点的值
public Node(T value, Node<T> next, Node<T> pre)
{
this.value = value;
this.next = next;
this.pre = pre;
}
public String toString()
{
return this.value + "";
}
}
public Node<T> header; //定义头节点
public int size; //定义链表的长度
public int flag; //指针所在位置
public DbLinkedList()
{
this.header = new Node<T>(null, null, null);//空的头节点,用来区分双向循环链表的首尾
this.flag = 0;
this.size = 0;
}
public void add(T value)//在链表的尾巴上面加一个节点
{
Node<T> temp=entry(size);
temp.value=value;
Node<T> next=new Node<T>(null, null, null);
next.pre=temp;
temp.next=next;
this.flag++;
this.size=this.flag;
}
public T get(int index)
{
return entry(index-1).value;
}
private Node<T> entry(int index) //迭代至index处的节点
{
Node<T> node = this.header;
for(int i=1; i<=index; i++)
{
node = node.next;
}
return node;
}
public void clear()
{
this.header = new Node<T>(null, null, null);
this.size = 0;
this.flag = 0;
}
//标志从当前位置前移一位,表示此时后进了一步
public boolean flagpre()
{
if(flag==0) //标志在第一步,不能再后退了
{
return false;
}
flag--;
return true;
}
//标志从当前位置前后一位,表示此时前进了一步
public boolean flagnext()
{
if(flag==size) //标志在最后一步,不能再后退了
{
return false;
}
flag++;
return true;
}
//在第一步时返回true
public boolean firststep()
{
if(flag==0)
{
return true;
}
return false;
}
//在最后一步时返回true
public boolean endstep()
{
if(flag==size)
{
return true;
}
return false;
}
public boolean isEmpty()
{
return this.size == 0;
}
public int size()
{
return this.size;
}
}
package com.read.List;
public class god {
public static class select{
String name;
String x;
String y;
select(){
}
public void clear(){
name=null;
x=null;
y=null;
}
}
public static void main(String[] args){
DbLinkedList<select> List=new DbLinkedList<select>();
select temp=new select();
for(int i=0;i<5;i++)
{
temp.clear();
temp.name="No."+i;
temp.x="x="+i;
temp.y="y="+i;
List.add(temp);
}
temp=List.get(List.flag);
System.out.println(temp.name);
System.out.println(temp.x);
System.out.println(temp.y);
temp.clear();
System.out.println(List.flag);
List.flagpre();
System.out.println(List.flag);
temp=List.get(List.flag);
System.out.println(temp.name);
System.out.println(temp.x);
System.out.println(temp.y);
DbLinkedList<String> it=new DbLinkedList<String>();
it.add("0");
it.add("1");
it.add("2");
it.add("3");
it.add("4");
System.out.println(it.get(it.flag));
it.flagpre();
System.out.println(it.get(it.flag));
}
}[/code]
控制台结果:
No.4
x=4
y=4
5
4
null
null
null
4
3
其中,temp第二次赋值失败了(3个null),it成功了(3)
ps:本人是新来的,积分少,见谅!