Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现链表堆栈和队列,而我不';我不知道我设置的代码是否足够有效_Java_Stack_Queue_Linked List - Fatal编程技术网

Java 实现链表堆栈和队列,而我不';我不知道我设置的代码是否足够有效

Java 实现链表堆栈和队列,而我不';我不知道我设置的代码是否足够有效,java,stack,queue,linked-list,Java,Stack,Queue,Linked List,一直在为分配的链表队列和堆栈类进行实现。我已经测试了这两个,它们似乎都能工作,但我担心实现的某些部分会比我目前设置的做得更好,我不想因为效率低下的代码而被扣分 以下是我开设的课程: 节点 堆叠 排队 汽车类别并不重要,它只是以字符串形式存储车牌信息 我最担心的是我用While循环设置的那些,我不确定是否有一种更有效的内存实现方法。有没有一种更标准化的设置方法我可能会错过?我要改变的一件事是,我会让LQueue同时引用队列的头和尾。这样,您就可以在固定时间内insert(),而不必迭代所有现有元素

一直在为分配的链表队列和堆栈类进行实现。我已经测试了这两个,它们似乎都能工作,但我担心实现的某些部分会比我目前设置的做得更好,我不想因为效率低下的代码而被扣分

以下是我开设的课程:

节点

堆叠

排队

汽车类别并不重要,它只是以字符串形式存储车牌信息


我最担心的是我用While循环设置的那些,我不确定是否有一种更有效的内存实现方法。有没有一种更标准化的设置方法我可能会错过?

我要改变的一件事是,我会让
LQueue
同时引用队列的头和尾。这样,您就可以在固定时间内
insert()
,而不必迭代所有现有元素


此外,如果您愿意,还可以使两个
size()
方法以恒定时间运行:跟踪成员变量中的当前大小,在
insert()
上递增,在
remove()上递减

我要改变的一件事是,我要使
LQueue
同时引用队列的头和尾。这样,您就可以在固定时间内
insert()
,而不必迭代所有现有元素


另外,如果您愿意,两种
size()
方法都可以在固定时间内运行:跟踪成员变量中的当前大小,在
insert()
上递增,在
remove()
上递减。我能想到的只有两件事:

  • 跟踪列表中元素的数量,int在添加时递增,在删除时递减。这将使size()方法立即生效。您所要做的就是返回int值

  • 对于队列,使用节点引用跟踪尾部,就像跟踪头部一样。这样,您就不必遍历列表来查找列表的末尾。尾部将始终是最后添加的节点。这将允许您不必在每次添加内容时都遍历列表;相反,你可以把它加到尾巴的末端


  • 我能想到的只有两件事:

  • 跟踪列表中元素的数量,int在添加时递增,在删除时递减。这将使size()方法立即生效。您所要做的就是返回int值

  • 对于队列,使用节点引用跟踪尾部,就像跟踪头部一样。这样,您就不必遍历列表来查找列表的末尾。尾部将始终是最后添加的节点。这将允许您不必在每次添加内容时都遍历列表;相反,你可以把它加到尾巴的末端

  • 考虑把这个考虑进去
    public class Node {
    Node next;
    Car car;
    
    /**
     * A node object, used for the creation of LinkedLists.
     * @param car
     */
    public Node(Car car)
    {
        next = null;
        this.car = car;
    }
    }
    
    public class LStack {
    Node head = null;
    
    /**
     * Adds a car object to the list
     * @param car = the car object to be added
     */
    public void push(Car car)
    {
        Node oldHead = head;
        head = new Node(car);
        head.next = oldHead;
    }
    
    /**
     * Removes the top car from the list
     * @return the car at the top of the list
     */
    public Car pop()
    {
        Car headCar = head.car;
        head = head.next;
        return headCar;
    }
    
    /**
     * Checks if the list is empty
     * @return whether or not the list is empty
     */
    public boolean isEmpty()
    {
        return (head == null);
    }
    
    /**
     * 
     * @return the size of the list
     */
    public int size()
    {
        Node nextNode = head;
        int count = 0;
        while (nextNode != null)
        {
            count++;
            nextNode = nextNode.next;
        }
        return count;
    }
    
    /**
     * Displays the list of car license plate information
     */
    public void display()
    {
        Node nextNode = head;
        while (nextNode != null)
        {
            System.out.print(nextNode.car.getLicense() + "|");
            nextNode = nextNode.next;
        }
        System.out.println();
    }
    
    /**
     * 
     */
    public void reverseStack()
    {
        // not implemented yet
    }
    }
    
    public class LQueue {
    Node head = null;
    
    /**
     * Adds a car object to the list
     * 
     * @param car
     *            = the car object to be added
     */
    public void insert(Car car) {
        Node current = head;
        if (current != null) {
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node(car);
        }
        else
        {
            head = new Node(car);
        }
    }
    
    /**
     * Removes the top car from the list
     * 
     * @return the car at the top of the list
     */
    public Car remove() {
        Car headCar = head.car;
        head = head.next;
        return headCar;
    }
    
    /**
     * Checks if the list is empty
     * 
     * @return whether or not the list is empty
     */
    public boolean isEmpty() {
        return (head == null);
    }
    
    /**
     * 
     * @return the size of the list
     */
    public int size() {
        Node nextNode = head;
        int count = 0;
        while (nextNode != null) {
            count++;
            nextNode = nextNode.next;
        }
        return count;
    }
    
    /**
     * Displays the list of car license plate information
     */
    public void display() {
        Node nextNode = head;
        while (nextNode != null) {
            System.out.print(nextNode.car.getLicense() + "|");
            nextNode = nextNode.next;
        }
        System.out.println();
    }
    
    /**
     * 
     */
    public void reverseQueue() {
    
    }
    }