将多个信息存储在单个链接列表中的一个节点中

时间:2016-07-09 10:41:04

标签: java singly-linked-list

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

在询问用户多个车辆信息后:// Get current document and current layer var doc = app.activeDocument; var activeLay = doc.activeLayer; var a=0 // find the current layer and assign its code to the variable a for(i=doc.layers.length-1; i >=0; ) { if(doc.layers[i]==activeLay) { a=i; alert("a"+a); break; } else{ i--; } alert ("i"+i); } // Now cycle remaining layer under the exsiting one, and jump the selection // and delete the outer area of selection for each layer for(i=a-1; i >=0;) { // make layer i active doc.activeLayer=doc.layers[i]; alert ("active layer"+i); // where is my selection in regards to the active layer? var s = app.activeDocument.selection.bounds; var xSo=s[0]; var ySo=s[1]; var xLo = activeLay.bounds[0].value; var yLo = activeLay.bounds[1].value; // I have to go from actual selection poisition to the NEXT layer... which i just made active... DeltaX=xLo-s[0]; DeltaY=ySo-s[1]; doc.selection.translateBoundary(DeltaX,DeltaY); //Now invert selection and delete doc.selection.invert doc.selection.fill (fillType, mode, 0, preserveTransparency) // ??? here what i cannot do? doc.selection.invert i--; } 我将为每辆车存储此信息。我必须使用单链表来存储所有进入和离开洗车的车辆。

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

我该怎么做?

这是我的单链表:

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

2 个答案:

答案 0 :(得分:3)

  

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

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

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

您已经拥有通用 Node<T>,因此请使用它:

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

现在,您可以在链接列表中使用此类节点。

答案 1 :(得分: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

}