數據結構 鏈表-->bug

以下是大學中《數據結構-Java版》- 朱戰立(編)書籍中的一個小bug,不太算bug,只算一個小錯誤吧,貼出來記下它。

分析在程序之後補充:錯誤之處已經用特殊字符標出來了。

package test.inter;

public interface List {
	public void inset(int i, Object obj) throws Exception;
	public Object delete(int i) throws Exception;
	public Object getData(int i) throws Exception;
	public int size();
	public boolean isEmpty();
}

package test3;

public class Node {
	
	public Object element;
	public Node next;
	
	public Node(){
		
	}
	public Node(Node next) {
		this.next = next;
	}
	public Node(Object element, Node next) {
		this.element = element;
		this.next = next;
	}
	
	public Object getElement() {
		return element;
	}
	public void setElement(Object element) {
		this.element = element;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	
	public String toString() {
		return element.toString();
	}
}

package test3;

import test.inter.List;

public class LinList implements List {
	
	Node head;
	Node current;
	int size;
	
	public LinList() {
		head = current = new Node();
		size = 0;
	}
	
	public void index(int i) throws Exception {
		if(i<-1 || i>size-1) {
			
		}
		/**********關鍵的bug點**********/
		if(i == -1) {
			current = head;
			return;
		}
		/******************************/
		current = head.next;
		
		int j = 0;
		
		while(j<i && (current!=null)) {
			current = current.next;
			j++;
		}
	}
	
	@Override
	public Object delete(int i) throws Exception {
		if(size == 0) {
			throw new Exception("鏈表已經空了,無法再刪除元素。");
		}
		if(i<0 || i>size-1) {
			throw new Exception("index參數的值不合法。");
		}
		
		index(i-1);
		Object obj = current.next.getElement();
		current.setNext(current.next.next);
		size--;
		return obj;
	}

	@Override
	public Object getData(int i) throws Exception {
		if(i<-1 || i>size-1) {
			throw new Exception("參數不合法。");
		}
		index(i);
		return current.getElement();
	}

	@Override
	public void inset(int i, Object obj) throws Exception {
		if(i<0 || i>size) {
			throw new Exception("參數不合法。");
		}
		index(i-1);
		
		current.setNext(new Node(obj, current.next));
		size++;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public int size() {
		return size;
	}
}

package test3;

public class LinListTest {

	public static void main(String[] args) throws Exception {
		LinList linList = new LinList();
		
		for(int i=0; i<10; i++) {
			linList.inset(i, new Integer(i+1));//定位情況:index(i-1);
		}
		
		System.out.println("size_before: " + linList.size());
		System.out.println("getData: " + linList.getData(0));//定位情況:index(i);
		System.out.println("delete: " + linList.delete(0));//定位情況:index(i-1);
		System.out.println("size_last: " + linList.size());
	}
}

總結:

以上錯誤之處就出在標識的:current = head; 這一句是否需要。

如果不寫,測試類運行的結果是:

size_before: 10
getData: 1
delete: 2
size_last: 9

很顯然,認真可以發現刪除(delete)的元素的值是錯的。

正確的應該是下面寫上了 current = head; 的結果:

size_before: 10
getData: 1
delete: 1
size_last: 9

這下刪除(delete)的結果就是正確的。


結合圖形:

 

word畫的,醜了點尷尬

      1>無current = head;的情況:

在構造方法裏面有:head = current = new Node(null); 說明在初始化的時候,current指向的是如圖所示的“頭結點”的地方,所以在我執行:delete(0); 的時候,在定位的時候由於index(i-1); --> i=-1; 直接return; 結束了定位,current指向的還是“頭結點”,所以在刪除的時候:current.setNext(current.next.next); 自然就跳躍了兩個結點,指向瞭如圖中A1的位置,此時A1.getElement()=2

      2>有current = head;的情況:

在此時,定位到i==-1的時候,這時候的current = head,那麼在current.setNext(current.next.next);自然就指向了A0,這纔是我們想要的結果,也就是保證了程序的"正確性"(因爲這是'程序是否優秀的先決條件',所以才慢慢去分析了它),此時A0.getElement()=1;




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章