日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA复习5(集合——拓展——单向链表)

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(集合——拓展——单向链表)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。