javaSE基础之 LinkedList的底层简单实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaSE基础之 LinkedList的底层简单实现相关的知识,希望对你有一定的参考价值。
这里贴上LinkedList底层的简单实现
package com.yck.mylinkedlist; public class Node { private Node previous; //上一结点 private Object object; //本结点所存储的东西 private Node next; //下一个结点 public Node(){} public Node(Node previous, Object object, Node next) { super(); this.previous = previous; this.object = object; this.next = next; } public Node getPrevious() { return previous; } public void setPrevious(Node previous) { this.previous = previous; } public Object getObject() { return object; } public void setObject(Object object) { this.object = object; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
package com.yck.mylinkedlist; /** * 手动实现LinkedList底层,这次用了泛型 * @author Administrator * * @param <T> */ public class MyLinkedList<T> { private Node first; private Node last; private int size; public MyLinkedList(){} public void add(T t) { Node n =new Node(); if(first == null) { n.setPrevious(null); n.setObject(t); n.setNext(null); first = n; last = n; size++; } else { n.setPrevious(last); n.setObject(t); n.setNext(null); last.setNext(n); last = n; size++; } } public void add(int index,T t) { rangCheck(index); Node temp = new Node(); if(index == 0) { temp.setPrevious(null); temp.setObject(t); temp.setNext(first); first = temp; } else if(index != size) { temp.setPrevious(getNode(index-1)); temp.setObject(t); temp.setNext(getNode(index)); getNode(index-1).setNext(temp); getNode(index).setPrevious(temp); } else { temp.setPrevious(last); temp.setObject(t); temp.setNext(null); last = temp; } size++; } public void add(MyLinkedList<T> list) { Node temp = new Node(); temp = list.first; temp.setPrevious(this.last); this.last.setNext(temp); this.last = list.last; this.size += list.size; list.first.setPrevious(null); } public void remove(int index) { getNode(index - 1).setNext(getNode(index+1));; getNode(index).setPrevious(getNode(index -1)); size--; } public Node getNode(int index) { rangCheck(index); Node temp = new Node(); temp=first; for(int i=0;i<index;i++) { temp=temp.getNext(); } return temp; } public Object get(int index) { return getNode(index).getObject(); } public void rangCheck(int index) { if(index<0 || index>=this.size) try { throw new Exception(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { MyLinkedList<String> list = new MyLinkedList<String>(); list.add("aaa"); list.add("ooo"); list.add("xxx"); list.add("fff"); for(int i=0;i<list.size;i++) System.out.println(list.get(i)); System.out.println("****************"); list.remove(2); for(int i=0;i<list.size;i++) System.out.println(list.get(i)); System.out.println("****************"); MyLinkedList<String> list1 = new MyLinkedList<String>(); list1.add("I"); list1.add("love"); list1.add("u"); System.out.println(list1.first.getPrevious()); System.out.println("****************"); for(int i=0;i<list1.size;i++) System.out.println((String)list1.get(i)); System.out.println("****************"); list.add(list1); for(int i=0;i<list.size;i++) System.out.println((String)list.get(i)); System.out.println("****************"); } }
差点忘记贴上面简单测试的结果了
以上是关于javaSE基础之 LinkedList的底层简单实现的主要内容,如果未能解决你的问题,请参考以下文章