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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/19 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA复习5(集合——拓展——单向链表) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

擴(kuò)展: 實(shí)現(xiàn)單向鏈表

?

鏈表其實(shí)就是一種順序存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),一個(gè)節(jié)點(diǎn)上存在兩個(gè)屬性 數(shù)據(jù) 指向下一個(gè)節(jié)點(diǎn)的指針

?

對(duì)于鏈表的操作,其實(shí)就是一組操作標(biāo)準(zhǔn):

1 增加元素

?

2 刪除元素

?

3 判斷鏈表是否為空

?

3 返回鏈表中的長度

?

既然以上的操作定義為標(biāo)準(zhǔn),則可以抽象為接口 鏈表類直接實(shí)現(xiàn)該接口中的標(biāo)準(zhǔn)

?

實(shí)現(xiàn)鏈表:

?

1 定義鏈表的操作標(biāo)準(zhǔn)

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 定義節(jié)點(diǎn)類

package org.node;

/**

?* 定義節(jié)點(diǎn)類?? 兩個(gè)屬性? 存儲(chǔ)的數(shù)據(jù)? 指向下一個(gè)節(jié)點(diǎn)的指針

?* @author wubo

?*

?*/

public class Node {

?

????

???? ? Object element; //保存的數(shù)據(jù)

???? ?

???? ? Node next;?? //指針

???? ?

???? ?

???? ? //構(gòu)造方法

???? ? //頭節(jié)點(diǎn)

???? ? public Node(Node nextval) {

????????? ?

????????? ? this.next=nextval;

???? ? }

???? ?

???? ? //不是頭節(jié)點(diǎn)

???? ? 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; //當(dāng)前節(jié)點(diǎn)

????

???? int size ; //記錄節(jié)點(diǎn)元素的個(gè)數(shù)

????

???? //初始化一個(gè)空鏈表

???? public LinkList() {

????????? // TODO Auto-generated constructor stub

????????? //初始化空的頭節(jié)點(diǎn)

????????? this.head=current=new Node(null);

????????? this.size=0;

?????????

???? }

????

???? //定位方法 找到當(dāng)前對(duì)象的前一個(gè)節(jié)點(diǎn)

???? public void index(int index)throws Exception{

?????????

????????? //要對(duì)輸入的index進(jìn)行判斷

????????? if(index<-1||index>size-1) {

??????????????

?????????????? throw new Exception("參數(shù)錯(cuò)誤");

??????????????

????????? }

????????? if(index==-1) {? //如果傳進(jìn)來的是頭節(jié)點(diǎn)直接return

??????????????

?????????????? return ;

????????? }

?????????

????????? current=head.next;

?????????

????????? int j=0; //循環(huán)變量

?????????

????????? 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

?????????

????????? //增加之前判斷參數(shù)

?????????

????????? if(index<0||index>size) {

??????????????

?????????????? throw new Exception("參數(shù)錯(cuò)誤");

????????? }

?????????

????????? // 定位節(jié)點(diǎn)

????????? 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("參數(shù)錯(cuò)誤");

????????? }

?????????

????????? 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("參數(shù)錯(cuò)誤");

????????? }

?????????

????????? 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));

????????? }

???? }

}

?

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的JAVA复习5(集合——拓展——单向链表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。