我要写一个双链表, header的pre 是null, tail的next也是null
然后header和taill一上来就有,加节点的时候只是在中间加
请问该怎么写
private Node addBefore(E e, Node node)
这个方法?
然后header和taill一上来就有,加节点的时候只是在中间加
请问该怎么写
private Node addBefore(E e, Node node)
这个方法?
双链表结构在头部 或 尾部加结点嘛addFirst(),addLast()
中间不能叫加的嘛 叫insert(index n, E e)
要想insertBefore(index n, E e)
就得找到 n-1 个节点
if(n<List.size())
{
new 个节点newNode;
newNode.data = E;
newNode.next = n-1个节点;
n-1.last = newNode;
}else
{
// 超出了想怎么处理自己办
}其实具体的你可以参考JDK中的双端队列 Deque接口 和实现了这个接口的LinkedList类
private Node addBefore(E e, Node node) {//e是什么东西,加入是要在e节点前面加入Node
Node n=e.pre;
n.next=node;
node.pre=n;
e.pre=node;
node.next=e;
}
简单的说了一下,LZ还是把问题描述的全一点
Node newNode = new Node(e);
newNode.next = node;//插入新节点之后,新节点的后续指针指向node节点
newNode.prev = node.prev;//插入新节点之后,新节点的前驱指针指向node节点之前的前驱节点
newNode.next.prev = newNode;//插入新节点之后,新节点的后续指针的前驱指向新节点
newNode.prev.next = newNode;//插入新节点之后,新节点的前驱指针的后续指向新节点
size++;
}
无非就是一个插入的时候要处理一下之前的指针。画个图就明了了。这个是双向循环链表的,不知道是不是你说的双链表?
Node header
Node tail
然后header和tail是2个一直存在的node
在header和tail之间添加node
header->next->pre = n ;
n->pre = header ;
header->next = n ;对么?
首节点将前驱置为null,同样尾节点将后导置null。class Node{public Node();Public Node(Node pre,Node tail,Objec value){
this.pre=pre;
this.tail=tail;
this.value=value;
} Node pre=null;
Node tail=null;
Object value=null;
/*
*@param node要在哪个节点前面加
*@param addNode 欲添加的节点
*在该节点前面添加
*/
public void addFront(Node node,Node addNode){
if(null==node.pre){
//若该节点是首节点
node.pre=addNode;
addNode.tail=node;
addNode.pre=null;
}esle{
if(null==node.tail)// the ending node node.pre=addNode;
addNode.pre=node.pre;
addNode.tail=node;
else{
addNode.pre=node.pre;
addNode.tail=node;
node.pre=addNode;
}}}public void addBehind()//在某节点后面添加,这个就不写了private Node pre;
private Node tail;
private Object value;
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Structure;import java.util.ArrayList;
import java.util.Arrays;/**
*
* @author jiang
*/
public class LinkedNode { /*
*可以构建一个数组来存放所有节点,丰富操作
* 略去,此处方便计,构建了两个静态节点,后面使用
*/
static Node head = new Node(0, 1);
static Node tail = new Node(1, 2); public LinkedNode() {
head.setTail(tail);
tail.setPre(head);
}// public Node getNode(int index) {
//
// }
public void addFront(Node node, Node addNode) { if (null == node.getPre())//首节点
{
node.setPre(addNode);
addNode.setPre(null);
addNode.setTail(node);
} else {
if (null == node.getTail())//尾节点
{
addNode.setPre(node.getPre());
node.setPre(addNode);
addNode.setTail(tail);
} else {
addNode.setPre(node.getPre());
node.setPre(addNode);
addNode.setTail(node);
}
}
} public static void main(String[] args) {
LinkedNode lnode = new LinkedNode();
Node nodeTmp = new Node(2, 3);
//lnode.addFront(lnode.getNode(index),nodeTmp);
lnode.addFront(tail, nodeTmp);
// System.out.println(lnode.toArray);
}
public Node node;
}class Node { public Node() {
} public Node(int index, Integer value) {
this.index = index;
this.value = value;
this.Tail = null;
this.pre = null;
} public Node getTail() {
return Tail;
} public void setTail(Node Tail) {
this.Tail = Tail;
} public int getIndex() {
return index;
} public void setIndex(int index) {
this.index = index;
} public Node getPre() {
return pre;
} public void setPre(Node pre) {
this.pre = pre;
} public Integer getValue() {
return value;
} public void setValue(Integer value) {
this.value = value;
} @Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Node other = (Node) obj;
if (this.pre != other.pre && (this.pre == null || !this.pre.equals(other.pre))) {
return false;
}
if (this.Tail != other.Tail && (this.Tail == null || !this.Tail.equals(other.Tail))) {
return false;
}
if (this.index != other.index) {
return false;
}
return true;
} @Override
public int hashCode() {
int hash = 5;
hash = 37 * hash + (this.pre != null ? this.pre.hashCode() : 0);
hash = 37 * hash + (this.Tail != null ? this.Tail.hashCode() : 0);
hash = 37 * hash + this.index;
return hash;
} public String toString() {
return "this is " + this.index;
}
private Node pre;
private Node Tail;
private int index;
private Integer value;
}
既然是无表头的链表,那么,添加元素也就无所谓before和after了。因为,before和after是相对于表头而言的。
这种无表头的链表(包括单向和双向),添加操作,要比有表头的操作,多出判断链表大小是否为0的操作,因为,有没有元素,其操作是不同的;而删除操作,则要多出判断链表大小是否小于等于1的操作,原因也是因为,1个元素或空链表与2个以上链表的删除操作是不同的。(不同,指的是思路或代码)。
这帖子看看 也就是8楼写的