java双向链表

package datastruct;

/*
*双向链表的的结点
*/
public class DoubleNode {
private DoubleNode previous;// 指向前一个接点的指针
private int value;// 接点保存的值
private DoubleNode next;// 指向下一个接点的指针



public DoubleNode(int value) {
  super();
  this.value = value;
  this.previous = this;//初始时指向自身
this.next = this;//初始时指向自身
}

public DoubleNode() {
  // TODO Auto-generated constructor stub
}

public DoubleNode getPrevious() {
  return previous;
}

public void setPrevious(DoubleNode previous) {
  this.previous = previous;
}

public int getValue() {
  return value;
}

public void setValue(int value) {
  this.value = value;
}

public DoubleNode getNext() {
  return next;
}

public void setNext(DoubleNode next) {
  this.next = next;
}

}

package datastruct;
/**
* 双向链表
* @author dell
*
*/
public class MyDoubleList {

private DoubleNode head;// 头结点,初始值为null
private int length = 0;// 链表的长度

public int getLength() {
  return length;
}

public void setLength(int length) {
  this.length = length;
}

public MyDoubleList() {
  head = new DoubleNode();
}

public DoubleNode getHead() {
  return head;
}

public void setHead(DoubleNode head) {
  this.head = head;
}

/**
  * @param args
  */
public static void main(String[] args) {
  MyDoubleList ml = new MyDoubleList();
  ml.add(new DoubleNode(1));
  ml.add(new DoubleNode(2));
  ml.add(new DoubleNode(3));
  ml.add(new DoubleNode(4));
  ml.add(new DoubleNode(5));
  ml.add(new DoubleNode(6));
  ml.add(new DoubleNode(7));
  ml.remove(new DoubleNode(3));
   ml.insert(new DoubleNode(11), 3);

  while (ml.head != null) {
   System.out.println(ml.head.getValue());
   ml.head = ml.head.getPrevious();
  }
}

/**
  * 添加新节点
*
  * @param n要加入的节点
*/
public void add(DoubleNode n) {
  head.setNext(n);//头结点的next指针指向n
  //n的前驱指针指向head
  n.setPrevious(head);
  head = n;// 指针移动
this.length++;
}

/**
  * 删除某个节点
*
  * @param n要删除的节点
*/
public void remove(DoubleNode n) {
  if (head == null)
   return;
  if (n.getValue() == this.head.getValue()) {
   head = head.getPrevious();
   head.setNext(null);
   // head.setp
   return;
  }

  DoubleNode pre = head;// 当前节点,初始值为头结点
DoubleNode cur = head.getPrevious();// 当前节点的前驱节点
while (cur != null) {
   if (cur.getValue() == n.getValue()) {
    pre.setPrevious(cur.getPrevious());
    cur.getPrevious().setNext(pre);
    // pre.getNext().setNext(cur.getNext());
    return;//
   }

   pre = pre.getPrevious();// 移动指针
cur = cur.getPrevious();// 移动指针
}

}

/**
  *插入节点
*/
public void insert(DoubleNode n, int i) {
  if (head == null) {
   add(n);
   return;
  }

  DoubleNode pre = head;
  DoubleNode cur = head.getPrevious();
  int j = 1;
  while (cur != null) {
   if (j == i-1) {
    cur.setNext(n);
    n.setPrevious(cur);
    n.setNext(pre);
    pre.setPrevious(n);
    return;
   }

   pre = pre.getPrevious();// 移动指针
cur = cur.getPrevious();// 移动指针
j++;
  }

}
}


 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭梧悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值