コードコピーは次のとおりです。
/**
*一元配置リンクリスト
*
*/
パブリッククラスのnodeList <e> {
プライベート静的クラスノード<e> {//ノードクラス
eデータ
ノード<e>次のノードを指します
ノード(e e){
this.data = e;
this.next = null;
}
}
プライベートノード<e>ヘッドリンクリストのヘッダーノード
プライベートノード<e> last; //リンクリストのテールノード
プライベートノード<e>その他= null;
プライベートInt length = 0; //数
/**
*ノンパラメーター建設方法
*/
public nodeList(){
//デフォルトのノードは空です
this.head = new node <e>(null);
}
/**
*初期化中にノードを作成します
*
* @paramデータ
* データ
*/
public nodelist(e data){
this.head = new node <e>(data);
this.last = head;
長さ++;
}
/**
*ノードを追加する(テール挿入方法)
*
* @paramデータ
* データ
*/
public void add(e data){
if(isempty()){
head = new node <e>(data);
last = head;
長さ++;
} それ以外 {
node <e> newNode = new Node <e>(data);
last.next = newNode;
last = newNode;
}
}
/**
*インデックスでデータを取得します(インデックス入力エラーはバウンドの例外をスローします)
* @paramインデックスインデックス
* @return indexのデータ
*/
public e get(int index){
if(index <0 || index> length){
新しいindexoutofboundsexception( "index:"+index);
}
その他= head;
for(int i = 0; i <index; i ++){
other = other.next;
}
return other.data;
}
/**
*新しい値は古い値を置き換えます
* @returnの成功は真実であり、偽として見つかりません
*/
public boolean set(e oldvalue、e newValue){
その他= head;
while(other!= null){
if(other.data.equals(oldvalue)){
other.data = newValue;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*要素を指定した後、要素を挿入します
*
* @paramデータ
*指定された要素
* @param InsertData
*挿入する必要がある要素
* @return falseは要素が見つかりません、trueは挿入が成功しました
*/
public boolean add(e data、e insertData){
その他= head;
while(other!= null){
if(other.data.equals(data)){
node <e> newNode = new node <e>(insertData);
node <e> temp = other.next;
newnode.next = temp;
other.next = newNode;
長さ++;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*この要素がリンクリストに含まれているかどうか
* @returnは、偽ではなく真であると含まれています
*/
public boolean contains(e data){
その他= head;
while(other!= null){
if(other.data.equals(data)){
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*指定された要素を削除します
*削除する必要がある@paramデータ要素
* @returnは存在せず、成功に真実です
*/
public boolean remove(e data){
その他= head;
ノード<e> temp = head; //前のノードを保存するために使用される一時変数
while(other!= null){
if(other.data.equals(data)){
temp.next = other.next;
長さ - ;
trueを返します。
}
temp = other;
other = other.next;
}
falseを返します。
}
/**
*リンクリストが空であるかどうかを判断します
*
* @return emptyは本当です、非空白は偽です
*/
public boolean isempty(){
return length == 0;
}
/**
*リンクリストをクリアします
*/
public void clear(){
this.head = null;
this.length = 0;
}
/**
*すべてのノードを出力します
*/
public void printlink(){
if(isempty()){
System.out.println( "空のリンクリスト");
}それ以外{
その他= head;
while(other!= null){
System.out.print(other.data);
other = other.next;
}
System.out.println();
}
}
}