您的位置:首页 > 编程语言 > Java开发

java实现双向链表

2016-05-21 10:37 706 查看
public class DoubleLinkList {
private Node head;  //链表头结点
private Node tail;   //链表尾结点
int size=1;
//初始化链表
public DoubleLinkList(int data) {
// TODO Auto-generated constructor stub
head=new Node(null, 1, null);
tail=head;
}
//链表是否为空
public boolean isEmpty() {
return size==0;
}
//获得双向链表的长度
public int  getSize() {
return size;
}
//获得指定位置的节点,index从0开始 计算
public Node getNode(int index) {
if (index<0||index>size-1) {
throw new IndexOutOfBoundsException("索引越界");
}else {
int a=0;
Node current=head;
while (index!=a) {
current=current.next;
a++;
}
//结束循环后current 指向index位置的节点
return current;
}
}
//获得指定位置节点的数据
public int  getData(int index) {
return getNode(index).data;
}
//在链表的头部位置插入节点
public void  addHeadData(int data) {
Node node=new Node(null, data, this.head);
this.head.pre=node;
this.head=node;
if (tail==null) {
this.head=tail;
}
size++;
}
//在尾部位置插入节点
public void  addTailData(int data) {
if (head==null) {
head=new Node(null, data, null);
this.tail=this.head;
}else {
Node node =new Node(tail, data, null);
tail.next=node;
tail=node;
}
size++;

}
//在指定位置插入节点
public void  addData(int index ,int data) {
if (index<0||index>size) {
throw new IndexOutOfBoundsException("索引越界");
}else {
if (head==null) {
this.addTailData(data);   //尾部插入
}else {
if (index==0) {
this.addHeadData(data);
}else {
Node node=this.getNode(index-1);  //要先找到插入的前一个节点
Node current=node.next;
Node newNode=new Node(node, data, current);
//newNode.pre=node,newNode.next=current;
node.next=newNode;
current.pre=newNode;
size++;
}
}
}
}
//删除尾结点
public void  deleteTail() {
Node node=this.getNode(size-2);
Node current=node.next;    //要删除的节点
node.next=current.next;
current.next=null;
current.pre=null;
size--;
}
//删除指定位置节点
public void delete(int index) {
//System.out.println(size-1);
if (index<0||index>size-1)
throw new IndexOutOfBoundsException("索引越界");
//删除的是头结点
if (index==0) {
Node current=head;
this.head=head.next;
this.head.pre=null;
size--;
}else {
Node node=this.getNode(index-1);
Node current=node.next;    //要删除的节点
node.next=current.next;
if (current.next!=null)    //如果是尾结点
current.next.pre=node;
else {
current.next=null;
current.pre=null;
size--;
}
}
}
//正向打印链表
public void  print() {
for(Node node=head;node!=null;node=node.next)
{
System.out.print(node.data +"  ");
}
System.out.println();
}
//反向打印链表
public void  reversePrint() {
for (Node node=tail;node!=null;node=node.pre) {
System.out.print(node.data+"  ");
}
System.out.println();
}
//链表的结点类
class Node{
int data;
Node pre;
Node next;
public Node(Node pre,int data,Node next) {
// TODO Auto-generated constructor stub
this.pre=pre;
this.data=data;
this.next=next;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
DoubleLinkList list=new DoubleLinkList(1);
for (int i=0;i<6;i++) {
list.addTailData(i);
}
System.out.println("正向打印链表");
list.print();
System.out.println("插入头结点后打印链表");
list.addHeadData(20);
list.print();
System.out.println("插入尾结点后打印链表");
list.addTailData(30);
list.print();
list.addData(2, 10);
System.out.println("插入数据后正向打印链表");
list.print();
list.delete(0);
System.out.println("删除头结点后正向打印链表 ");
list.print();
list.deleteTail();
System.out.println("删除尾结点后正向打印链表 ");
list.print();
list.delete(3);
System.out.println("删除索引为3的数据后打印链表");
list.print();
//list.reversePrint();

}

}


结果


正向打印链表

1 0 1 2 3 4 5

插入头结点后打印链表

20 1 0 1 2 3 4 5

插入尾结点后打印链表

20 1 0 1 2 3 4 5 30

插入数据后正向打印链表

20 1 10 0 1 2 3 4 5 30

删除头结点后正向打印链表

1 10 0 1 2 3 4 5 30

删除尾结点后正向打印链表

1 10 0 1 2 3 4 5

删除索引为3的数据后打印链表

1 10 0 2 3 4 5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: