# Java数据结构--双向链表

2017-05-09

Java数据结构--双向链表。

## Java实现双向链表

```package DoubleLink;

/**
* Created by Zz on 2017/5/5 0005.
*/
public class Node {
public Node next;
public Node previous;
private int data;

public Node(int data) {
this.data = data;
}
public String toString(){
return data+" ";
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public boolean equals(Object other) {
Node temp = null;
if (other instanceof Node) {
temp = (Node)other;
if (temp.getData() == this.getData()) {
return true;
}else  {
return false;
}
}else {
return false;
}
}
}
```

```package DoubleLink;

/**
* Created by Zz on 2017/5/5 0005.
*/
private Node first;
private Node end;
Node node = new Node(data);
if (first!=null) {
node.next = first;
first.previous = node;
first = node;
} else {
first = node;
end = node;
}
}

Node node = new Node(data);
if(end!=null) {
end.next = node;
node.previous = end;
end = node;
} else {
first = node;
end = node;
}
}

public Node find(int data) {
Node temp = new Node(data);
Node f = first;
while (f!=null) {
if (f.equals(temp))
break;
else
f = f.next;
}
return f;
}

public void delete(int data) {
Node node = find(data);
if (node!=null) {
node.previous.next = node.next;
node.next.previous = node.previous;
}
}

public void update(int ydata,int mdata) {
Node node = find(ydata);
node.setData(mdata);
}

public String toString() {
StringBuffer stringBuffer = new StringBuffer();
Node temp = first;
while (temp!=null) {
if (temp == end)
stringBuffer.append("["+temp.getData()+"]");
else
stringBuffer.append("["+temp.getData()+"]"+",");
temp = temp.next;
}
return stringBuffer.toString();
}
}
```

``` public static void main(String[] args) {
`[30],[20],[10],[40],[50]`