Java二重リンクリスト実装のサンプルコード共有
この記事では、Java での二重リンク リストの実装の詳細な説明に関する関連情報を主に紹介します。必要な方は参照してください。この構造は確立されていますが、開発者としては、表示する能力も必要です。この構造を自分で。早速、コードを示します:
まず、リンクされたリストのノードクラス:
/** * 链表节点 * @author Administrator * * @param <T> */ public class ChainNode<T> { private T data; //对象编号 private int dataNo; public ChainNode<T> nextChainNode; public ChainNode<T> preChainNode; public ChainNode(T data, ChainNode<T> nextChainNode, ChainNode<T> preChainNode) { this.data = data; this.nextChainNode = nextChainNode; this.preChainNode = preChainNode; } public ChainNode(T data) { this.data = data; } public int getDataNo() { return dataNo; } public void setDataNo(int dataNo) { this.dataNo = dataNo; } public void setData(T data) { this.data = data; } public T getData() { return data; } }
次に、リンクされたリスト:
<pre name="code" class="java">/**
* 链表实现类
* @author Administrator
*
* @param <T>
*/
public class Chain<T> {
//头节点
private ChainNode<T> headNode;
//末尾节点指针
private ChainNode<T> lastNode;
private int size;
//创建链表就创建头节点
public Chain() {
this.headNode = new ChainNode<T>(null);
this.lastNode = headNode;
}
//增加一个节点
public void addNode(T data) {
ChainNode<T> node = new ChainNode<T>(data);
if(lastNode != null){
lastNode.nextChainNode = node;
node.preChainNode = node;
node.setDataNo(size);
lastNode = node;
size++;
}
}
//删除指定索引的节点
public void deleteNode(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.preChainNode.nextChainNode = node.nextChainNode;
if(node.nextChainNode != null){
node.nextChainNode.preChainNode = node.preChainNode;
}
node.nextChainNode = null;
node.preChainNode = null;
size--;
//重新设置节点的编号
for (ChainNode<T> chainNode = node.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
chainNode.setDataNo(chainNode.getDataNo()-1);
}
return;
}
}
throw new Exception("the corresponding data that can not be found");
}
//获取指定索引的节点
public T get(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
return node.getData();
}
}
throw new Exception("the corresponding data that can not be found");
}
//修改对应索引的节点
public void set(int dataNo,T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.setData(data);
return;
}
}
throw new Exception("the data that is to be modified can not be found");
}
//是否包含对应数据的节点
public boolean isContains(T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> chainNode = headNode.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
if(chainNode.getData() == data){
return true;
}
}
return false;
}
//获取节点数量(不含头节点)
public int getSize() {
return size;
}
}
public class ChainTest {
public static void main(String[] args) throws Exception{
String oneString = "one";
String twoString = "two";
String threeString = "three";
String fourString = "four";
Chain<String> chain = new Chain<String>();
chain.addNode(oneString);
chain.addNode(twoString);
chain.addNode(threeString);
chain.addNode(fourString);
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
try {
String string = chain.get(2);
System.out.println("the data of the second node is"+string);
System.out.println(chain.isContains(fourString));
chain.set(1, "haha");
System.out.println("modify chain");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
chain.deleteNode(3);
System.out.println("delete one node");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
リーリー
以上がJava二重リンクリスト実装のサンプルコード共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

Spring Bootは、Java開発に革命をもたらす堅牢でスケーラブルな、生産対応のJavaアプリケーションの作成を簡素化します。 スプリングエコシステムに固有の「構成に関する慣習」アプローチは、手動のセットアップを最小化します。
