在Java中创建节点类

时间:2015-07-21 16:20:50

标签: java linked-list nodes

所以我对Java和编程很新,我想知道如何创建一个节点类?

到目前为止,我有:

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;
    }

}

问题就是这些问题,所以我把它们放下来,但是,下一个问题要求我引用ItemInfoNode节点的头部和尾部。这里有点困惑。感谢

编辑:谢谢你的帮助!我试图创建一个" InsertInfo"将名称,价格,标签号等信息放入一个节点的方法。我该如何创建此方法?

到目前为止,我得到了这个..我在一个不同的类中有一个Iteminfo构造函数,但是,我不确定如何使用它/如果我甚至应该这样做..

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

        head = new ItemInfoNode (Iteminfo, head);
    }

1 个答案:

答案 0 :(得分:11)

欢迎来到Java! 这个节点就像一个块,它们必须组装成令人惊奇的东西! 在这种特殊情况下,您的节点可以表示列表,链表,您可以在此处查看示例:

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;
        }
    }
}

编辑:

将ItemInfo声明为:

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;
    }
}

然后,您可以使用链接列表中的节点,如下所示:

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());
    }
}