双向链表和单链表的区别在于他多了一个pre域保存上一个节点的位置,这样可以在任意位置就能找到所有节点,比较方便
完整代码
class Node2 {
public int data;
public Node2 next;
public Node2 pre;
public Node2(int data) {
this.data = data;
this.next = null;
this.pre = null;
}
}
public class MyDoubleList {
public Node2 head;
public Node2 feet;
public MyDoubleList() {
this.head = null;
this.feet = null;
}
public void addFirst(int data) {
Node2 node = new Node2(data);
if(this.head==null) {
this.feet = node;
this.head = node;
return;
}
node.next = this.head;
this.head.pre = node;
this.head = node;
}
public void addLast(int data) {
Node2 node = new Node2(data);
if(this.head==null) {
this.feet = node;
this.head = node;
return;
}
feet.next = node;
node.pre = feet;
feet = node;
}
public void addIndex(int index,int data){//按指定位置插入
Node2 node = new Node2(data);
Node2 cur = this.head;
if (index<0 || index>size()) {
throw new RuntimeException("下标不合法");
}
if (index==0) {
addFirst(data);
return;
}
if (index==size()) {
addLast(data);
return;
}
for (int i = 0;i<index;i++) {
cur = cur.next;
}
node.next = cur;
node.pre = cur.pre;
cur.pre = node;
node.pre.next = node;
}
public void changeBefore(int key,int data) {//修改正数第key个元素
Node2 prev = this.head;
if (key<1 || key > size()) {
throw new RuntimeException("key越界");
}
if(key==1) {
this.head.data = data;
}
else {
int cot = 1;
while(cot!=key) {
prev = prev.next;
cot++;
}
prev.data = data;
return;
}
}
public void addOrder(int data) {//按找data域大小插入
Node2 node = new Node2(data);
if (this.head==null) {
this.head = node;
this.feet = node;
return;
}
if (data<this.head.data) {
node.next = this.head;
this.head.pre = node;
this.head = node;
return;
}else {
Node2 prev = this.head;
while(prev.next!=null) {
prev = prev.next;
}
if (data>prev.data) {
prev.next = node;
node.pre = prev;
return;
}
while(data<prev.data) {
prev = prev.pre;
}
node.next = prev.next;
node.pre = prev;
prev.next = node;
node.pre.next = node;
}
}
public void displayForward() {//从前打印
Node2 cur = this.head;
while (cur!=null) {
System.out.print(cur.data+" ");
cur = cur.next;
}
}
public void displayBackward() {
Node2 cur = this.feet;
while(cur!=null) {
System.out.print(cur.data+" ");
cur = cur.pre;
}
}
public int size() {
int cot = 1;
Node2 count = this.head;
while (count.next!=null) {
count = count.next;
cot++;
}
return cot;
}
public void remove(int key){
Node2 cur = this.head;
while (cur != null) {
if(cur.data == key) {
//当前cur是不是头结点
if(cur == this.head) {
this.head = this.head.next;
this.head.pre = null;
}else {
cur.pre.next = cur.next;
if (cur.next!=null){
cur.next.pre = cur.pre;
}
else {
this.feet = cur.pre;
}
}
return ;
}else {
cur = cur.next;
}
}
}
public void removeAllKey(int key) {//删除所有key元素
Node2 cur = this.head;
while (cur != null) {
if(cur.data == key) {
//当前cur是不是头结点
if(cur == this.head) {
this.head = this.head.next;
this.head.pre = null;
}else {
cur.pre.next = cur.next;
if (cur.next!=null){
cur.next.pre = cur.pre;
}
else {
this.feet = cur.pre;
}
}
}
cur = cur.next;
}
}
public void clear() {//清空链表
this.head = null;
this.feet = null;
}
}