以下代码只是根据大话数据结构的理论编写,只保证了结果和预期一样。
package com.myTest.stack;
import java.util.LinkedList;
/**
* Java实现双向链表结构,add,get,remove, !mod
* @param <E>
*/
public class TwoWayLinkList<E extends Object> {
public static void main(String[] args) {
LinkedList<String> strings = new LinkedList<>();
TwoWayLinkList<Integer> list = new TwoWayLinkList<>();
/* list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.addLast(5);*/
list.addFirst(6);
list.addFirst(7);
list.addFirst(8);
list.addLast(9);
System.out.println(list);
list.add(2,11);
System.out.println(list);
System.out.println(list.get(3));
list.remove(3);
System.out.println(list);
list.mod(1,100);
System.out.println(list);
}
private Node<E> first;
private Node<E> last;
private static int size = 0;
public int size() {
return size;
}
/**
* 栈节点
* @param <E>
*/
private static class Node<E> {
Node<E> prev;
Node<E> next;
E e;
public Node(Node prev, E e, Node next) {
this.prev = prev;
this.next = next;
this.e = e;
}
private Node() {
}
}
/**
* 从链表结尾添加元素
* @param e
*/
public void addLast(E e) {
Node<E> l = last;
Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
}
size++;
}
public E getLast() {
if (last != null) {
return last.e;
} else {
return null;
}
}
/**
* 从链表开始插入元素
* @param e
*/
public void addFirst(E e) {
Node<E> f = first;
if(f==null){
Node<E> newNode = new Node<>(null, e, null);
first = newNode;
last=newNode;
}else {
Node<E> eNode = new Node<>(null, e, f);
first.prev=eNode;
first=eNode;
}
size++;
}
public E getFirst() {
if (first != null) {
return first.e;
} else throw new RuntimeException("链表不存在元素");
}
/**
* 指定索引添加元素
* @param index
* @param e
*/
public void add(int index, E e) {
if (size >= 0 && index <= size) {
Node<E> x = first;
for (int i = 0; i < index; i++) {
x = x.next;
}
if (x == null) {
first = new Node<>(null, e, null);
last = first;
} else if (x.prev != null) {
Node<E> newNode = new Node<>(x.prev, e, x);
x.prev.next = newNode;
x.prev = newNode;
} else if (x.prev == null) {
Node<E> newNode = new Node<>(null, e, x);
first = newNode;
x.prev = newNode;
}
} else throw new RuntimeException("异常");
size++;
}
/**
* 获取标记索引的元素
* @param index
* @return
*/
public E get(int index){
if(size>=0&&index<=size){
Node<E> x = first;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x.e;
}else return null;
}
/**
* 删除元素
* @param index
* @return
*/
public void remove(int index){
if(size>=0&&index<=size) {
Node<E> tmp = first;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
Node<E> prevNode = tmp.prev;
Node<E> nextNode = tmp.next;
tmp.prev=null;
tmp.next=null;
prevNode.next=nextNode;
nextNode.prev=prevNode;
size++;
}
}
/**
* 修改指定索引元素,源代码应该没有这个方法
* @param index
* @return
*/
public void mod(int index,E e){
if(size>=0&&index<=size) {
Node<E> tmp = first;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
tmp.e=e;
}
}
@Override
public String toString() {
String result="[";
if(first!=null){
Node<E> tmp=first;
while(tmp.next!=null){
result=result+tmp.e+",";
tmp=tmp.next;
}
result=result+tmp.e+",";
}
int i = result.lastIndexOf(",");
result=result.substring(0,i);
result=result+"]";
return result;
}
}