1、链表
我们可以用虚拟头结点 dummyHead 来简化添加、删除的操作
public class LinkedList<E> {
private class Node {
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private final Node dummyHead;
private int size;
public LinkedList() {
dummyHead = new Node();
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
/**
* 添加
*/
public void add(int index, E e) {
if (index < 0 || index > size) throw new RuntimeException("need 0 <= index <= size");
Node prev = dummyHead;
for (int i = 0; i < index; i++) prev = prev.next;
prev.next = new Node(e, prev.next);
size++;
}
public void addFirst(E e) {
add(0, e);
}
public void addLast(E e) {
add(size, e);
}
/**
* 删除
*/
public E remove(int index) {
if (index < 0 || index >= size) throw new RuntimeException("need 0 <= index < size");
Node prev = dummyHead;
for (int i = 0; i < index; i++) prev = prev.next;
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
return delNode.e;
}
public E removeFirst() {
return remove(0);
}
public E removeLast() {
return remove(size - 1);
}
/**
* 存在就删除
*/
public void removeElement(E e) {
Node prev = dummyHead;
while (prev.next != null) {
if (e.equals(prev.next.e)) {
prev.next = prev.next.next;
size--;
}
else prev = prev.next;
}
}
/**
* 修改
*/
public void set(int index, E e) {
if (index < 0 || index >= size) throw new RuntimeException("need 0 <= index < size");
Node cur = dummyHead.next;
for (int i = 0; i < index; i++) cur = cur.next;
cur.e = e;
}
/**
* 查看
*/
public E get(int index) {
if (index < 0 || index >= size) throw new RuntimeException("need 0 <= index < size");
Node cur = dummyHead.next;
for (int i = 0; i < index; i++) cur = cur.next;
return cur.e;
}
public E getFirst() {
return get(0);
}
public E getLast() {
return get(size - 1);
}
public boolean contains(E e) {
Node cur = dummyHead.next;
while (cur != null) {
if (cur.e.equals(e)) return true;
cur = cur.next;
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Node cur = dummyHead.next; cur != null; cur = cur.next) {
sb.append(cur).append("->");
}
sb.append("NULL");
return sb.toString();
}
}
2、链表栈
我们很容易基于上面的链表来实现栈,并且 push、pop、peek 的复杂度都是 O(1) 级别的,非常的酷
public class LinkedListStack<E> implements Stack<E> {
private final LinkedList<E> list;
public LinkedListStack() {
this.list = new LinkedList<>();
}
@Override
public void push(E e) {
list.addFirst(e);
}
@Override
public E pop() {
return list.removeFirst();
}
@Override
public E peek() {
return list.getFirst();
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LinkedListStack: Top [");
sb.append(list);
sb.append(']');
return sb.toString();
}
}
3、链表队列
我们使用两个变量 head 和 tail,分别指向链表的头和尾,用 tail 入队,用 head 出队
public class LinkedListQueue<E> implements Queue<E> {
private class Node {
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private Node head;
private Node tail;
private int size;
public LinkedListQueue() {
head = tail = null;
size = 0;
}
@Override
public void enqueue(E e) {
if (tail == null) head = tail = new Node(e);
else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
}
@Override
public E dequeue() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
Node retNode = head;
head = retNode.next;
retNode.next = null;
size--;
if (head == null) tail = null;
return retNode.e;
}
@Override
public E getFront() {
if (isEmpty()) throw new RuntimeException("Queue is empty");
return head.e;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LinkedListQueue: Front [");
for (Node cur = head; cur != null; cur = cur.next) {
sb.append(cur).append("->");
}
sb.append("NULL] Tail");
return sb.toString();
}
}
标签:Node,return,next,链表,Override,public,size
From: https://www.cnblogs.com/n139949/p/17302856.html