双向链表-数据结构-java实现
双向链表-数据结构-java实现
目录
抽象表:
package edu.cquptzx.List;
publicinterface List
{
publicvoid insert(int i ,Object obj) throws Exception; //插入
public Object delete(int i ) throws Exception; //删除
public Object getData(int i ) throws Exception; //获取i元素
publicint size(); //表数据总数
publicboolean isEmpty(); //是否为空
}
package edu.cquptzx.List;
publicclass TNode
{
TNode prior;
Object element;
TNode next;
TNode( TNode nextval)
{
prior = nextval;
next = nextval;
}
TNode(TNode priorval,Object obj,TNode nextval)
{
prior = priorval;
element = obj;
next = nextval;
}
public TNode getPrior()
{
returnprior;
}
publicvoid setPrior(TNode priorval)
{
prior = priorval;
}
public TNode getNext()
{
returnnext;
}
publicvoid setNext(TNode nextval)
{
next = nextval;
}
public Object getElement()
{
returnelement;
}
publicvoid setElement(Object obj)
{
element = obj;
}
public String toString()
{
returnelement.toString();
}
}
/**
*
*/
package edu.cquptzx.List;
/**
* @author cquptzx
*
*/
publicclass DoubleLinkList implements List {
TNode head;
TNode current;
intsize;
/**
* 构造函数:
* 初始化循环链表.
*/
DoubleLinkList()
{
head=current=new TNode(null);
head.prior=head;
head.next=head;
size = 0;
}
/**
* 定位成员函数index(int i)的实现
* 循环从头开始查找,循环的条件是:1.定位完成j==i;2.链表查找结束了.
* @param i
* @throws Exception 当参数i错误时,抛出异常.
*/
publicvoid index(int i )throws Exception
{
if(i<-1 || i >size-1)
{
thrownew Exception("i error in INDEX of DoubleLinkList.");
}
if(i == -1) return;
current = head.next;
int j = 0;
while(current!=head && j<i)
{
current = current.next;
j++;
}
}
/* (non-Javadoc)
* @see edu.cquptzx.List.List#insert(int, java.lang.Object)
*/
@Override
publicvoid insert(int i, Object obj) throws Exception
{
// TODO Auto-generated method stub
if(i<0 || i>size)
{
thrownew Exception ("i error in INSERT.");
}
index(i-1);
current.setNext(new TNode(current.getNext(),obj,current.next.getPrior()));
current.next.next.setPrior(current.next.getNext());
size++;
}
/* (non-Javadoc)
* @see edu.cquptzx.List.List#delete(int)
*/
@Override
public Object delete(int i) throws Exception {
// TODO Auto-generated method stub
if(size == 0)
{
thrownew Exception ("Link Blank in DELETE.");
}
if(i<0 || i>size-1)
{
thrownew Exception ("i error in DELETE.");
}
index(i-1);
Object obj = current.next.getElement();
current.setNext(current.next.next);
current.next.setPrior(current.getNext());
size--;
return obj;
}
/* (non-Javadoc)
* @see edu.cquptzx.List.List#getData(int)
*/
@Override
public Object getData(int i) throws Exception {
// TODO Auto-generated method stub
if(i<-1 || i>size-1)
{
thrownew Exception ("i error in getData.");
}
index(i);
returncurrent.getElement();
}
/* (non-Javadoc)
* @see edu.cquptzx.List.List#size()
*/
@Override
publicint size() {
// TODO Auto-generated method stub
returnsize;
}
/* (non-Javadoc)
* @see edu.cquptzx.List.List#isEmpty()
*/
@Override
publicboolean isEmpty() {
// TODO Auto-generated method stub
returnsize == 0;
}
}
package edu.cquptzx.List;
publicclass DoubleLinkListTest
{
publicstaticvoid main(String agrs[])
{
DoubleLinkList doubleLinkList = new DoubleLinkList();
int n = 10;
try
{
for(int i = 0;i<n;i++)
{
doubleLinkList.insert(i, new Integer(i+1));
}
doubleLinkList.delete(4);
for(int i = 0;i<doubleLinkList.size;i++)
{
System.out.print(doubleLinkList.getData(i)+" ->");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}