9.19单链表带哨兵和双向链表带哨兵

1.单链表

public class Main {
public static void main(String[] args) {
LNode L = new LNode();
L.addFirst(4);//头插
L.addFirst(3);
L.addFirst(2);
L.addFirst(1);
L.addLast(5);//尾插
L.Isempty();//判空
L.query();//遍历
System.out.println();
System.out.println(L.findLast().value);//最后一个结点
LNode.Node L2 = L.findNode(0);//指定索引查询
if (L2 == null) {
System.out.println("未查询到指定索引");
} else {
System.out.println(L2.value);
}
int i = L.valuecheck(1);
if (i == -1) {
System.out.println("不存在指定元素");
} else {
System.out.println("存在该元素,索引为" + i);
}
System.out.println("链表大小" + L.number());
L.removeIndex(0);
L.removeValue(2);
L.insertIndex(0, 2);
L.insertIndex(4, 6);
L.removeFirst();
L.removeLast();
L.query();

}

public static class LNode {
private Node head = new Node(-1, null);//头指针

public class Node {
int value;
Node next;

public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}

//头插
public void addFirst(int value) {
head.next = new Node(value, head.next);
/* insertIndex(0,value);*/
}

//尾插
public void addLast(int value) {
Node node = findLast();
node.next = new Node(value, null);
}

//头删
public void removeFirst() {
removeIndex(0);
}

//尾删
public void removeLast() {
removeIndex(number() - 1);
}

//查找指定节点
public Node findNode(int index) {
int i = -1;
for (Node p = head; p != null; p = p.next) {
if (i == index) {
return p;
}
i++;
}
return null;
}

//找最后一个节点
public Node findLast() {
Node p;
for (p = head; p.next != null; p = p.next) {
}
return p;
}

//遍历
public void query() {
for (Node p = head.next; p != null; p = p.next) {
System.out.print(p.value + " ");
}
}

//返回链表实际元素长度不含头结点
public int number() {
int i = 0;
for (Node p = head.next; p != null; p = p.next) {
i++;
}
return i;
}

//查找指定值所在索引
public int valuecheck(int value1) {
int i = -1;
for (Node p = head.next; p != null; p = p.next) {
i++;
if (p.value == value1) {
return i;
}
}
return -1;
}

//删除指定索引节点
public void removeIndex(int index) {
Node prev = findNode(index - 1);
if (prev == null) {
System.out.println("空链表");
return;
}
Node removed = prev.next;
if (removed == null) {
System.out.println("索引不存在");
return;
}
prev.next = removed.next;
}

//删除指定值节点
public void removeValue(int value1) {
int i = -1;
int j = 0;
for (Node p = head.next; p != null; p = p.next) {
i++;
if (value1 == p.value) {
j++;
removeIndex(i);
}
}
if (j == 0) {
System.out.println("该值不存在");
}
}

//按索引插入指定值
public void insertIndex(int index, int value) {
Node prev = findNode(index - 1);
if (prev == null) {
System.out.println("插入失败");
return;
}
prev.next = new Node(value, prev.next);
}

//判空
public void Isempty() {
if (head.next == null) {
System.out.println("空链表");
} else {
System.out.println("非空链表");
}
}

}
}
 
2.双向链表

//优势操作最后节点方法简单,因为尾节点可知
public class Main {
public static void main(String[] args) {
DoubleLinkedList d=new DoubleLinkedList();
d.insertIndex(0,2);
d.addFirst(1);
d.addFirst(0);
d.addLast(3);
/* d.removeFirst();
d.removeIndex(0);
d.removeLast();
System.out.println(d.findNode(1).value);*/
d.query();

}

public static class DoubleLinkedList {
public static class Node {
Node prev;//上一个节点指针
int value;//值
Node next;//下一个节点指针

public Node(Node prev, int value, Node next) {
this.prev = prev;
this.value = value;
this.next = next;
}
public Node() {

}
}
private Node head;//头哨兵
private Node tail;//尾哨兵
public DoubleLinkedList()
{
head=new Node(null,-1,null);
tail=new Node(null,-1,null);
head.next=tail;
tail.prev=head;
}
public Node findNode(int index)
{
int i=-1;
for(Node p=head;p!=tail;p=p.next)
{
if(i==index)
{
return p;
}
i++;
}
return null;
}
public void insertIndex(int index,int value)
{
Node prev=findNode(index-1);
if(prev==null)
{
System.out.println("索引错误");
return ;
}
Node p=new Node(prev,value,prev.next);
prev.next.prev=p;
prev.next=p;
}
public void addFirst(int value)
{
insertIndex(0,value);
}
public void addLast(int value)
{
Node p=new Node(tail.prev,value,tail);
tail.prev.next=p;
tail.prev=p;
}
public void removeFirst()
{
Node remove=head.next;
remove.next.prev=head;
head.next=remove.next;
}
public void removeLast()
{
Node remove=tail.prev;
remove.prev.next=tail;
tail.prev=remove.prev;
}
public void removeIndex(int index)
{
Node prev=findNode(index-1);//被删除元素的前一个
if(prev==null)
{
System.out.println("删除失败");
return ;
}
Node remove=prev.next;//被删除元素
if(prev==tail)
{
System.out.println("删除失败");
return ;
}
Node Next=remove.next;//被删除元素的后一个
prev.next=Next;
Next.prev=prev;
}
public void query()
{
for(Node p=head.next;p!=tail;p=p.next)
{
System.out.print(p.value+" ");
}
}
}
}
posted @ 2023-09-19 21:42  赵千万  阅读(15)  评论(0编辑  收藏  举报