|
| 1 | +package com.github.zhanglifeng.coding2017.basic; |
| 2 | + |
| 3 | +import java.util.NoSuchElementException; |
| 4 | + |
| 5 | +/** |
| 6 | + * 功能:实现LinkedList. |
| 7 | + * @author zhanglifeng. |
| 8 | + */ |
| 9 | +public class LinkedList implements List { |
| 10 | + private Node head, tail; |
| 11 | + private int size; |
| 12 | + |
| 13 | + private Node getNodeByIndex(int index) { |
| 14 | + if (index < 0 || index > size - 1) { |
| 15 | + throw new IndexOutOfBoundsException("线性表索引越界"); |
| 16 | + } |
| 17 | + Node current = head; |
| 18 | + for (int i = 0; i < size && current != null; i++, current = current.next) { |
| 19 | + if (i == index) { |
| 20 | + return current; |
| 21 | + } |
| 22 | + } |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + public void add(Object o) { |
| 27 | + addLast(o); |
| 28 | + } |
| 29 | + |
| 30 | + public void add(int index, Object o) { |
| 31 | + if (index < 0 || index > size) { |
| 32 | + throw new IndexOutOfBoundsException("线性表索引越界"); |
| 33 | + } |
| 34 | + |
| 35 | + if (0 == index) { |
| 36 | + addFirst(o); |
| 37 | + }else{ |
| 38 | + Node node = getNodeByIndex(index - 1); |
| 39 | + node.next = new Node(o, node.next); |
| 40 | + size ++; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public Object get(int index) { |
| 45 | + return getNodeByIndex(index).data; |
| 46 | + } |
| 47 | + |
| 48 | + public Object remove(int index) { |
| 49 | + if (index < 0 || index > size - 1) { |
| 50 | + throw new IndexOutOfBoundsException("线性表索引越界"); |
| 51 | + } |
| 52 | + |
| 53 | + if(0 == index){ |
| 54 | + return removeFirst(); |
| 55 | + }else if(size - 1 == index){ |
| 56 | + return removeLast(); |
| 57 | + }else{ |
| 58 | + Node node = getNodeByIndex(index); |
| 59 | + Node preNode = getNodeByIndex(index - 1); |
| 60 | + preNode.next = node.next; |
| 61 | + size --; |
| 62 | + return node.data; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public int size() { |
| 67 | + return size; |
| 68 | + } |
| 69 | + |
| 70 | + public void addFirst(Object o) { |
| 71 | + Node currentHead = head; |
| 72 | + Node newNode = new Node(o, currentHead); |
| 73 | + head = newNode; |
| 74 | + if(currentHead == null){ |
| 75 | + tail = newNode; |
| 76 | + } |
| 77 | + |
| 78 | + size++; |
| 79 | + } |
| 80 | + |
| 81 | + public void addLast(Object o) { |
| 82 | + Node currentTail = tail; |
| 83 | + Node newNode = new Node(o, null); |
| 84 | + tail = newNode; |
| 85 | + if(currentTail == null){ |
| 86 | + head = newNode; |
| 87 | + }else { |
| 88 | + currentTail.next = newNode; |
| 89 | + } |
| 90 | + size++; |
| 91 | + } |
| 92 | + |
| 93 | + public Object removeFirst() { |
| 94 | + if(head == null){ |
| 95 | + throw new NoSuchElementException(); |
| 96 | + } |
| 97 | + Node node = new Node(head.data, null); |
| 98 | + head = head.next; |
| 99 | + size --; |
| 100 | + return node.data; |
| 101 | + } |
| 102 | + |
| 103 | + public Object removeLast() { |
| 104 | + if(tail == null){ |
| 105 | + throw new NoSuchElementException(); |
| 106 | + } |
| 107 | + Node node = getNodeByIndex(size - 1); |
| 108 | + node.next = null; |
| 109 | + size --; |
| 110 | + return node.data; |
| 111 | + } |
| 112 | + |
| 113 | + public Iterator iterator() { |
| 114 | + return new LinkedListIterator(this); |
| 115 | + } |
| 116 | + |
| 117 | + private static class Node { |
| 118 | + Object data; |
| 119 | + Node next; |
| 120 | + |
| 121 | + public Node(Object data, Node next) { |
| 122 | + this.data = data; |
| 123 | + this.next = next; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private class LinkedListIterator implements Iterator { |
| 128 | + LinkedList linkedList = null; |
| 129 | + private int current = 0; |
| 130 | + |
| 131 | + public LinkedListIterator(LinkedList linkedList) { |
| 132 | + this.linkedList = linkedList; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean hasNext() { |
| 137 | + return current < size; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Object next() { |
| 142 | + return linkedList.get(current ++); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public static void main(String[] args) { |
| 147 | + LinkedList linkedList = new LinkedList(); |
| 148 | + linkedList.add("s1"); |
| 149 | + linkedList.add("s2"); |
| 150 | + linkedList.add("s3"); |
| 151 | + linkedList.addFirst("s0"); |
| 152 | + linkedList.addLast("s4"); |
| 153 | + |
| 154 | + Iterator it = linkedList.iterator(); |
| 155 | + while(it.hasNext()){ |
| 156 | + System.out.print(it.next() + " "); |
| 157 | + } |
| 158 | + System.out.println(); |
| 159 | + System.out.println("第3个元素:" + linkedList.get(3)); |
| 160 | + |
| 161 | + System.out.println(linkedList.removeFirst()); |
| 162 | + System.out.println(linkedList.size()); |
| 163 | + System.out.println("Last element:" + linkedList.removeLast()); |
| 164 | + System.out.println(linkedList.size()); |
| 165 | + System.out.println("第2个元素:" + linkedList.remove(2)); |
| 166 | + System.out.println(linkedList.size()); |
| 167 | + } |
| 168 | +} |
0 commit comments