JAVA复习5(集合——拓展——单向链表)
擴展: 實現單向鏈表
?
鏈表其實就是一種順序存儲的數據結構,一個節點上存在兩個屬性 數據 指向下一個節點的指針
?
對于鏈表的操作,其實就是一組操作標準:
1 增加元素
?
2 刪除元素
?
3 判斷鏈表是否為空
?
3 返回鏈表中的長度
?
既然以上的操作定義為標準,則可以抽象為接口 鏈表類直接實現該接口中的標準
?
實現鏈表:
?
1 定義鏈表的操作標準
| package org.node; ? public interface List { ? ???? /** ???? ?* 獲得鏈表中的長度 ???? ?* @return ???? ?*/ ???? public int size(); ???? ???? /** ???? ?* 判斷鏈表是否為空 ???? ?* @return ???? ?*/ ???? public boolean isEmpty(); ???? ???? /** ???? ?* 插入元素 ???? ?* @param index ???? ?* @param obj ???? ?* @throws Exception ???? ?*/ ???? public void add(int index,Object obj)throws Exception; ???? ???? ???? /** ???? ?* 刪除元素 ???? ?* @param index ???? ?* @throws Exception ???? ?*/ ???? public void remove(int index)throws Exception; ???? ???? ???? /** ???? ?* 取得鏈表中的指定元素 ???? ?* @param index ???? ?* @return ???? ?* @throws Exception ???? ?*/ ???? public Object get(int index)throws Exception; } ? |
?
2 定義節點類
| package org.node; /** ?* 定義節點類?? 兩個屬性? 存儲的數據? 指向下一個節點的指針 ?* @author wubo ?* ?*/ public class Node { ? ???? ???? ? Object element; //保存的數據 ???? ? ???? ? Node next;?? //指針 ???? ? ???? ? ???? ? //構造方法 ???? ? //頭節點 ???? ? public Node(Node nextval) { ????????? ? ????????? ? this.next=nextval; ???? ? } ???? ? ???? ? //不是頭節點 ???? ? public Node(Object obj, Node nextval) { ????????? ? ????????? ? this.element=obj; ????????? ? ????????? ? this.next=nextval; ???? ? } ? ???? 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; ???? } } ? |
?
3 定義鏈表類
| package org.node; public class LinkList implements List{ ? ???? Node head; //頭指針 ???? ???? Node current; //當前節點 ???? ???? int size ; //記錄節點元素的個數 ???? ???? //初始化一個空鏈表 ???? public LinkList() { ????????? // TODO Auto-generated constructor stub ????????? //初始化空的頭節點 ????????? this.head=current=new Node(null); ????????? this.size=0; ????????? ???? } ???? ???? //定位方法 找到當前對象的前一個節點 ???? public void index(int index)throws Exception{ ????????? ????????? //要對輸入的index進行判斷 ????????? if(index<-1||index>size-1) { ?????????????? ?????????????? throw new Exception("參數錯誤"); ?????????????? ????????? } ????????? if(index==-1) {? //如果傳進來的是頭節點直接return ?????????????? ?????????????? return ; ????????? } ????????? ????????? current=head.next; ????????? ????????? int j=0; //循環變量 ????????? ????????? while(current!=null&&j<index) { ?????????????? ?????????????? current=current.next; ?????????????? j++; ????????? } ???? } ???? ???? ???? @Override ???? public int size() { ????????? // TODO Auto-generated method stub ????????? return this.size; ???? } ? ???? @Override ???? public boolean isEmpty() { ????????? // TODO Auto-generated method stub ????????? return this.size==0; ???? } ? ???? @Override ???? public void add(int index, Object obj) throws Exception { ????????? // TODO Auto-generated method stub ????????? ????????? //增加之前判斷參數 ????????? ????????? if(index<0||index>size) { ?????????????? ?????????????? throw new Exception("參數錯誤"); ????????? } ????????? ????????? // 定位節點 ????????? index(index-1); ????????? ????????? current.setNext(new Node(obj,current.next)); ????????? size++; ???? } ? ???? @Override ???? public void remove(int index) throws Exception { ????????? // TODO Auto-generated method stub ????????? ????????? if(isEmpty()) { ?????????????? ?????????????? throw new Exception("空鏈表"); ????????? } ????????? ????????? if(index<0||index>size) { ?????????????? ?????????????? throw new Exception("參數錯誤"); ????????? } ????????? ????????? index(index-1); //定位操作 ????????? ????????? current.setNext(current.next.next); ????????? ????????? size--; ???? } ? ???? @Override ???? public Object get(int index) throws Exception { ????????? // TODO Auto-generated method stub ????????? if(index<-1||index>size-1) { ?????????????? ?????????????? throw new Exception("參數錯誤"); ????????? } ????????? ????????? index(index); ????????? ????????? ????????? return current.getElement(); ???? } ? } ? |
?
測試類:
| package org.node; ? public class NodeTest { ? ???? ???? public static void main(String[] args) throws Exception { ????????? ????????? ????????? LinkList? list=new LinkList(); ????????? ????????? for(int i=0;i<10;i++) { ?????????????? ?????????????? ?????????????? list.add(i, "i"+i); ????????? } ????????? ????????? ????????? for(int i=0;i<list.size;i++) { ?????????????? ?????????????? System.out.println(list.get(i)); ????????? } ???? } } ? |
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的JAVA复习5(集合——拓展——单向链表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公司回应招聘前台要求身材 硬性要求臀围8
- 下一篇: LG 新能源有望上半年决定在亚利桑那州建