2016-07-09 7 views
1

1つのノードに複数の情報を1つのリンクリストで追加しようとしています...どうすればいいですか?1つのノードに複数の情報を1つのリンクリストに格納する

ユーザーに複数の車両情報を問い合わせた後:plateNo(String), vehicleType(String), serviceType(String)この情報を車両ごとに保存する必要があります。私は、すべての車両を入室させ、洗濯を残すために、単独でリンクされたリストを使用する必要があります。

私のプログラムでは、サービスオーダーで車両洗浄に出入りするすべての車両を表示する必要があります。

どうすればよいですか?

これは私の単独のLinkedListです:

public class LinkedList<T> 
{ 
    private Node<T> head; // first node in the linked list 
    private int count; 

    public int getCount() { 
     return count; 
    } 

    public Node getHead() { 
     return head; 
    } 

    public LinkedList() { 
     head = null; // creates an empty linked list 
     count = 0; 
    } 

    public void displayList(){ 
     Node<T> current = head;  // start at beginning 
     while(current != null)  // until end of list, 
     { 
      System.out.print(current.getData() + " "); 
      current = current.getNext(); 
      //move to next link 
     } 
     System.out.println(""); 
    } 

    public Node deleteFront() 
    { 
     Node<T> temp = head; 
     if(head.getNext() == null) // if only one item 
      return null;   // return null 
     head = head.getNext();  // first --> old next 
     count--; 
     return temp; 
    } 

    public void removeValue(T value) 
    { 
     Node<T> current = head, prev = null; 
     while (current != null) 
     { //if current node contains value 
      if (value == current.getData()) 
      { 
       //handle front removal (case 1) 
       if(prev == null) 
        head = current.getNext(); 
       else //handle mid removal (case 2) 
        prev.setNext(current.getNext()); 
        // prev node now points to        maxNode's (a.k.a current)       successor, removing max node. 

       break; // remove first occurence only 
      } 
      // update prev to next position (curr) 
      prev = current; 
      // move curr to the next node 
      current = current.getNext(); 
     } 
    } 

    public void addFront(T n) 
    { 
     Node<T> newNode = new Node<T>(n); 
     newNode.setNext(head); 
     head = newNode; 
     count++; 
    } 
} 

マイノード

public class Node<T> { 
    private T data; 
    private Node next; 
    public T getData() { 
     return data; 
    } 
    public void setData(T data) { 
     this.data = data; 
    } 
    public Node getNext() { 
     return next; 
    } 
    public void setNext(Node next) { 
     this.next = next; 
    } 
    public Node(T data) { 
     this.data = data; 
     this.next = null; 
    } 
} 

答えて

3

私は私がどのように...単独リンクリストに1つのノードにいくつかの情報を追加しようとしていますそれを行う?

...考えてオブジェクト指向!そのモデル車両クラスを作成します。

class Vehicle { 
    String plateNo; 
    String vehicleType; 
    String serviceType; 
    // constructors, getters, setters, other methods ... 
} 

あなたは既にジェネリックNode<T>を持っているので、それを使用します。

Vehicle vehicle = callAwesomeMethodThatCreatesVehicleInstance(); 
Node<Vehicle> node = new Node(vehicle); 

今すぐあなたのリンクリストに、このようなノードを使用することができます。

+0

正直なところ...これは答えることが広すぎます。私は短く妊娠した答えでOOPの概念を説明することはできません。時間をかけて学んでください(何年もかかるかもしれません)。 – Seelenvirtuose

0

コードは正常です。保存したいすべての情報を含む新しいクラスを定義するだけで済みます。ジェネリックデータ型TのNodeクラスをすでに作成しているので、ここで作成する新しいクラスを挿入できます。

リンクリストのためのあなたのコード内で次に
class Details{ 
    String plateNo; 
    String vehicleType; 
    String serviceType; 

    public Details(){ 
     this.plateNo = ""; 
     this.vehicleType = ""; 
     this.serviceType = ""; 
    } 
} 

public class LinkedList<T> 
{ 
    private Node<Details> head = new Details(); 
    //rest of the class 

} 
+0

メインクラスでこのメソッドを使用するにはどうすればよいですか?申し訳ありません、私はプログラミングの初心者です... – Minchae

+0

メインクラスの外に '詳細'クラスをプライベート宣言することなく作成できます。次に、あなたのコードで 'T'の代わりに 'Details'を使用してください - 専用ノード head;プライベートノードになります。

head = new Details(); –

+0

一般的な 'LinkedList 'の中に 'Node

'があることは非常に悪い示唆です。 – Seelenvirtuose

関連する問題