双向链表
JAVA实现
package com.wc4cloud.list;
import java.util.Iterator;
public class LinkedList<T> implements Iterable<T> {
private Node<T> first;
private Node<T> last;
private long size;
public LinkedList() {
first = new Node<>(null, null, null);
last = null;
size = 0;
}
public void clear() {
first.next = null;
last = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public T getFirst() {
if (isEmpty()) {
return null;
}
return first.next.value;
}
public T getLast() {
if (isEmpty()) {
return null;
}
return last.value;
}
public boolean insert(T value) {
if (isEmpty()) {
last = new Node<>(first, value, null);
first.next = last;
} else {
Node<T> newNode = new Node<>(last, value, null);
last.next = newNode;
last = newNode;
}
size++;
return true;
}
public boolean insert(T value, int index) {
if (index < size) {
Node<T> prev = first;
for (int i = 0; i < index; i++) {
prev = prev.next;
}
Node<T> current = prev.next;
Node<T> newNode = new Node<>(prev, value, current);
prev.next = newNode;
current.prev = newNode;
size++;
return true;
} else {
return insert(value);
}
}
public T get(int index) {
if (index >= size) {
return null;
}
Node<T> node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node.next.value;
}
public int indexOf(T value) {
Node<T> node = first;
for (int i = 0; node.next != null; i++) {
node = node.next;
if (node.value.equals(value)) {
return i;
}
}
return -1;
}
public T remove(int index) {
if (index < size) {
Node<T> node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
Node<T> current = node.next;
current.prev.next = current.next;
size--;
return current.value;
}
return null;
}
public boolean set(T value, int index) {
if (index >= size) {
return false;
}
Node<T> node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
node.next.value = value;
return true;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private Node<T> node = first;
@Override
public boolean hasNext() {
return node.next != null;
}
@Override
public T next() {
node = node.next;
return node.value;
}
};
}
private static class Node<T> {
private Node<T> prev;
private T value;
private Node<T> next;
public Node(Node<T> prev, T value, Node<T> next) {
this.prev = prev;
this.value = value;
this.next = next;
}
}
}
快慢指针判断环形链表
package com.wc4cloud.list;
public class CircularLinkedList<T> {
private static class Node<T> {
private Node<T> prev;
private T value;
private Node<T> next;
public Node(Node<T> prev, T value, Node<T> next) {
this.prev = prev;
this.value = value;
this.next = next;
}
}
public static void main(String[] args) {
Node<Integer> head = new Node<>(null, 0, null);
Node<Integer> node = head;
for (int i = 1; i < 41; i++) {
node.next = new Node<>(node, i, null);
node = node.next;
}
Node<Integer> circular = head;
for (int i = 0; i < 15; i++) {
circular = circular.next;
}
node.next = circular;
Node<Integer> slow = head;
Node<Integer> fast = head;
Node<Integer> temp = null;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
System.out.println("队列为环形队列");
temp = head;
continue;
}
if (temp != null) {
temp = temp.next;
if (temp == slow) {
System.out.println("环形队列入口为:" + temp.value);
break;
}
}
}
}
}
Golang实现
package list
type Node struct {
prev *Node
value interface{}
next *Node
}
func NewNode(prev *Node, value interface{}, next *Node) *Node {
return &Node{prev: prev, value: value, next: next}
}
type LinkedList struct {
first *Node
last *Node
size int
}
func NewLinkedList() *LinkedList {
first := NewNode(nil, nil, nil)
size := 0
return &LinkedList{first: first, last: nil, size: size}
}
func (l *LinkedList) Clear() {
l.first.next = nil
l.last = nil
l.size = 0
}
func (l *LinkedList) IsEmpty() bool {
return l.size == 0
}
func (l *LinkedList) Size() int {
return l.size
}
func (l *LinkedList) GetFirst() (value interface{}) {
if l.IsEmpty() {
return nil
}
return l.first.next.value
}
func (l *LinkedList) GetLast() interface{} {
if l.IsEmpty() {
return nil
}
return l.last.value
}
func (l *LinkedList) Insert(value interface{}) bool {
if l.IsEmpty() {
l.last = NewNode(l.first, value, nil)
l.first.next = l.last
} else {
newNode := NewNode(l.last, value, nil)
l.last.next = newNode
l.last = newNode
}
l.size++
return true
}
func (l *LinkedList) InsertIndex(value interface{}, index int) bool {
if index < l.size {
prev := l.first
for i := 0; i < index; i++ {
prev = prev.next
}
current := prev.next
newNode := NewNode(prev, value, current)
prev.next = newNode
current.prev = newNode
l.size++
return true
} else {
return l.Insert(value)
}
}
func (l *LinkedList) Get(index int) interface{} {
if index >= l.size {
return nil
}
node := l.first
for i := 0; i < index; i++ {
node = node.next
}
return node.next.value
}
func (l *LinkedList) IndexOf(value interface{}) int {
node := l.first
for i := 0; node.next != nil; i++ {
node = node.next
if node.value == value {
return i
}
}
return -1
}
func (l *LinkedList) Remove(index int) interface{} {
if index < l.size {
node := l.first
for i := 0; i < index; i++ {
node = node.next
}
current := node.next
current.prev.next = current.next
l.size--
return current.value
}
return nil
}
func (l *LinkedList) Set(value interface{}, index int) bool {
if index >= l.size {
return false
}
node := l.first
for i := 0; i < index; i++ {
node = node.next
}
node.next.value = value
return false
}