以下は、Javaを使用して編集者が共有する単一のリンクリストです。ご不明な点がございましたら、メッセージを残してください。
最初にノードクラスを定義します
public class node {次のノード保護ノード; //ポインターフィールドpublic int data; // data field public node(int data){this。 data = data; } //このノードpublic void display(){systemを表示します。 out.print(data + ""); }}次に、単一のリンクリストを定義し、関連するメソッドを実装します。
public class linklist {public node first; //ヘッダープライベートINT POS = 0; //ノードパブリックリンクリストの位置{this.first = null;} //ヘッダーpublic void addfirstnode(int node = new node = new node(data); node.next = first = node;} tempnode = first; first = tempnode.next; return tempnode;} //任意の場所にノードを挿入し、index add(int index、int data){node node = new node(data); node current(data); node current; node first; first; while = first; = 0;} //任意の場所でノードを削除するパブリックノードdeleteBypos(int index){node current = first; node first; first; while(pos!= index){pos ++; pos ++; current = current.next;} if(current == first){first = first.next;}ノードのデータ(最初のデータのみを削除)パブリックノードdeleteBydata(int data){node current = first; node first = first; //前のノードを覚えておいてください(current.data!= data){if(current.next == null){return null;} current = current; current = current.next;} if(current == first){first = first.next;} != null) {current.display();current = current.next;}System.out.println();}// Find node information based on location public Node findByPos(int index) {Node current = first;if (pos != index) {current = current.next;pos++;}return current;}// Find node information based on data public Node findByData(int data) {Node current = first; while(current.data!= data){if(current.next == null)return null; current = current.next;} return current;}}}最後に、テストクラスを通じて関連するテストを行うことができます。
public class testlinkList {public static void main(string [] args){linklist linklist = new linklist(); linklist.addfirstnode(20); linklist.addfirstnode(21); linklist.addfirstnode(19); // print19,21,20 linklist.add(1、22); // print19,22,21,20 linklist.add(2、23); // print19,22,23,21,20 linklist.add(3、99); // print19,22,23,99,21,20 //このメソッドの呼び出しは、19,22,23,99,21,20 linklist.displayallnodes()を印刷します。 }}この時点で、単一のリンクリストの操作がここで終了しました。
上記は、編集者が紹介したJavaシングルリンクリストの実装コードです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!