public class LinkListNode {//定义节点
public int data;//数据域
public LinkListNode nextNode;//下个节点
public LinkListNode preNode;//前面节点
public LinkListNode(char c)//参数是字符型,数据是整形,是为了用字符数组添加节点
{
data=c-'0';
nextNode=null;
preNode=null;
}
public int getValues()//获取数据
{
return data;
}
public void setValues(int dat)//修改数据
{
data=dat;
}
}
双向链表要分别建两个类,一个是LinkListNode节点类,封装节点所有用
public class LinkList {//双向链表
private LinkListNode headPointer;//头指针
private LinkListNode tailPointer;//尾指针
private int length;//链表长度
public LinkList()//无参构造函数,构造空链表
{
headPointer=null;
tailPointer=null;
length=0;
}
public LinkList(char toCharArray[])//有参构造函数,字符数组传入,构建多个节点
{
int lengths=toCharArray.length;
this.length=lengths;
LinkListNode nowNode=new LinkListNode(' ');//构建当前链表将要插入节点的位置的节点,同时new LinkListNode(' ')防止java中的空指针异常。
for(int i=0;i<length;i++)
{
if(toCharArray[i]>='0'&&toCharArray[i]<='9')
{
LinkListNode node=new LinkListNode(toCharArray[i]);//新建节点
node.nextNode=null;//让该节点的下一个节点的指向为空
if(i==0)//构建头结点
{
headPointer=node;//头节点的指向是将要插入的节点
headPointer.nextNode=node;//头结点下一个节点为将要插入的节点
headPointer.preNode=null;//头节点的前面一个节点指向为null
nowNode=node;//插入节点后,把node设置为链表中将要插入下一个节点的节点。
}
else
{
nowNode.nextNode=node;//同上
node.preNode=nowNode;
nowNode=node;
tailPointer=node;
}
}
}
}
public void travel()//从头节点遍历
{
LinkListNode p=headPointer;
while(p!=null)
{
System.out.print(p.getValues());//输出节点值
p=p.nextNode;
}
}
public int search(int target)//查找目标在节点中的位置,如果查找不到,返回-1,否则,返回节点位置
{
LinkListNode p=headPointer;
int position=0;
while(p!=null)
{
if(p.getValues()==target)
return position;
position++;
p=p.nextNode;
}
return -1;
}
public void insert(int index,char target)//插入节点
{
LinkListNode p1=headPointer;//获取头节点
LinkListNode p2=headPointer;//获取头节点,让p2的位置在p1的后面
LinkListNode node=new LinkListNode(target);
int counts=0;
if(index==0)//如果在头节点插入
{
node.nextNode=headPointer;
headPointer.preNode=node;
headPointer=node;
headPointer.preNode=null;
}
else
{
while(p2!=null)//在中间插入的情况
{
if(counts==index)//到达插入的位置
{
p1.nextNode=node;
node.preNode=p1;
node.nextNode=p2;
p2.preNode=node;
break;
}
else//未到达索引位置,往前进
{
p1=p2;
p2=p2.nextNode;
}
counts++;
}
if(p2==null)//在末尾插入的情况
{
p1.nextNode=node;
node.preNode=p1;
node.nextNode=null;
tailPointer=node;
}
}
this.length++;
}
public LinkListNode getHead()//获取头节点
{
return headPointer;
}
public LinkListNode getTailNode()//获取尾节点
{
return tailPointer;
}
public void delete(int index)//删除索引为index的元素
{
if(headPointer!=null)
{
LinkListNode p1=headPointer;
LinkListNode p2=headPointer;
int counts=0;
if(index==0)//删除头节点
{
headPointer=headPointer.nextNode;
headPointer.preNode=null;
}
else
{
p2=p2.nextNode;
while(p2.nextNode!=null)//删除中间和尾节点的节点
{
counts++;
if(counts==index)
{
p1.nextNode=p2.nextNode;
p2.nextNode.preNode=p1;
break;
}
else
{
p1=p2;
p2=p2.nextNode;
}
}
if(p2.nextNode==null)//如果删除的时候尾节点,将重置尾节点
{
p1.nextNode=p2.nextNode;
tailPointer=p1;
}
}
this.length--;//删除节点,长度减一
}
else
{
System.out.println("空链表,无法删除!");
}
}
public int getLength()
{
return this.length;
}
public static void main(String[] args) {
char []a={'1','2','3'};
LinkList linkList=new LinkList(a);
linkList.insert(3, '0');
linkList.travel();//从头节点遍历
System.out.print("\n");
linkList.delete(1);
linkList.travel();
}
的成员,另一个是LinkList的链表类,用来实现链表的各项功能