package com.panjiahao;
public class DoubleLinkLst {
private static class linkNode{
int value;
linkNode prev;
linkNode next;
public linkNode(int value) {
this.value = value;
}
}
linkNode head;
linkNode tail;
//头插法
public void addFirst(int data){
linkNode node=new linkNode(data);
if(head==null){
head=tail=node;
}else {
node.next = head;
head.prev = node;
head = node;
}
}
//尾插法
public void addLast(int data){
linkNode node=new linkNode(data);
if(head==null){
head=tail=node;
}else {
tail.next=node;
node.prev=tail;
tail = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
checkIndex(index);
if (index == 0) {//插在头节点的前面
addFirst(data);
}else if(index==size()){//插在尾节点的后面
addLast(data);
}else{//插在中间
//先将结点移动到要插入的位置前面
linkNode current=head;
while(index>0){
current=current.next;
index--;
}
//再将新结点插入到链表中
linkNode node=new linkNode(data);
current.prev.next=node;
node.next=current;
node.prev=current.prev;
current.prev=node;
}
}
private void checkIndex(int index){
if(index<0||index>size())
throw new ArrayIndexOutOfBoundsException("index索引不合法index="+index);
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
if(head==null){
return false;
}
linkNode current=head;
while(current!=null){
if(current.value==key){
return true;
}
current=current.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (contains(key)) {//先判断链表中是否含有元素key
linkNode current = head;
while (current != null) {
if (current.value == key) {
//判断节点的位置
if (current == head) {//删除的结点是头结点
head = head.next;
if(head==null){
tail=null;
return;
}
head.prev.next = null;
head.prev = null;
}
else if (current == tail) {//删除的结点是为节点
tail = tail.prev;
tail.next.prev = null;
tail.next = null;
}
else {//删除的结点在链表的中间
current.prev.next=current.next;
current.next.prev=current.prev;
current.prev=current.next=null;
}
break;
}
current=current.next;//继续循环遍历
}//while循环结束
}
}
//删除所有值为key的节点
public void removeAllKey(int key){
if(head==null){
return;
}
//先处理头尾结点
while(true){
if(head.value==key){
head=head.next;
if(head!=null) {
head.prev.next = null;
head.prev = null;
}else{
break;
}
}else if(tail.value==key){
tail=tail.prev;
tail.next.prev=null;
tail.next=null;
}else{
break;
}
}
linkNode current =head;
int count=0;
while(current!=null){
count++;
current=current.next;
}
if(count>=3){
linkNode tmp=head.next;
linkNode nextnode=tmp;
while(nextnode.next!=null){
nextnode=nextnode.next;
if(tmp.value==key){
tmp.prev.next=tmp.next;
tmp.next.prev=tmp.prev;
tmp.next=tmp.prev=null;
}
tmp=nextnode;
}
}
}
//得到单链表的长度
public int size(){
linkNode current=head;
int count =0;
while(current!=null){
count++;
current=current.next;
}
return count;
}
public void clear(){
linkNode current=head;
while(current!=null){
head=head.next;
if(head!=null) {
head.prev.next = null;
head.prev = null;
}
current=current.next;
}
head=tail=null;
}
public void display(){
StringBuilder sb=new StringBuilder();
sb.append("[");
linkNode current=head;
while(current!=null){
sb.append(current.value);
if(current.next!=null){
sb.append(",");
}
current=current.next;
}
sb.append("]");
System.out.println(sb);
}
}
数据结构双链表的模拟实现
最新推荐文章于 2024-07-18 17:57:16 发布