package 链表.双向链表;
import java.util.NoSuchElementException;
/**
* @author 芜情
* 带头结点的双向链表
*/
public class DLinkedList{
//定义链表节点
private static class Node{
private int data;
private Node prev;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
Node(){
}
Node(Node prev, int e, Node next){
this.prev = prev;
data = e;
this.next = next;
}
}
//头结点
private Node head;
//链表长度
private int size;
//初始化双向链表
public DLinkedList() {
head = new Node();
head.setPrev(null);
head.setNext(null);
size = 0;
}
//判断链表是否为空
public boolean isEmpty(){
return head.getNext() == null;
}
//清空链表
public void clear(){
head.setNext(null);
}
//链表长度
public int size(){
return size;
}
/*============================================*/
/*内部方法,为了好理解,从名字能看出来代码要干什么*/
//判断下标是不是有效
private boolean isValidIndex(int index) {
return index >= 0 && index < size;
}
//判断某个操作的位置是否有效,如执行插入操作时
private boolean isValidPosition(int position) {
return position >= 0 && position <= size;
}
//定义报错信息
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkIndex(int index) {
if (!isValidIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPosition(int position) {
if (!isValidPosition(position))
throw new IndexOutOfBoundsException(outOfBoundsMsg(position));
}
//返回指定下标节点
private Node node(int index){
Node currentNode = head;
int i = 0;
while(i <= index){
currentNode = currentNode.getNext();
++i;
}
return currentNode;
}
/*============================================*/
/**
* 增加节点
*/
//在中间插入节点
public void add(int position, int e){
checkPosition(position);
if(position == 0){
addFirst(e);
}else{
Node positionNode = node(position - 1);
final Node node = new Node(positionNode, e, positionNode.getNext());
positionNode.setNext(node);
positionNode.getNext().setPrev(node);
++size;
}
}
//插入首节点
public void addFirst(int e){
final Node node = new Node(head, e, head.getNext());
head.setPrev(node);
head.setNext(node);
++size;
}
//插入尾节点
public void addLast(int e){
if(!isEmpty()){
Node last = node(size - 1);
final Node node = new Node(last, e, null);
last.setNext(node);
++size;
}else{
addFirst(e);
}
}
public void add(int e){
addLast(e);
}
/**
* 删除节点
*/
//删除中间的一个节点
public int remove(int index){
checkIndex(index);
if(index == 0){
return remove();
}else{
final Node node = node(index);
node.getNext().setPrev(node.getPrev());
node.getPrev().setNext(node.getNext());
--size;
return node.getData();
}
}
//删除首节点,返回被删除的元素
public int removeFirst(){
final Node f = head.getNext();
if(f == null){
throw new NoSuchElementException();
}
f.getNext().setPrev(head);
head.setNext(f.getNext());
--size;
return f.getData();
}
public int remove(){
return removeFirst();
}
//删除尾节点
public int removeLast(){
if(isEmpty()){
throw new NoSuchElementException();
}
return remove(size - 1);
}
/*链表主要以增删节点的操作比较重要,改和查就不写了*/
}
【数据结构】双向链表
最新推荐文章于 2024-09-11 02:01:15 发布