大话数据结构 java源代码_大话数据结构(八)Java程序——双向链表的实现

packagecom.aclie.dataStructe4.sqeList;public classMyDoubleLinkList {private int length =0;//当前长度

private Node head;//头结点

private Node tail;//当前结点结点

publicMyDoubleLinkList(){

initLink();

}public voidinitLink(){

head= new Node(null);

tail= new Node(null);this.head =tail;

length++;

}//获取链表长度

public intgetSize(){returnlength;

}//判断链表是否为空

public booleangetEmpty(){return getSize()==0;

}//根据索引查找元素 从第一个有效值开始

public Node getNode(intindex){

Node p;if(index < 0 || index >length ){

System.out.println("参数错误");

}if(index < this.length/2){

p= this.head;for(int i=0; i

p=p.next;

}

}else{

p= this.tail;for(int i= length; i>index;i--){

p=p.prev;

}

}returnp;

}public Object getData(intindex){returngetNode(index).data;

}//在头结点处插入

public booleanaddHead(Object e){//前驱引用为null,后继为node

Node node = new Node(e, null, this.head);//改变头结点的前驱后继

this.head.prev =node;this.head =node;if(tail == null){

tail= this.head;

}

length++;return true;

}//在尾结点插入

public booleanaddTail(Object e){if(this.head == null){this.head = new Node(e,null,null);this.tail = this.head;

}else{

Node node= new Node(e,this.tail,null);this.tail.next =node;this.tail =node;

}

length++;return true;

}//在指定位置插入元素

public boolean addData(intindex,Object ele){if(index <0 || index > this.length){

System.out.println("参数错误");

}if(this.head == null){this.addTail(ele);//用尾插法

}else{if(index == 0){

addHead(ele);//用头插法

}else{

Node p= this.getNode(index);//要插入处的结点

Node n =p.next;

Node node= new Node(ele,p,n);//要插入的结点

n.prev =node;

p.next=node;

length++;

}

}return true;

}public void removeData(intindex){if(index < 0 || index >length){

System.out.println("参数错误");

}else{

Node del= null;if(index == 0){

del= this.head;this.head = this.head.next;this.head.prev = null;

length--;

}else if(index == (length-1)){

Node p= this.getNode(index-1);//得到要删除结点的前驱结点

del = p.next;//要删除的结点

p.next =del.next;if(del.next != null){

del.next.prev=p;

}

del.next= null;

del.prev= null;

length--;this.tail.next = null;this.tail.prev =p;this.tail =p;

}else{

Node p= this.getNode(index-1);//要删除结点的前驱结点

del = p.next;//要删除的结点

p.next =del.next;if(del.next != null){

del.next.prev=p;

}

del.prev= null;

del.next= null;

length--;

}

}

}//打印所有链表中的元素

public voidprint(){

Node current= this.head;while(current != null){

System.out.println(current.data);

current=current.next;

}

}//反向打印链表

public voidreversePrint(){

Node current= this.tail;while(current != null){

System.out.println(current.data);

current=current.prev;

}

}public static voidmain(String args[]){

MyDoubleLinkList linkList= newMyDoubleLinkList();

linkList.addHead("aaaa");//System.out.println(linkList.getData(1));

linkList.addTail("bbbb");//System.out.println(linkList.getData(3));

linkList.addData(2, "eeee");//linkList.print();

linkList.removeData(2);

linkList.print();

System.out.println(".....");

linkList.reversePrint();

}

}classNode{

Node prev;//指针域中前驱

Node next;//指针域中后继

Object data;//数据域

publicNode(Node current){

prev=current;

next=current;

}//双链表前驱后继及数字域

publicNode(Object d, Node p,Node n){this.data =d;this.prev =p;this.next =n;

}publicNode getPrev() {returnprev;

}public voidsetPrev(Node prev) {this.prev =prev;

}publicNode getNext() {returnnext;

}public voidsetNext(Node next) {this.next =next;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值