无头双向链表的相关操作(Java实现)
程序员文章站
2024-03-22 11:03:34
...
package singleList;
//无头双向链表
public class DoubleLinkedList {
class Node{
private int data;
private Node next;
private Node prev;
public Node(int data){
this.data = data;
this.next = null;
this.prev = null;
}
}
private Node head;
private Node last;
public DoubleLinkedList(){
this.head = null;
}
//1.头插,head变,last不变
public void addFirst(int data){
Node node = new Node(data);
if(head == null){
this.head = node;
this.last = node;
}else {
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
//2.尾插,头不变,last变
public void addLast(int data){
Node node = new Node(data);
if(this.last == null){
this.last = node;
this.head = node;
}else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
//3.获取链表长度
public int getLength(){
Node cur = this.head;
int len = 0;
while(cur != null){
len++;
cur = cur.next;
}
return len;
}
//4.在指定索引位置插入数据,只能在[0,lenth]区间插入
//如要在: 1->2->3->4->null 的index=2位置插入data=5,则为:1->2->5->3->4->null
public boolean addIndex(int index, int data){
if(index < 0 || index > this.getLength()){
System.out.println("插入位置不合法!");
return false;
}
if(index == 0){
addFirst(data);
return true;
}
if(index == this.getLength()) {
addLast(data);
return true;
}
Node node = new Node(data);
Node cur = this.head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
cur.prev.next = node;
node.next = cur;
node.prev = cur.prev;
cur.prev = node;
return true;
}
//5.是否包含key
public boolean contains(int key){
Node cur = this.head;
while(cur != null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
//6.移除链表中等于key的第一个元素节点
public boolean remove(int key){
if(this.head == null){
return false;
}
// 1->2->4
Node cur = this.head;
while(cur != null){
if(cur.data == key){
if(cur == this.head){
//是头节点
if(cur.next == null){
//只有一个元素
this.head = this.last = null;
}else {
cur.next.prev = null;
this.head = cur.next;
}
return true;
}else {
cur.prev.next = cur.next;
if(cur.next == null){
//是尾节点
this.last = cur.prev;
}else {
cur.next.prev = cur.prev;
return true;
}
}
return true;
}
cur = cur.next;
}
return false;
}
//7.移除链表中所有等于key的节点
public void removeAllKey(int key){
if(this.head == null){
return ;
}
Node cur = this.head;
while (cur != null){
if(cur.data == key){
//移除头节点
if(cur == this.head){
if(cur.next == null){
//只有一个元素
this.head = this.last = null;
return ;
}else {
cur.next.prev = null;
this.head = cur.next;
}
}else {
cur.prev.next = cur.next;
if(cur.next == null){
this.last = cur.prev;
}else {
cur.next.prev = cur.prev;
}
}
}
cur = cur.next;
}
}
//8.清除链表
public void clear(){
while(this.head != null){
Node cur = this.head.next;
this.head.next = null;
this.head.prev = null;
this.head = cur;
}
this.last = null;
}
//9.打印双向链表
public void display(){
if(this.head == null){
System.out.println("链表为空!");
}else {
Node cur = this.head;
while(cur != null){
System.out.print(cur.data);
System.out.print("->");
cur = cur.next;
}
System.out.println();
}
}
public static void main(String[] args) {
DoubleLinkedList linkedList = new DoubleLinkedList();
linkedList.addLast(1);
linkedList.addLast(2);
linkedList.addLast(3);
linkedList.addLast(3);
linkedList.addLast(4);
linkedList.addLast(5);
linkedList.addLast(5);
linkedList.addLast(6);
linkedList.clear();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}