posts - 495,  comments - 11,  trackbacks - 0

          interface List
          {
          public void insert(int i,Object obj) throws Exception;?? //插入
          public Object delete(int i) throws Exception;??????????? //刪除
          public Object getData(int i) throws Exception;?????????? //取數(shù)據(jù)元素
          public int size();?????????????????????????????????????? //求元素個數(shù)
          public boolean isEmpty();??????????????????????????????? //是否空
          }

          //單鏈表結(jié)點類

          class Node
          {
          Object element;?? //數(shù)據(jù)元素
          Node next;?????? //表示下一個結(jié)點的對象引用

          Node(Node nextval)?? //用于頭結(jié)點的構(gòu)造函數(shù)1
          {
          ?? next = nextval;
          }

          Node(Object obj,Node nextval)?? //用于其他結(jié)點的構(gòu)造函數(shù)2
          {
          ?? element = obj;
          ?? next = nextval;
          }

          public Node getNext()?? //取next
          {
          ?? return next;
          }

          public void setNext(Node nextval)?? //置next
          {
          ?? next = nextval;
          }

          public Object getElement()??? //取element
          {
          ?? return element;
          }

          public void setElement(Object obj)?? //置element
          {
          ?? element = obj;
          }

          public String toString()?? //轉(zhuǎn)換element為String類型
          {
          ?? return element.toString();
          }
          }

          //單鏈表類

          class LinList implements List
          {
          Node head;????? //頭指針
          Node current;?? //當前結(jié)點位置
          int size;?????? //數(shù)據(jù)元素個數(shù)

          LinList()?????? //構(gòu)造函數(shù)
          {
          ?? head = current = new Node(null);
          ?? size =0;
          }

          public void index(int i) throws Exception
          { //定位到第i個結(jié)點(以0開始記起)
          ????? if(i < -1 || i > size-1)
          ????? {
          ??? throw new Exception("參數(shù)錯誤!");
          ?? }
          ?? if(i == -1) return;
          ?? current = head.next;
          ?? int j = 0;
          ?? while((current != null) && j < i)
          ?? {
          ??? current = current.next;
          ??? j++;
          ?? }
          ???? }

          ???? public void insert(int i,Object obj) throws Exception
          ???? {
          ?? if(i < 0 || i > size)
          ?? {
          ??? throw new Exception("參數(shù)錯誤!");
          ?? }
          ?? index(i-1);
          ?? current.setNext(new Node(obj,current.next));
          ?? size++;
          }

          public Object delete(int i) throws Exception
          {
          ?? if(size == 0)
          ?? {
          ??? throw new Exception("鏈表已空無元素可刪除!");
          ?? }
          ?? if(i < 0 || i > size-1)
          ?? {
          ??? throw new Exception("參數(shù)錯誤!");
          ?? }
          ?? index(i-1);
          ?? Object obj = current.next.getElement();
          ?? current.setNext(current.next.next);
          ?? size--;
          ?? return obj;
          }

          public Object getData(int i) throws Exception
          {
          ????? if(i < -1 || i > size-1)
          ????? {
          ??? throw new Exception("參數(shù)錯誤!");
          ?? }
          ?? index(i);
          ?? return current.getElement();
          }

          public int size()
          {
          ?? return size;
          }

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

          /* 主函數(shù)
          * 刪除數(shù)列{1,2,3,4,5,6,7,8,9,10}里的元素5
          */

          public class LinListTest
          {
          public static void main(String args[])
          {
          ?? LinList linList = new LinList();
          ?? int n = 10;
          ?? try
          ?? {
          ??? for(int i = 0;i < n;i++)
          ??? {
          ???? linList.insert(i,new Integer(i+1));
          ??? }

          ??? linList.delete(4);

          ??? for(int i =0;i<linList.size;i++)
          ??? {
          ???? System.out.print(linList.getData(i)+"?? ");
          ??? }
          ?? }
          ?? catch(Exception e)
          ?? {
          ??? System.out.println(e.getMessage());
          ?? }
          }
          }

          posted on 2007-05-04 10:50 jadmin 閱讀(144) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 饶平县| 汨罗市| 亚东县| 台东县| 玉门市| 临桂县| 慈利县| 塘沽区| 洱源县| 泾源县| 柳河县| 沁阳市| 澜沧| 遂平县| 乌什县| 鄂伦春自治旗| 南宫市| 高平市| 尼勒克县| 阿拉善右旗| 浙江省| 慈溪市| 孟村| 三门峡市| 长岭县| 大姚县| 天台县| 和田县| 武陟县| 汪清县| 渝中区| 新昌县| 富源县| 尤溪县| 兴隆县| 瑞金市| 凤山县| 尼玛县| 舟曲县| 长宁区| 七台河市|