java 用双向链表实现SJBLinkedList


public class SJBLinkedList{

private Node first;

private Node last;

private int size;

public int size(){

return size;

}

public boolean isEmpty(){

return size == 0;

}

public void add(Object obj){

Node node = new Node();

if(first == null){

node.obj = obj;

node.previous = null;

node.next = null;

first = node;

last = node;

}else{

node.obj = obj;

node.previous = last;

node.next = null;

last.next = node;

last = node;

}

size++;

}

private Node getNode(int index){

if(index < 0 || index >size ){

try {

throw new Exception();

} catch (Exception e) {

e.printStackTrace();

System.exit(0);

}

}

Node temp = null;

if(first != null){

temp = first;

for (int i=0;i<index;i++){

temp = temp.next;

}

}

return temp;

}

public Object get(int index){

return getNode(index).obj;

}

public void remove(int index){

if(index < 0 || index >size ){

try {

throw new Exception();

} catch (Exception e) {

e.printStackTrace();

System.exit(0);

}

}

Node currentNode = getNode(index);

Node upNode = currentNode.previous;

Node downNode = currentNode.next;

upNode.next = downNode;

downNode.previous = upNode;

size--;

}

private class Node{

private Node previous;

private Object obj;

private Node next;

public Node(Node previous,Node next,Object obj){

this.previous = previous;

this.obj = obj;

this.next = next;

}

public Node(){

}

public Node getPrevious() {

return previous;

}

public void setPrevious(Node previous) {

this.previous = previous;

}

public Object getObj() {

return obj;

}

public void setObj(Object obj) {

this.obj = obj;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

public static void main(String[] args){

SJBLinkedList list = new SJBLinkedList();

list.add("123");

list.add("234");

list.add("345");

for(int i=0;i<3;i++){

System.out.println(list.get(i));

}

//System.out.println(list.get(5));

}

}

优质内容筛选与推荐>>
1、想要入坑机器学习?这是MIT在读博士的AI心得
2、AI打《星际》被人类碾压?不,事情并没有这么简单
3、Towardsbiologicallyplausibledeeplearning
4、tensorflow编程:Variables
5、EasyUI中那些不容易被发现的坑——EasyUI重复请求2次的问题


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号