卜若的代碼筆記-數據結構系列-第七章:鏈表-循環鏈表的實現

1.繼承自雙向鏈表,然後把雙向鏈表的尾巴的end的next指向head。然後它的Insert,delete,get,函數不變。

 public static void main(String[] args) throws IOException {


        int[] data = new int[4];
        data[0] = 1;
        data[1] = 2;
        data[2] = 3;
        data[3] = 4;
        //定義一個統計表
        IntCircleTable table = new IntCircleTable(data[0]);
        //將數據加載到表中
        for (int i =1;i<data.length;i++){
            table.add(new IntDoubleDirElement(data[i]));
        }
        //遍歷統計
        String value = "";
        for (int i =0;i<30;i++){
            value+= table.get(i)+" ";

        }
        System.out.println(value);
        //插入測試
        table.insert(new IntDoubleDirElement(91),2);
        value = "";
        for (int i =0;i<30;i++){
            value+= table.get(i)+" ";

        }
        System.out.println(value);
        //刪除測試
        table.delete(1);
        value = "";
        for (int i =0;i<30;i++){
            value+= table.get(i)+" ";

        }
        System.out.println(value);

    }

2.實現:

package com.table.circle;

import DoubleDirTable.IntDoubleDirElement;
import DoubleDirTable.IntDoubleDirTable;

public class IntCircleTable extends IntDoubleDirTable {
    public IntCircleTable(int value) {
        super(value);
    }

    @Override
    public void add(IntDoubleDirElement intDoubleDirElement) {


        end().setNext(intDoubleDirElement);
        intDoubleDirElement.setLast(end());
        setEnd(intDoubleDirElement);
        end().setNext(head());

    }

}

 

 

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