目录
定义一个双向链表
public class DoubleLinkedList {
// 当前链表中有效的元素个数
private int size;
// 头节点
private Node head;
// 尾节点
private Node tail;
}
定义双向链表的节点类
/**
* 双链表的节点类
*/
class Node {
//前驱节点
Node prev;
//保存具体值
int val;
//后继节点
Node next;
public Node(int val) {
this.val = val;
}
public Node(Node prev, int val, Node next) {
this.prev = prev;
this.val = val;
this.next = next;
}
}
增
-
头插法
/**
* 头插法
* @param val
*/
public void addFirst(int val){
Node node = new Node(null,val,head);
if (head == null){
tail = node;
}else{
head.prev = node;
}
head = node;
size++;
}
-
尾插法
/**
* 尾插法
* @param val
*/
public void addLast(int val){
Node node = new Node(tail,val,null);
if (tail == null){
head = node;
}else {
tail.next = node;
}
tail = node;
size++;
}
-
根据索引插入
/**
* 在index位置插入节点
* @param val
*/
public void addIndex(int index,int val) {
if (index<0 || index>size) {
System.err.println("索引非法!");
return;
} else if (index == 0) addFirst(val);
else if (index == size) addLast(val);
else {
Node prev = findNode(index - 1);
Node newNode = new Node(prev,val,prev.next);
prev.next.prev = newNode;
prev.next = newNode;
size++;
}
}
-
测试
/**
* @author happy
*/
public class Test {
public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.addFirst(1);
doubleLinkedList.addFirst(2);
doubleLinkedList.addLast(3);
doubleLinkedList.addLast(4);
//2->1->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.addIndex(0,5);
doubleLinkedList.addIndex(2,7);
doubleLinkedList.addIndex(6,9);
//5->2->7->1->3->4->9->null
System.out.println(doubleLinkedList);
}
}
查
-
根据index查找
/**
* 根据索引来查找,返回查找到的值
* @param index
* @return
*/
public int getIndex(int index){
if (indexCheck(index)){
return findNode(index).val;
}else {
System.out.println("索引非法!");
return -1;
}
}
-
根据val查找
/**
* 根据val值查找,返回true/false
* @param val
* @return
*/
public boolean contains(int val){
for (Node node = head; node != null ; node = node.next) {
if (node.val == val){
return true;
}
}
return false;
}
-
测试
/**
* @author happy
*/
public class Test {
public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.addFirst(1);
doubleLinkedList.addFirst(2);
doubleLinkedList.addLast(3);
doubleLinkedList.addLast(4);
//2->1->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.addIndex(0,5);
doubleLinkedList.addIndex(2,7);
doubleLinkedList.addIndex(6,9);
//5->2->7->1->3->4->9->null
System.out.println(doubleLinkedList);
//5
System.out.println(doubleLinkedList.getIndex(0));
//7
System.out.println(doubleLinkedList.getIndex(2));
//9
System.out.println(doubleLinkedList.getIndex(6));
//true
System.out.println(doubleLinkedList.contains(3));
//false
System.out.println(doubleLinkedList.contains(10));
}
}
改
-
根据index修改
/**
* 根据index修改
* @param index
* @param newVal
* @return
*/
public int set(int index,int newVal){
if (indexCheck(index)){
Node node = findNode(index);
int oldVal = node.val;
node.val = newVal;
return oldVal;
}else {
System.out.println("索引非法!");
return -1;
}
}
-
测试
/**
* @author happy
*/
public class Test {
public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.addFirst(1);
doubleLinkedList.addFirst(2);
doubleLinkedList.addLast(3);
doubleLinkedList.addLast(4);
//2->1->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.addIndex(0,5);
doubleLinkedList.addIndex(2,7);
doubleLinkedList.addIndex(6,9);
//5->2->7->1->3->4->9->null
System.out.println(doubleLinkedList);
//5
System.out.println(doubleLinkedList.getIndex(0));
//7
System.out.println(doubleLinkedList.getIndex(2));
//9
System.out.println(doubleLinkedList.getIndex(6));
//true
System.out.println(doubleLinkedList.contains(3));
//false
System.out.println(doubleLinkedList.contains(10));
doubleLinkedList.set(0,3);
doubleLinkedList.set(6,10);
doubleLinkedList.set(3,5);
//3->2->7->5->3->4->10->null
System.out.println(doubleLinkedList);
}
}
删
-
删除头节点
public void removeFirst(){
removeIndex(0);
}
-
删除尾节点
public void removeLast(){
removeIndex(size-1);
}
-
删除index节点
/**
* 删除index位置的节点
* @param index
*/
public void removeIndex(int index){
if (indexCheck(index)){
Node node = findNode(index);
removeNode(node);
}else {
System.out.println("索引非法!");
}
}
/**
-
删除指定节点
/**
* 删除指定节点,分治法
* @param node
*/
private void removeNode(Node node){
Node prev = node.prev;
Node next = node.next;
if (prev == null){
head = next;
}else {
//有前驱节点
//先处理前驱节点
prev.next = next;
node.prev = null;
}
if (next == null){
tail = prev;
}else {
next.prev = prev;
node.next = null;
}
size--;
}
-
测试
/**
* @author happy
*/
public class Test {
public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.addFirst(1);
doubleLinkedList.addFirst(2);
doubleLinkedList.addLast(3);
doubleLinkedList.addLast(4);
//2->1->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.addIndex(0,5);
doubleLinkedList.addIndex(2,7);
doubleLinkedList.addIndex(6,9);
//5->2->7->1->3->4->9->null
System.out.println(doubleLinkedList);
//5
System.out.println(doubleLinkedList.getIndex(0));
//7
System.out.println(doubleLinkedList.getIndex(2));
//9
System.out.println(doubleLinkedList.getIndex(6));
//true
System.out.println(doubleLinkedList.contains(3));
//false
System.out.println(doubleLinkedList.contains(10));
doubleLinkedList.set(0,3);
doubleLinkedList.set(6,10);
doubleLinkedList.set(3,5);
//3->2->7->5->3->4->10->null
System.out.println(doubleLinkedList);
doubleLinkedList.removeIndex(0);
doubleLinkedList.removeIndex(2);;
doubleLinkedList.removeIndex(4);
//2->7->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.removeFirst();
//7->3->4->null
System.out.println(doubleLinkedList);
doubleLinkedList.removeLast();
//7->3->null
System.out.println(doubleLinkedList);
}
}
判断索引是否合法
public boolean indexCheck(int index){
if (index<0 || index>=size)
return false;
return true;
}
找到index位置的节点----二分法
/**
* 找到index索引对应的节点,使用二分查找
* @param index
* @return index对应的节点Node
*/
private Node findNode(int index) {
Node ret = null;
if(index < (size >> 1)){
ret = head;
for (int i = 0; i <index ; i++) {
ret = ret.next;
}
}else {
ret = tail;
for (int i = size-1; i > index ; i--) {
ret = ret.prev;
}
}
return ret;
}
toString()方法
public String toString(){
String ret = "";
Node node = head;
while (node != null){
ret += node.val + "->";
node = node.next;
}
ret += null;
return ret;
}
全部代码
public class DoubleLinkedList {
// 当前链表中有效的元素个数
private int size;
// 头节点
private Node head;
// 尾节点
private Node tail;
/**
* 头插法
* @param val
*/
public void addFirst(int val){
Node node = new Node(null,val,head);
if (head == null){
tail = node;
}else{
head.prev = node;
}
head = node;
size++;
}
/**
* 尾插法
* @param val
*/
public void addLast(int val){
Node node = new Node(tail,val,null);
if (tail == null){
head = node;
}else {
tail.next = node;
}
tail = node;
size++;
}
/**
* 在index位置插入节点
* @param val
*/
public void addIndex(int index,int val) {
if (index<0 || index>size) {
System.err.println("索引非法!");
return;
} else if (index == 0) addFirst(val);
else if (index == size) addLast(val);
else {
Node prev = findNode(index - 1);
Node newNode = new Node(prev,val,prev.next);
prev.next.prev = newNode;
prev.next = newNode;
size++;
}
}
/**
* 根据索引来查找,返回查找到的值
* @param index
* @return
*/
public int getIndex(int index){
if (indexCheck(index)){
return findNode(index).val;
}else {
System.out.println("索引非法!");
return -1;
}
}
/**
* 根据val值查找,返回true/false
* @param val
* @return
*/
public boolean contains(int val){
for (Node node = head; node != null ; node = node.next) {
if (node.val == val){
return true;
}
}
return false;
}
/**
* 根据index修改
* @param index
* @param newVal
* @return
*/
public int set(int index,int newVal){
if (indexCheck(index)){
Node node = findNode(index);
int oldVal = node.val;
node.val = newVal;
return oldVal;
}else {
System.out.println("索引非法!");
return -1;
}
}
public void removeFirst(){
removeIndex(0);
}
public void removeLast(){
removeIndex(size-1);
}
/**
* 删除index位置的节点
* @param index
*/
public void removeIndex(int index){
if (indexCheck(index)){
Node node = findNode(index);
removeNode(node);
}else {
System.out.println("索引非法!");
}
}
/**
* 删除指定节点,分治法
* @param node
*/
private void removeNode(Node node){
Node prev = node.prev;
Node next = node.next;
if (prev == null){
head = next;
}else {
//有前驱节点
//先处理前驱节点
prev.next = next;
node.prev = null;
}
if (next == null){
tail = prev;
}else {
next.prev = prev;
node.next = null;
}
size--;
}
public boolean indexCheck(int index){
if (index<0 || index>=size)
return false;
return true;
}
/**
* 找到index索引对应的节点,使用二分查找
* @param index
* @return index对应的节点Node
*/
private Node findNode(int index) {
Node ret = null;
if(index < (size >> 1)){
ret = head;
for (int i = 0; i <index ; i++) {
ret = ret.next;
}
}else {
ret = tail;
for (int i = size-1; i > index ; i--) {
ret = ret.prev;
}
}
return ret;
}
public String toString(){
String ret = "";
Node node = head;
while (node != null){
ret += node.val + "->";
node = node.next;
}
ret += null;
return ret;
}
}
/**
* 双链表的节点类
*/
class Node {
//前驱节点
Node prev;
//保存具体值
int val;
//后继节点
Node next;
public Node(int val) {
this.val = val;
}
public Node(Node prev, int val, Node next) {
this.prev = prev;
this.val = val;
this.next = next;
}
}