【資料結構】單向迴圈連結串列
阿新 • • 發佈:2019-01-03
寫的中間發現一個問題,我試了一下,final修飾的引用是指該引用只能指向一個物件不能更改指向其他物件,但是該引用調該物件的方法進行內部資訊修改是OK的
package 連結串列.迴圈連結串列;
import java.util.NoSuchElementException;
/**
* @author 蕪情
* 帶頭結點的單向迴圈連結串列
*/
public class CLinkedList{
//定義連結串列節點
private static class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
Node(){
}
Node(int e, Node next){
data = e;
this.next = next;
}
}
//頭結點
private Node head;
//連結串列長度
private int size;
//初始化雙向連結串列
public CLinkedList() {
head = new Node();
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{
final Node positionNode = node(position - 1);
final Node node = new Node(e, positionNode.getNext());
positionNode.setNext(node);
++size;
}
}
//插入首節點
public void addFirst(int e){
final Node last = node(size - 1);
final Node node = new Node(e, head.getNext());
head.setNext(node);
last.setNext(node);
++size;
}
//插入尾節點
public void addLast(int e){
if(isEmpty()){
addFirst(e);
}else{
final Node first = head.getNext();
final Node last = node(size - 1);
final Node node = new Node(e, first);
last.setNext(node);
++size;
}
}
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);
final Node beforeNode = node(index - 1);
beforeNode.setNext(node.getNext());
--size;
return node.getData();
}
}
//刪除首節點,返回被刪除的元素
public int removeFirst(){
final Node first = head.getNext();
if(first == null){
throw new NoSuchElementException();
}
final Node last = node(size - 1);
head.setNext(first.getNext());
last.setNext(first.getNext());
--size;
return first.getData();
}
public int remove(){
return removeFirst();
}
//刪除尾節點
public int removeLast(){
if(isEmpty()){
throw new NoSuchElementException();
}
return remove(size - 1);
}
/*連結串列主要以增刪節點的操作比較重要,改和查就不寫了*/
}