package com.datastures.linkedlist;
import javax.xml.transform.Templates;
public class LikedListTest {
private Node first; //第一个节点
private Node last; //最后一个节点
private static int size;
/*
* 加入元素方法
* a,b,c
*/
public void add(Object obj){
Node node = new Node(obj);
if(first == null){ //是否是链表中的第一个元素
first = node;
last = node;
}else{
node.previous = last; //第二个节点
node.next = null;
last.next = node;
last = node; //相当于前面的节点的next指向当前的节点,当前节点的previous指向前一个节点的last
}
size++;
}
/*
* 在指定地点加入元素
*/
public void add(int index,Object obj){
Node node = new Node(obj); //建立新的节点
Node tmp = getNode(index); //取到当前位置节点
if(tmp!=null && (index > 0 && index <= size - 1)){
Node up = tmp.previous;
node.previous = up;
up.next = node;
node.next = tmp;
tmp.previous = node;
}
if(index == 0 && tmp!=null){
tmp.previous = node;
node.next = tmp;
first = node;
}
if(index == size -1 && tmp!=null){
tmp.next = node;
node.previous = tmp;
node.next = null;
last = node;
}
size++;
}
/*
* 重写toString
*/
public String toString(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
Node tmp = first;
while (tmp != null) {
stringBuilder.append(tmp.element + ",");
tmp = tmp.next;
}
stringBuilder.setCharAt(stringBuilder.length() - 1,']');
return stringBuilder.toString();
}
/*
* 根据下标求出值
*/
public Object get(int index){
if(index < 0 || index > size - 1){
throw new RuntimeException("索引数组不合法" + index);
}
Node tmp = getNode(index);
return tmp!=null?tmp.element:null;
}
/*
* 得到某个节点
*/
public Node getNode(int index){
Node tmp = null;
if(index > (size >> 1)){
tmp = first;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
}else {
tmp = last;
for (int i = size - 1; i >index; i--) {
tmp = tmp.previous;
}
}
return tmp;
}
/*
* 删除某个节点
*/
public void remove(int index){
Node tmp = getNode(index);
if(tmp !=null){
Node up = tmp.previous; //当前节点的上一个节点
Node down = tmp.next; //当前节点的下一个节点
if(up!=null){
up.next = down;
}
if(down!=null){
down.previous = up;
}
if(index == 0){
first = down;
}
if(index == size -1){
last = up;
}
}
size--;
}
public static void main(String[] args) {
LikedListTest likedListTest = new LikedListTest();
likedListTest.add("a");
likedListTest.add(123);
likedListTest.add("dfjj");
likedListTest.add("dfdg");
likedListTest.add("etr");
likedListTest.add("kjhh");
likedListTest.add("cv");
System.out.println(likedListTest.toString());
System.out.println(likedListTest.get(6));
likedListTest.remove(5);
System.out.println(likedListTest.toString());
likedListTest.remove(0);
System.out.println(likedListTest.toString());
likedListTest.remove(size - 1);
System.out.println(likedListTest.toString());
likedListTest.add(1, "kjkj");
System.out.println(likedListTest.toString());
likedListTest.add(0,"fdjkdsla");
System.out.println(likedListTest.toString());
likedListTest.add(size - 1,"gaoqi");
System.out.println(likedListTest.toString());
}
}
/*
Node类
*/
package com.datastures.linkedlist;
/**
* 双向链接节点
* @author zhuaisha98kbaoni
*
*/
public class Node {
Node previous; //上一个节点
Node next; //下一个节点
Object element; //元素
public Node(Node previous,Node next,Object element) {
this.previous = previous;
this.next = next;
this.element = element;
}
public Node(Object element) {
this.element = element;
}
}