|
| 1 | +package cn.fyl.first; |
| 2 | + |
| 3 | +public class LinkedList implements List { |
| 4 | + |
| 5 | + private Node head,tail; //头尾结点 |
| 6 | + private int size; //保存链表大小 |
| 7 | + |
| 8 | + //将值插入链表 |
| 9 | + public void add(Object o) { |
| 10 | + add(size(),o); |
| 11 | + } |
| 12 | + |
| 13 | + //将值从index位置插入链表 |
| 14 | + public void add(int index, Object o) { |
| 15 | + if(index == 0){ |
| 16 | + addFirst(o); |
| 17 | + } |
| 18 | + else if(index >= size){ |
| 19 | + addLast(o); |
| 20 | + } |
| 21 | + else{ |
| 22 | + Node current = head; |
| 23 | + for (int i = 1; i < index; i++) { |
| 24 | + current = current.next; |
| 25 | + } |
| 26 | + Node temp = current.next; |
| 27 | + current.next = new Node(o); |
| 28 | + (current.next).next = temp; |
| 29 | + size++; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + //取出index位置的值 |
| 34 | + public Object get(int index) { |
| 35 | + if(index < 0 || index >=size){ |
| 36 | + return null; |
| 37 | + } |
| 38 | + else if(index == 0){ |
| 39 | + return head.data; |
| 40 | + } |
| 41 | + else if(index == size-1){ |
| 42 | + return tail.data; |
| 43 | + } |
| 44 | + else{ |
| 45 | + Node current = head; |
| 46 | + for (int i = 1; i < index; i++) { |
| 47 | + current = current.next; |
| 48 | + } |
| 49 | + Node temp = current.next; |
| 50 | + return temp.data; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + //删除index位置的值 |
| 55 | + public Object remove(int index) { |
| 56 | + if(index < 0 || index >size) |
| 57 | + return null; |
| 58 | + else if(index ==0) |
| 59 | + return head.data; |
| 60 | + else if(index == size -1) |
| 61 | + return tail.data; |
| 62 | + else{ |
| 63 | + Node previous = head; |
| 64 | + for (int i = 1; i < index; i++) { |
| 65 | + previous = previous.next; |
| 66 | + } |
| 67 | + Node current = previous.next; |
| 68 | + previous.next = current.next; |
| 69 | + size--; |
| 70 | + return current.data; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public int size() { |
| 75 | + return size; |
| 76 | + } |
| 77 | + |
| 78 | + //添加头结点 |
| 79 | + public void addFirst(Object o) { |
| 80 | + Node newNode = new Node(o); |
| 81 | + newNode.next = head; //指向头引用所指结点 |
| 82 | + head = newNode; //头引用指向新增结点 |
| 83 | + size++; |
| 84 | + if(tail == null){ |
| 85 | + tail = head; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + //添加尾结点 |
| 90 | + public void addLast(Object o) { |
| 91 | + Node newNode = new Node(o); |
| 92 | + if(tail == null){ |
| 93 | + head = tail = newNode; |
| 94 | + } |
| 95 | + else{ |
| 96 | + tail.next = newNode; |
| 97 | + tail = tail.next; |
| 98 | + } |
| 99 | + size++; |
| 100 | + } |
| 101 | + |
| 102 | + //删除头结点 |
| 103 | + public Object removeFirst(){ |
| 104 | + if(size == 0){ |
| 105 | + return null; |
| 106 | + } |
| 107 | + else if(size == 1){ |
| 108 | + Node temp = head; |
| 109 | + head = tail = null; |
| 110 | + size = 0; |
| 111 | + return temp.data; |
| 112 | + } |
| 113 | + else{ |
| 114 | + Node temp = head; |
| 115 | + head = head.next; |
| 116 | + size--; |
| 117 | + return temp.data; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + //删除尾结点 |
| 122 | + public Object removeLast() { |
| 123 | + if(size == 0){ |
| 124 | + return null; |
| 125 | + } |
| 126 | + else if(size ==1){ |
| 127 | + Node temp = head; |
| 128 | + head =tail =null; |
| 129 | + size =0; |
| 130 | + return temp.data; |
| 131 | + } |
| 132 | + else{ |
| 133 | + Node current = head; |
| 134 | + for (int i = 0; i < size - 2; i++) { |
| 135 | + current = current.next; |
| 136 | + } |
| 137 | + Node temp =tail; |
| 138 | + tail = current; |
| 139 | + tail.next = null; |
| 140 | + size--; |
| 141 | + return temp.data; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + private static class Node { |
| 148 | + Object data; |
| 149 | + Node next; |
| 150 | + public Node(Object o){ |
| 151 | + data = o; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public static void main(String[] arg){ |
| 156 | + LinkedList l = new LinkedList(); |
| 157 | + l.add(0); |
| 158 | + l.add(2); |
| 159 | + l.add(4); |
| 160 | + l.add(3, 1); |
| 161 | + System.out.println(l.removeLast()+" "+l.removeFirst()+" "+l.get(0)+" "+l.get(1)); |
| 162 | + } |
| 163 | +} |
0 commit comments