单向链表
package com.jiaxinli.linkedlist;
public class SingleLinkedList {
private static class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
}
}
public Node head;
public Node create() {
// 创建节点
Node node1 = new Node(12);
Node node2 = new Node(23);
Node node3 = new Node(34);
Node node4 = new Node(45);
Node node5 = new Node(56);
// 建立引用
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
head = node1;
return head;
}
//头插法
public void addFirst(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
//尾插法
public void addLast(int data) {
Node node = new Node(data);
if (head == null) {
head = node;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
checkRange(index);
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
Node prevNode = findPrevNodeByIndex(index);
Node node = new Node(data);
node.next = prevNode.next;
prevNode.next = node;
}
private Node findPrevNodeByIndex(int index) {
Node current = head;
for (int i = 0; i < (index - 1); i++) {
current = current.next;
}
return current;
}
private void checkRange(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("下标不合法, index = " + index);
}
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
Node current = head;
while (current != null) {
if (current.value == key) {
return true;
}
current = current.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (head.value == key) {
head = head.next;
return;
}
Node current = head;
while (current.next != null) {
if (current.next.value == key) {
Node delNode = current.next;
current.next = delNode.next;
return;
}
current = current.next;
}
}
//删除所有值为key的节点
public void removeAllKey(int key) {
if (head == null) {
return;
}
Node prevNode = head;
Node current = head.next;
while (current != null) {
if (current.value == key) {
prevNode.next = current.next;
} else {
prevNode = current;
}
current = current.next;
}
if (head.value == key) {
head = head.next;
}
}
//得到单链表的长度
public int size() {
Node current = head;
int count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public void display() {
Node current = head;
StringBuilder sb = new StringBuilder();
sb.append("[");
while (current != null) {
sb.append(current.value);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append("]");
System.out.println(sb.toString());
}
public void clear() {
Node current = head;
while (current != null) {
Node nextNode = current.next;
current.next = null;
current = nextNode;
}
head = null;
}
}
双向链表
package com.jiaxinli.doublelinkedlist;
public class DoubleLinkedList {
// 定义双向链表的节点
private static class ListNode {
int value;
ListNode prev;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
// 头节点
public ListNode head;
// 尾节点
public ListNode tail;
//头插法
public void addFirst(int data){
ListNode Node=new ListNode(data);
if (head == null) {
head=Node;
tail=Node;
}else{
Node.next=head;
head.prev=Node;
head=Node;
}
}
//尾插法
public void addLast(int data){
ListNode Node=new ListNode(data);
if (head == null) {
head=Node;
tail=Node;
}else{
tail.next=Node;
Node.prev=tail;
tail=Node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
//判断是否合法
checkRangeForAdd(index);
if (index == 0) {
addFirst(data);
}
if (index == size()) {
addLast(data);
}
//判断index的位置
ListNode tempNode = findNodeByIndex(index);
ListNode Node = new ListNode(data);
Node.next = tempNode;
Node.prev = tempNode.prev;
tempNode.prev.next = Node;
tempNode.prev = Node;
}
private ListNode findNodeByIndex(int index) {
ListNode current=head;
if(index>0){
current=current.next;
index--;
}
return current;
}
private void checkRangeForAdd(int index) {
if (index < 0 || index > size()) {
System.out.println("下标不合法");
}
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
if(head==null){
return false;
}
ListNode current=head;
while(current!=null){
if(current.value==key){
return true;
}
current=current.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
ListNode current=head;
while(current!=null){
if(current.value==key){
if (current == head) {
head=head.next;
if(head==null){
tail=null;
}else{
head.prev.next=null;
head.prev=null;
}
}else if(current==tail){
tail=tail.prev;
tail.next=null;
}else{
current.next.prev=current.prev;
current.prev.next=current.next;
}
return;
}
current=current.next;
}
}
//删除所有值为key的节点
public void removeAllKey(int key){
ListNode current=head;
while(current!=null){
if(current.value==key){
if (current == head) {
head = head.next;
// 删除头节点
if (head == null) {
// 链表已经为空
tail = null;
} else {
// 只剩一个节点
head.prev = null;
}
} else if (current == tail) {
// 处理尾节点
tail = tail.prev;
tail.next = null;
} else {
current.next.prev = current.prev;
current.prev.next = current.next;
}
}
// 继续遍历
current = current.next;
}
}
//得到单链表的长度
public int size(){
ListNode current=head;
int count=0;
if (current != null) {
current=current.next;
count++;
}
return count;
}
public void display(){
StringBuilder sb=new StringBuilder();
sb.append("[");
ListNode current=head;
while(current!=null){
sb.append(current.value);
if(current.next!=null){
sb.append(",");
}
current=current.next;
}
sb.append("]");
System.out.println(sb);
}
public void clear(){
head=null;
tail=null;
}
}