java 双向链表删除_java中双向链表的增、删、查操作

import java.util.NoSuchElementException;

public class DoublyLinkedListImpl {

private Node head;// sentinel before first item

private Node tail;// sentinel after last item

private int size;// number of elements on list

public DoublyLinkedListImpl() {

size = 0;

}

/**

* this class keeps track of each element information

*

* @author java2novice

*

*/

private class Node {

E element;

Node next;

Node prev;

public Node(E element, Node next, Node prev) {

this.element = element;

this.next = next;

this.prev = prev;

}

}

/**

* returns the size of the linked list

*

* @return

*/

public int size() {

return size;

}

/**

* return whether the list is empty or not

*

* @return

*/

public boolean isEmpty() {

return size == 0;

}

/**

* adds element at the starting of the linked list

*

* @param element

*/

public void addFirst(E element) {

Node tmp = new Node(element, head, null);

if (head != null) {

head.prev = tmp;

}// 跟原来的头节点交换位置

head = tmp;

if (tail == null) {

tail = tmp;

}// 首次添加节点的时候,头节点就是尾节点

size++;

System.out.println("adding: " + element);

}

/**

* adds element at the end of the linked list

*

* @param element

*/

public void addLast(E element) {

Node tmp = new Node(element, null, tail);

if (tail != null) {

tail.next = tmp;

}// 跟原来的尾节点交换位置

tail = tmp;

if (head == null) {

head = tmp;

}// 首次添加节点的时候,头节点就是尾节点

size++;

System.out.println("adding: " + element);

}

/**

* get element at the specified location of the linked list

*

* @param loc

*/

public Node getElement(int loc) {

Node tmp = head;

int index = 0;

while (loc != index) {

tmp = tmp.next;

index++;

}

return tmp;

}

/**

* add element at the specified location of the linked list

*

* @param element

* @param loc

*/

public void insertAfterElement(E element, int loc) {

if (loc == size) {

addLast(element);

}

if (loc == 0) {

addFirst(element);

}

Node preEle = getElement(loc);

Node nextEle = preEle.next;

Node tmp = new Node(element, null, preEle);

// preEle=tmp.prev;//这一行没必要,因为创建类的时候已经指定preEle

preEle.next = tmp;

nextEle.prev = tmp;

tmp.next = nextEle;

size++;

System.out.println("inserting: " + element);

}

/**

* this method walks forward through the linked list

*/

public void iterateForward() {

System.out.println("iterating forward..");

Node tmp = head;

while (tmp != null) {

System.out.println(tmp.element);

tmp = tmp.next;

}

}

/**

* this method walks backward through the linked list

*/

public void iterateBackward() {

System.out.println("iterating backword..");

Node tmp = tail;

while (tmp != null) {

System.out.println(tmp.element);

tmp = tmp.prev;

}

}

/**

* this method removes element from the start of the linked list

*

* @return

*/

public E removeFirst() {

if (size == 0)

throw new NoSuchElementException();

Node tmp = head;

head = head.next;

head.prev = null;

size--;

System.out.println("deleted: " + tmp.element);

return tmp.element;

}

/**

* this method removes element from the end of the linked list

*

* @return

*/

public E removeLast() {

if (size == 0)

throw new NoSuchElementException();

Node tmp = tail;

tail = tail.prev;

tail.next = null;

size--;

System.out.println("deleted: " + tmp.element);

return tmp.element;

}

/**

* removes element from the specified location of the linked list

*

* @return

*/

public E removeByIndex(int loc) {

Node tmp = null;

if (loc >= size || loc < 0) {

throw new NoSuchElementException();

} else if (loc == 0) {

removeFirst();

} else if (loc == size - 1) {

removeLast();

} else {

tmp = getElement(loc);

Node preEle = tmp.prev;

Node nextEle = tmp.next;

preEle.next = nextEle;

nextEle.prev = preEle;

size--;

System.out.println("deleted: " + tmp.element);

return tmp.element;

}

return null;

}

public static void main(String a[]) {

DoublyLinkedListImpl dll = new DoublyLinkedListImpl();

dll.addFirst(10);

dll.addFirst(34);

dll.addLast(56);

dll.addLast(364);

dll.insertAfterElement(88, 2);

dll.insertAfterElement(99, 3);

dll.iterateForward();

dll.removeByIndex(4);

dll.iterateBackward();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表是一种常见的数据结构,与单向链表相比,它可以在节点之间进行双向遍历。在Java,我们可以使用类来实现双向链表。 下面是一个简单的Java程序,演示如何创建和输出双向链表: ```java // 双向链表节点类 class Node { public int data; public Node prev; public Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } // 双向链表类 class DoubleLinkedList { public Node head; public Node tail; public DoubleLinkedList() { this.head = null; this.tail = null; } // 在链表头部插入节点 public void insertAtHead(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; return; } newNode.next = head; head.prev = newNode; head = newNode; } // 在链表尾部插入节点 public void insertAtTail(int data) { Node newNode = new Node(data); if (tail == null) { head = newNode; tail = newNode; return; } newNode.prev = tail; tail.next = newNode; tail = newNode; } // 输出链表 public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } // 测试类 public class Main { public static void main(String[] args) { DoubleLinkedList list = new DoubleLinkedList(); list.insertAtHead(1); list.insertAtHead(2); list.insertAtTail(3); list.insertAtTail(4); list.printList(); // 输出:2 1 3 4 } } ``` 在上面的程序,我们首先定义了一个`Node`类来表示双向链表的每个节点,其包含了当前节点的值、前一个节点和后一个节点。接着,我们定义了`DoubleLinkedList`类来表示整个双向链表,其包含了头节点和尾节点。 在`DoubleLinkedList`类,我们定义了`insertAtHead`和`insertAtTail`方法来在链表头部和尾部插入节点,分别需要创建一个新节点,并将它与当前链表的头节点或尾节点进行连接。 最后,我们定义了`printList`方法来输出整个链表,只需要从链表的头节点开始遍历,依次输出每个节点的值即可。 在`Main`类,我们通过`DoubleLinkedList`类创建了一个双向链表,并向其插入了四个节点。最后,我们调用`printList`方法输出整个链表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值