中文字幕av高清_国产视频一二区_男女羞羞羞视频午夜视频_成人精品一区_欧美色视_在线视频这里只有精品

千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

手機(jī)站
千鋒教育

千鋒學(xué)習(xí)站 | 隨時隨地免費(fèi)學(xué)

千鋒教育

掃一掃進(jìn)入千鋒手機(jī)站

領(lǐng)取全套視頻
千鋒教育

關(guān)注千鋒學(xué)習(xí)站小程序
隨時隨地免費(fèi)學(xué)習(xí)課程

當(dāng)前位置:首頁  >  千鋒問問  > java單鏈表的實(shí)現(xiàn)方法

java單鏈表的實(shí)現(xiàn)方法

java單鏈表 匿名提問者 2023-07-28 18:46:42

java單鏈表的實(shí)現(xiàn)方法

我要提問

推薦答案

  在Java中,單鏈表是一種常見的數(shù)據(jù)結(jié)構(gòu),用于存儲一系列具有相同類型的元素。單鏈表由一系列節(jié)點(diǎn)組成,每個節(jié)點(diǎn)包含一個數(shù)據(jù)元素和一個指向下一個節(jié)點(diǎn)的引用。以下是Java中單鏈表的實(shí)現(xiàn)及其基本操作:

千鋒教育

  節(jié)點(diǎn)類的定義:

  javaCopy codeclass Node {

  int data;

  Node next;

  public Node(int data) {

  this.data = data;

  this.next = null;

  }

  }

  單鏈表類的定義:

  javaCopy codeclass LinkedList {

  private Node head;

  public LinkedList() {

  this.head = null;

  }

  // 在鏈表尾部添加節(jié)點(diǎn)

  public void append(int data) {

  Node newNode = new Node(data);

  if (head == null) {

  head = newNode;

  } else {

  Node current = head;

  while (current.next != null) {

  current = current.next;

  }

  current.next = newNode;

  }

  }

  // 在鏈表頭部插入節(jié)點(diǎn)

  public void prepend(int data) {

  Node newNode = new Node(data);

  newNode.next = head;

  head = newNode;

  }

  // 刪除指定值的節(jié)點(diǎn)

  public void delete(int data) {

  if (head == null) {

  return;

  }

  if (head.data == data) {

  head = head.next;

  return;

  }

  Node current = head;

  while (current.next != null) {

  if (current.next.data == data) {

  current.next = current.next.next;

  return;

  }

  current = current.next;

  }

  }

  // 遍歷并打印鏈表元素

  public void print() {

  Node current = head;

  while (current != null) {

  System.out.print(current.data + " ");

  current = current.next;

  }

  }

  }

其他答案

  •   除了基本的添加、插入和刪除操作,單鏈表還支持其他常用的操作,如搜索和反轉(zhuǎn)。以下是Java中單鏈表的搜索和反轉(zhuǎn)操作的實(shí)現(xiàn):

      搜索指定值的節(jié)點(diǎn):

      javaCopy code// 在鏈表中搜索指定值的節(jié)點(diǎn),返回節(jié)點(diǎn)的引用,如果找不到返回null

      public Node search(int data) {

      Node current = head;

      while (current != null) {

      if (current.data == data) {

      return current;

      }

      current = current.next;

      }

      return null;

      }

      反轉(zhuǎn)鏈表:

      javaCopy code// 反轉(zhuǎn)鏈表

      public void reverse() {

      Node prev = null;

      Node current = head;

      Node next = null;

      while (current != null) {

      next = current.next;

      current.next = prev;

      prev = current;

      current = next;

      }

      head = prev;

      }

  •   在實(shí)際應(yīng)用中,我們可能需要獲取鏈表的長度以及在指定位置插入節(jié)點(diǎn)。以下是Java中單鏈表的長度和插入操作的實(shí)現(xiàn):

      獲取鏈表的長度:

      javaCopy code// 獲取鏈表的長度

      public int length() {

      int count = 0;

      Node current = head;

      while (current != null) {

      count++;

      current = current.next;

      }

      return count;

      }

      在指定位置插入節(jié)點(diǎn):

      javaCopy code// 在指定位置插入節(jié)點(diǎn)

      public void insertAt(int data, int position) {

      if (position < 0 || position > length()) {

      throw new IllegalArgumentException("Invalid position");

      }

      if (position == 0) {

      prepend(data);

      return;

      }

      Node newNode = new Node(data);

      Node current = head;

      for (int i = 0; i < position - 1; i++) {

      current = current.next;

      }

      newNode.next = current.next;

      current.next = newNode;

      }

      通過以上的三篇文章,讀者可以了解到Java中單鏈表的基本操作、搜索、反轉(zhuǎn)、獲取長度以及在指定位置插入節(jié)點(diǎn)等常用操作。單鏈表作為一種重要的數(shù)據(jù)結(jié)構(gòu),在編程中經(jīng)常被用到,掌握它的實(shí)現(xiàn)和操作將有助于更好地處理數(shù)據(jù)和問題。

主站蜘蛛池模板: 日韩欧美二区 | 99热首页 | 欧美成人在线免费视频 | 北条麻妃国产九九九精品小说 | 免费特黄视频 | 日韩成人免费在线 | 福利视频网站 | 欧美久久精品一级c片 | 久久久久国产精品免费免费搜索 | 2019亚洲日韩新视频 | 黄色影视片 | 国产精品国产a级 | 欧美成人激情视频 | 欧美人成在线观看 | 国产欧美一区在线 | 4hu在线| 国产乱码精品一区二区三区忘忧草 | 日日艹 | 日本免费精品 | 欧美精品一区二区三区在线播放 | 蜜桃官网 | 国产精品久久久久毛片软件 | 在线久草 | 欧美午夜精品久久久久免费视 | 亚洲高清一区二区三区 | 欧美精品欧美精品系列 | 香蕉久久网 | 99国产精品99久久久久久 | 四虎影院观看 | 91电影在线 | 黄色天堂网 | 成人av在线网 | 亚洲一区二区三区免费在线观看 | 久久国产精品视频 | 久久综合九色综合欧美狠狠 | 高清一区二区三区 | 天天爽夜夜爽 | 91视频播放 | av一级久久 | 久久久欧美 | 国产超碰人人爽人人做人人爱 |