【问题标题】:Storing more than one information into one Node in a singly linked list将多个信息存储到单链表中的一个节点中
【发布时间】:2016-07-09 10:41:03
【问题描述】:

我正在尝试将多个信息添加到单链表中的一个节点中...我该怎么做?

在向用户询问几个车辆信息后:plateNo(String), vehicleType(String), serviceType(String) 我必须为每辆车存储此信息。我必须使用单链表来存储所有进出洗车场的车辆。

然后,我的程序应该显示所有进入和离开洗车场的车辆及其服务订单。

我该怎么做?

这是我的单链表:

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

【问题讨论】:

    标签: java singly-linked-list


    【解决方案1】:

    我正在尝试将多个信息添加到单链表中的一个节点中...我该怎么做?

    ...通过思考面向对象!创建一个模拟车辆的类:

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

    你已经有一个 generic Node&lt;T&gt;,所以使用它:

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

    现在你可以在你的链表中使用这样的节点了。

    【讨论】:

    • 老实说......这太宽泛了,无法回答。我无法用简短而有意义的答案来解释 OOP 概念。请花时间学习(可能需要数年)。
    【解决方案2】:

    您的代码看起来不错。您只需要定义一个包含您要存储的所有信息的新类。由于您已经为通用数据类型 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
    
    }
    

    【讨论】:

    • 如何在主类中使用这个方法?抱歉,我是编程新手...
    • 您可以在主类之外创建“详细信息”类,而无需将其声明为私有。然后在你的代码中使用'Details'而不是'T' - private Node head;变为私有 Node
      head = new Details();
    • 在通用 LinkedList&lt;T&gt; 中包含 Node&lt;Details&gt; 是一个非常糟糕的建议。
    • 为什么这是个坏主意?
    • 嗯..它完全破坏了泛型机制。
    猜你喜欢
    • 2013-04-14
    • 2017-07-14
    • 2011-10-27
    • 2016-08-13
    • 2018-01-21
    • 1970-01-01
    • 2018-03-20
    • 2011-11-04
    • 1970-01-01
    相关资源
    最近更新 更多