亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)單鏈表、雙向鏈表

 更新時(shí)間:2021年08月01日 11:01:54   作者:New_Null  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)單鏈表、雙向鏈表的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)單鏈表、雙向鏈表的相關(guān)代碼,供大家參考,具體內(nèi)容如下

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

package code;

class Node
{
 Node next;
 int data;
 public Node(int data)
 {
 this.data=data;
 }
 
}
class LinkList
{
 Node first;
 //頭部
 public LinkList()
 {
 this.first=null;
 }
 public void addNode(Node no)
 {
 no.next=first;
 first=no;//在頭部添加
 }
 public void delectNode()
 {
 Node n=first.next;
 first=null;
 first=n;//在頭部刪除
 }
 //刪除指定位置
 public int Number()
 {
 int count=1;
 //查看有多少元素
 Node nd=first;
 while(nd.next!=null)
 {
  nd=nd.next;
  count++;
 }
 return count;
 }
 public void delectExact(int n)
 {
 //刪除指定位置
 if(n>1)
 {
  int count=1;
  Node de=first;
  while(count<n-1)
  {
  de=de.next;
  count++;
  
  }
  de.next=de.next.next;
 }
 else
  first=first.next;
 
 }
 public void addExact(int n,Node nd)
 {
 if(n>1)//添加指定位置
 {
  int count=1;
  Node de=first;
  while(count<n-1)
  {
  de=de.next;
  count++;
  
  }
  nd.next=de.next;
  de.next=nd;

 }
 else
  first=first.next;
 }
 public int findNode(int n)
 {
 int count=1;//查找一個(gè)數(shù)對應(yīng)的位置
 Node de=first;
 while(de.data!=n)
 {
  de=de.next;
  count++;
  if(de==null)
  {
  return -1;
  }
 }
 return count;
 }
 public void print()
 {
 Node no=first;//打印所有
 while(no!=null)
 {
  System.out.println(no.data);
  no=no.next;
 }
 }
}
public class TextNode
{
 public static void main(String[] args)
 {
 LinkList ll=new LinkList();
 ll.addNode(new Node(12));
 ll.addNode(new Node(15));
 ll.addNode(new Node(18));
 ll.addNode(new Node(19));
 ll.addNode(new Node(20));
 /*System.out.println(ll.first.data);

 ll.delectNode();
 System.out.println(ll.first.data);*/
 System.out.println(ll.Number());
 ll.delectExact(3);
 ll.addExact(3, new Node(100));
 System.out.println(ll.Number());
// ll.print();
 System.out.println(ll.findNode(112));
 
 }
}

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

public class DoubleLink
{
 public static void main(String[]args)
 {
 Node2 no=new Node2(5);
 no.addLeft(new Node2(6));
 no.addRight(new Node2(7));
 /*no.print();
 no.print2();*/
 no.addExact2(1, new Node2(8));
 no.print();
 System.out.println("--------------");
 no.print2();
 }
}
class Node2
{
 public Node2 first;
 public Node2 end;
 public Node2 left;
 public Node2 right;
 int data=0;
 public Node2(int n)
 {
 
 first=this;
 end=this;
 
 first.data=n;
 }
 //從頭部添加
 public void addLeft(Node2 before)
 {
 first.left=before;
 before.right=first;
 first=before;
 }
 //從尾部添加
 public void addRight(Node2 after)
 {
 end.right=after;
 after.left=end;
 end=after;
 }
 //插入正數(shù)(第三聲)的第幾個(gè)
 public void addExact(int n,Node2 no)
 {
 int count=0;
 if(n==0)
 {
  addLeft(no);
 }
 else
 { 
  Node2 f=first;
  while(true)
  {
  f=f.right;
  count++;
  if(count==n)
  {
   //此處為四個(gè)指針的指向的變化
   no.left=f.left;
   f.left.right=no;
 //  first.left=no;
   no.right=f;
   f.left=no;
   break;
  }
 
  }
 }
 }
 //插入倒數(shù)的第幾個(gè)
 public void addExact2(int n,Node2 no)
 {
 int count=0;
 if(n==0)
 {
  addRight(no);
 }
 else
 {
  Node2 f=end;
  while(true)
  {
  f=f.left;
  count++;
  if(count==n)
  {
   
   no.left=f;
   no.right=f.right;
   f.right.left=no;
   f.right=no;
   break;
   
  }
  }
 }
 }
 //正序遍歷
 public void print()
 {
 System.out.println(first.data);
 while(first.right!=null)
 {
  System.out.println(first.right.data);
  first=first.right;
 }
// System.out.println(end.data);
 }
 //倒序遍歷
 public void print2()
 {
 System.out.println(end.data);
 while(end.left!=null)
 {
  System.out.println(end.left.data);
  end=end.left;
 }
 }
 

}
/*值得注意的是,每一次插入一個(gè)新的對象的時(shí)候,需要注意指針指向的改變。
首先是這個(gè)新的對象兩邊的指向(左和右),其次是時(shí)左邊的對象向右的指向
和右邊對象向左的指向。
這四個(gè)指針的指向必須正確,否則可能導(dǎo)致正序或者倒序遍歷無法實(shí)現(xiàn)。
*/
/*對比單鏈表,單鏈表只能從一個(gè)方向遍歷,因?yàn)橹挥幸粋€(gè)頭,而雙向鏈表,有頭和尾,可以從
 * 頭遍歷,也可以從尾遍歷,而且其中一個(gè)對象因?yàn)橛袃蓚€(gè)方向的指針,所以他可以獲得左邊的
 * 對象也可以獲得右邊的對象。
 * 但是單鏈表的話,因?yàn)橹挥幸粋€(gè)方向,所以只能向左或右。添加對象的時(shí)候,雙向也可以從頭添加,也可以從尾添加。
 * 如果單鏈表要實(shí)現(xiàn)兩個(gè)方向添加比較難得,或者說不行,因?yàn)樗挥邢蜃蠡蛳蛴业囊粋€(gè)方向的指針
 * 而雙向鏈表每個(gè)對象都有兩個(gè)方向的指針沒這樣更靈活,但是這同樣有缺點(diǎn),因?yàn)檫@樣的話每個(gè)對象
 * 都會(huì)包含兩個(gè)指針,這同樣內(nèi)存會(huì)消耗更多。
 * 
 * */

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Spring超詳細(xì)講解IOC與解耦合

    Spring超詳細(xì)講解IOC與解耦合

    IoC就是比方說有一個(gè)類,我們想要調(diào)用類里面的方法(不是靜態(tài)方法),就要?jiǎng)?chuàng)建該類的對象,使用對象調(diào)用方法來實(shí)現(xiàn)。但對于Spring來說,Spring創(chuàng)建對象的過程,不是在代碼里面實(shí)現(xiàn)的,而是交給Spring來進(jìn)行配置實(shí)現(xiàn)的
    2022-08-08
  • Java Semaphore實(shí)現(xiàn)高并發(fā)場景下的流量控制

    Java Semaphore實(shí)現(xiàn)高并發(fā)場景下的流量控制

    在java開發(fā)的工作中是否會(huì)出現(xiàn)這樣的場景,你需要實(shí)現(xiàn)一些異步運(yùn)行的任務(wù),該任務(wù)可能存在消耗大量內(nèi)存的情況,所以需要對任務(wù)進(jìn)行并發(fā)控制。本文將介紹通過Semaphore類優(yōu)雅的實(shí)現(xiàn)并發(fā)控制,感興趣的可以了解一下
    2021-12-12
  • Java實(shí)現(xiàn)Map遍歷key-value的四種方法

    Java實(shí)現(xiàn)Map遍歷key-value的四種方法

    本文主要介紹了Java實(shí)現(xiàn)Map遍歷key-value的四種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java使用JDBC驅(qū)動(dòng)連接MySQL數(shù)據(jù)庫

    Java使用JDBC驅(qū)動(dòng)連接MySQL數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了Java使用JDBC驅(qū)動(dòng)連接MySQL數(shù)據(jù)庫的具體步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳

    關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳

    這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實(shí)是很相似的,都是將文件從一個(gè)地方轉(zhuǎn)移到另一個(gè)地方,這也是流的特點(diǎn)之一,需要的朋友可以參考下
    2023-04-04
  • Java算法真題詳解運(yùn)用單調(diào)棧

    Java算法真題詳解運(yùn)用單調(diào)棧

    一般使用單調(diào)棧無非兩個(gè)方向,單調(diào)遞減,單調(diào)遞增。單調(diào)遞增棧:存進(jìn)去的數(shù)據(jù)都是增加的,碰到減少的時(shí)候,這時(shí)就要進(jìn)行操作了。單調(diào)遞減棧:存進(jìn)去的數(shù)據(jù)都是減少的,碰到增加的時(shí)候,這時(shí)就要進(jìn)行操作了,下面我們在真題中運(yùn)用它
    2022-07-07
  • 使用Jmeter進(jìn)行http接口測試的實(shí)踐

    使用Jmeter進(jìn)行http接口測試的實(shí)踐

    本文主要針對http接口進(jìn)行測試,使用Jmeter工具實(shí)現(xiàn)。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 詳解java裝飾模式(Decorator Pattern)

    詳解java裝飾模式(Decorator Pattern)

    這篇文章主要為大家詳細(xì)介紹了java裝飾模式Decorator Pattern,這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有的類的一個(gè)包裝,對裝飾器模式感興趣的小伙伴們可以參考一下
    2016-04-04
  • Java TimedCache 帶時(shí)間緩存工具類詳解使用

    Java TimedCache 帶時(shí)間緩存工具類詳解使用

    工具類是包含集合框架、遺留的 collection 類、事件模型、日期和時(shí)間設(shè)施、國際化和各種實(shí)用工具類(字符串標(biāo)記生成器、隨機(jī)數(shù)生成器和位數(shù)組、日期Date類、堆棧Stack類、向量Vector類等)。集合類、時(shí)間處理模式、日期工具等各類常用工具包,本文將介紹帶時(shí)間緩存工具類
    2021-10-10
  • Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例)

    Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例)

    這篇文章主要介紹了Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論