📄 duolinklist.java
字号:
package DuoLink;
public class DuoLinkList
{
private DuoLinkItem Start=null;
private DuoLinkItem End=null;
private DuoLinkItem Current=null;
public DuoLinkList(Object item)
{
if(item!=null)
{
Current=Start=End=new DuoLinkItem(item);
}
}
public void clear()
{
Current=null;
End=null;
Start=null;
}
public DuoLinkItem getFirst()
{
Current=Start;
return Start==null?null:Start;
}
public DuoLinkItem getNext()
{
if(Current!=null){Current=Current.next;}
return Start==null?null:Current;
}
public int getLength(DuoLinkItem Start)
{
int i;
for(i=1;i>0;i++)
{
if(Start.item!=null&&Start.next!=null){Start=Start.next;}
else break;
}
return i;
}
public int getIndex(Object item)
{
int i; Current=Start;
for(i=0;i<this.getLength(this.getFirst());i++)
{
if(item==Current.item)break;
if(item!=Current.item)
{
Current=Current.next;
}
if(Current.next==null)
{
Current=Start;
System.out.println("The item is not exist!");
i=-1;
}
}
return i;
}
public boolean equals(DuoLinkList linkStart1,DuoLinkList linkStart2)
{
if(!linkStart1.getItemArray().equals(linkStart2.getItemArray())) return false;
else return true;
}
public Object[] getItemArray()
{
Object[] items=new Object[this.getLength(this.getFirst())];
for(int i=0;i<this.getLength(this.getFirst());i++)
{
items[i]=Start.item;
Start=Start.next;
}
return items;
}
public DuoLinkList intl(Object[] items)
{
if(items!=null)
{
for(int i=0;i<items.length;i++) intlItem(items[i]);
Current=Start;
}
return this;
}
public void intlItem(Object item)
{
DuoLinkItem newEnd=new DuoLinkItem(item);
if(Start==null)
{
Start=End=newEnd;
}
else
{
End.next=newEnd;
End=newEnd;
}
}
public DuoLinkItem searchItem(Object item)
{
int i; Current=Start;
for(i=0;i<this.getLength(this.getFirst());i++)
{
if(item==Current.item)
{
System.out.println("The item is found!It's:");
}
if(item!=Current.item)
{
Current=Current.next;
}
if(Current.next==null)
{
Current=Start;
System.out.println("The item is not found!The start item is:");
break;
}
}
return Current;
}
public DuoLinkList removeItem(Object item)
{
int i;
Current=this.searchItem(item);
for(i=this.getIndex(item);i<this.getLength(this.getFirst());i++)
{
Current=Current.next;
}
DuoLinkList newLinkList=new DuoLinkList(this.getItemArray());
return newLinkList;
}
public DuoLinkList insertItem(Object item,Object insert)
{
int i;DuoLinkItem newItem=new DuoLinkItem(insert);
Current=this.searchItem(item);
if(Current==Start||Current==null)
{
System.out.println("Can't find the item.Can't insert!Please input again");
return this;
}
else
{
newItem.last=Current.last;
newItem.next=Current.next;
}
return this;
}
public String toString()
{
System.out.println("The Link is:");
Object[] items=new Object[this.getLength(this.getFirst())];
items=this.getItemArray();
StringBuffer b = new StringBuffer(items.length*7);
b.append("[");
for(int i=0;i<this.getLength(this.getFirst());i++)
if (items[i] == null)
b.append("null, ");
else
b.append(items[i].toString() + ", ");
if (items.length > 0)
b.delete(b.length() - 2, b.length());
b.append(")");
return new String(b);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -