コードコピーは次のとおりです。
/**
*双方向リンクリストの実装
* @authorスキップ
* @version 1.0
*/
パブリッククラスdoubleNodelist <t> {
//ノードクラス
プライベート静的クラスノード<t> {
ノード<t> perv; //フロントノード
node <t> next; //最後のノード
Tデータ; //データ
パブリックノード(T T){
this.data = t;
}
}
プライベートノード<t> head; //ヘッドノード
プライベートノード<t> last; //テールノード
プライベートノード<t>その他; //スタンバイノードの保存のための一時操作
プライベートINTの長さ; //リンクリストの長さ
/**
*非グリコピック構造
*/
public doublenodelist(){
head = new node <t>(null);
last = head;
長さ= 0;
}
/**
*初期化中にノードを作成します
* @paramデータ
*/
public doublenodelist(t data){
head = new node <t>(data);
last = head;
長さ= 1;
}
/**
*ノードを追加します
* @paramデータが追加されたデータ
*/
public void add(t data){
if(isempty()){
head = new node <t>(data);
last = head;
長さ++;
}それ以外{
//テール挿入方法
その他= new node <t>(data);
other.perv = last;
last.next = other;
last =その他;
長さ++;
}
}
/**
*データを指定した後、ノードを挿入します
* @paramデータ指定データ
* @param InsertData挿入データ
* @return挿入が成功した場合はtrueを返し、挿入が失敗した場合はfalse。
*/
public boolean addafert(t data、t insertData){
その他= head;
while(other!= null){
if(other.data.equals(data)){
node <t> t = new node <t>(insertData);
t.perv =その他;
t.next = other.next;
other.next = t;
//最後のノードの後にノードを追加するかどうかを判断します
if(t.next == null){
last = t;
}
長さ++;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*データを指定する前にノードを挿入します
* @paramデータ指定データ
* @param InsertData挿入データ
* @return挿入が成功した場合はtrueを返し、挿入が失敗した場合はfalse。
*/
public boolean addbefore(t data、t insertData){
その他= head;
while(other!= null){
if(other.data.equals(data)){
node <t> t = new node <t>(insertData);
t.perv = other.perv;
t.next =その他;
other.perv.next = t;
長さ++;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*インデックスでデータを取得します
* @paramインデックスインデックス
* @returnデータ
*/
public t get(int index){
if(index> length || index <0){
新しいindexoutofboundsexception( "index:"+index);
}
その他= head;
for(int i = 0; i <index; i ++){
other = other.next;
}
return other.data;
}
/**
*新しい値は古い値を置き換えます
* @returnの成功は真実であり、偽として見つかりません
*/
パブリックブールセット(t oldvalue、t newValue){
その他= head;
while(other!= null){
if(other.data.equals(oldvalue)){
other.data = newValue;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*指定された要素を削除します
*削除する必要がある@paramデータ要素
* @returnは存在せず、成功に真実です
*/
public boolean remove(t data){
その他= head;
while(other!= null){
if(other.data.equals(data)){
other.perv.next = other.next;
長さ - ;
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*この要素がリンクリストに含まれているかどうか
* @returnは、偽ではなく真であると含まれています
*/
public boolean contains(t data){
その他= head;
while(other!= null){
if(other.data.equals(data)){
trueを返します。
}
other = other.next;
}
falseを返します。
}
/**
*最後のノードのデータを取得します
*最後のノードの@returnデータ
*/
public t getLast(){
last.dataを返します。
}
/**
*最初のノードのデータを取得します
* @return最初のノードのデータ
*/
public t getFirst(){
return head.data;
}
/**
*リンクリストの長さを取得します
* @returnの長さ
*/
public int getsize(){
戻り長。
}
/**
*空のリンクテーブルですか
* @return空のリストは真であり、空でないリストは偽です
*/
public boolean isempty(){
return length == 0;
}
/**
*リンクリストをクリアします
*/
public void clear(){
head = null;
長さ= 0;
}
/**
*リンクリストにすべてのノードを出力します
*/
public void printlist(){
if(isempty()){
System.out.println( "空のリンクリスト");
}それ以外{
その他= head;
for(int i = 0; i <length; i ++){
System.out.print(other.data+"");
other = other.next;
}
System.out.println();
}
}
}