2015-07-21 18 views
11

Więc jestem dość nowy w Javie i programowaniu i zastanawiałem się, jak utworzyć klasę węzłów?Tworzenie klasy węzłów w Javie

Do tej pory mam:

public class ItemInfoNode{ 
    private ItemInfoNode next; 
    private ItemInfoNode prev; 
    private ItemInfo info; 
    public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){ 
     info = info; 
     next = next; 
     prev = prev; 
    } 
    public void setInfo(ItemInfo info){ 
     info = info; 

    } 
    public void setNext(ItemInfoNode node){ 
     next = node; 
    } 
    public void setPrev(ItemInfoNode node){ 
     prev = node; 
    } 
    public ItemInfo getInfo(){ 
     return info; 
    } 
    public ItemInfoNode getNext(){ 
     return next; 
    } 
    public ItemInfoNode getPrev(){ 
     return prev; 
    } 

} 

Prawie pytanie zadane przez tych metod więc umieścić te w dół, ale następne pytanie zadaje mi odnieść się do głowy i ogona węzłów ItemInfoNode. Tylko trochę zdezorientowani. Dzięki

EDYCJA: Dzięki za pomoc! Próbuję utworzyć metodę "InsertInfo", która umieszcza informacje takie jak nazwa, cena, numer tagu itp. W jednym węźle. Jak mogę utworzyć tę metodę?

Do tej pory mam to .. Mam konstruktora Iteminfo w innej klasy, który posiada wszystko to, ale nie jestem pewien, jak korzystać ze/czy jestem nawet robić ..

public void InsertInfo(String name, String rfdnumber, double price, String original_position){ 

     head = new ItemInfoNode (Iteminfo, head); 
    } 
+1

Klasa wydaje się być w porządku. Węzły głowa i ogon powinny być zdefiniowane w innej klasie o nazwie 'LinkedList' lub podobnej. –

+0

Jak mam utworzyć tę klasę? Dokładniej, w jaki sposób utworzyć te nagłówkowe i puste odwołania. – John

+0

Zmień "info = info;" na "this.info = info;", to samo dla pozostałych pól w konstruktorze i ustawieniach. – Pshemo

Odpowiedz

8

Witamy w Javie! Te węzły są jak klocki, muszą być zmontowane, aby robić niesamowite rzeczy! W tym konkretnym przypadku, węzły mogą reprezentować listę, połączonej listy, można zobaczyć przykład tutaj:

public class ItemLinkedList { 
    private ItemInfoNode head; 
    private ItemInfoNode tail; 
    private int size = 0; 

    public int getSize() { 
     return size; 
    } 

    public void addBack(ItemInfo info) { 
     size++; 
     if (head == null) { 
      head = new ItemInfoNode(info, null, null); 
      tail = head; 
     } else { 
      ItemInfoNode node = new ItemInfoNode(info, null, tail); 
      this.tail.next =node; 
      this.tail = node; 
     } 
    } 

    public void addFront(ItemInfo info) { 
     size++; 
     if (head == null) { 
      head = new ItemInfoNode(info, null, null); 
      tail = head; 
     } else { 
      ItemInfoNode node = new ItemInfoNode(info, head, null); 
      this.head.prev = node; 
      this.head = node; 
     } 
    } 

    public ItemInfo removeBack() { 
     ItemInfo result = null; 
     if (head != null) { 
      size--; 
      result = tail.info; 
      if (tail.prev != null) { 
       tail.prev.next = null; 
       tail = tail.prev; 
      } else { 
       head = null; 
       tail = null; 
      } 
     } 
     return result; 
    } 

    public ItemInfo removeFront() { 
     ItemInfo result = null; 
     if (head != null) { 
      size--; 
      result = head.info; 
      if (head.next != null) { 
       head.next.prev = null; 
       head = head.next; 
      } else { 
       head = null; 
       tail = null; 
      } 
     } 
     return result; 
    } 

    public class ItemInfoNode { 

     private ItemInfoNode next; 
     private ItemInfoNode prev; 
     private ItemInfo info; 

     public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev) { 
      this.info = info; 
      this.next = next; 
      this.prev = prev; 
     } 

     public void setInfo(ItemInfo info) { 
      this.info = info; 
     } 

     public void setNext(ItemInfoNode node) { 
      next = node; 
     } 

     public void setPrev(ItemInfoNode node) { 
      prev = node; 
     } 

     public ItemInfo getInfo() { 
      return info; 
     } 

     public ItemInfoNode getNext() { 
      return next; 
     } 

     public ItemInfoNode getPrev() { 
      return prev; 
     } 
    } 
} 

EDIT:

stwierdzenie ItemInfo jak to:

public class ItemInfo { 
    private String name; 
    private String rfdNumber; 
    private double price; 
    private String originalPosition; 

    public ItemInfo(){ 
    } 

    public ItemInfo(String name, String rfdNumber, double price, String originalPosition) { 
     this.name = name; 
     this.rfdNumber = rfdNumber; 
     this.price = price; 
     this.originalPosition = originalPosition; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getRfdNumber() { 
     return rfdNumber; 
    } 

    public void setRfdNumber(String rfdNumber) { 
     this.rfdNumber = rfdNumber; 
    } 

    public double getPrice() { 
     return price; 
    } 

    public void setPrice(double price) { 
     this.price = price; 
    } 

    public String getOriginalPosition() { 
     return originalPosition; 
    } 

    public void setOriginalPosition(String originalPosition) { 
     this.originalPosition = originalPosition; 
    } 
} 

Następnie Możesz użyć swoich węzłów na połączonej liście w następujący sposób:

public static void main(String[] args) { 
    ItemLinkedList list = new ItemLinkedList(); 
    for (int i = 1; i <= 10; i++) { 
     list.addBack(new ItemInfo("name-"+i, "rfd"+i, i, String.valueOf(i))); 

    } 
    while (list.size() > 0){ 
     System.out.println(list.removeFront().getName()); 
    } 
} 
+0

Dziękuję bardzo! – John

+0

@John Nie ma za co! –