この記事では、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 サイトの他の関連記事を参照してください。

この記事では、Javaプロジェクト管理、自動化の構築、依存関係の解像度にMavenとGradleを使用して、アプローチと最適化戦略を比較して説明します。

この記事では、MavenやGradleなどのツールを使用して、適切なバージョン化と依存関係管理を使用して、カスタムJavaライブラリ(JARファイル)の作成と使用について説明します。

この記事では、カフェインとグアバキャッシュを使用してJavaでマルチレベルキャッシュを実装してアプリケーションのパフォーマンスを向上させています。セットアップ、統合、パフォーマンスの利点をカバーし、構成と立ち退きポリシー管理Best Pra

この記事では、キャッシュや怠zyなロードなどの高度な機能を備えたオブジェクトリレーショナルマッピングにJPAを使用することについて説明します。潜在的な落とし穴を強調しながら、パフォーマンスを最適化するためのセットアップ、エンティティマッピング、およびベストプラクティスをカバーしています。[159文字]

Javaのクラスロードには、ブートストラップ、拡張機能、およびアプリケーションクラスローダーを備えた階層システムを使用して、クラスの読み込み、リンク、および初期化が含まれます。親の委任モデルは、コアクラスが最初にロードされ、カスタムクラスのLOAに影響を与えることを保証します


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

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

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

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