简体   繁体   English

已实现的LinkedQueue中的pop方法将删除所有值,而不是第一个值

[英]pop method in implemented LinkedQueue is removing all values and not the first one

With this implementation of a LinkedQueue, all the methods except the pop() method work fine. 使用LinkedQueue的此实现,除pop()方法之外的所有方法都可以正常工作。 When using the pop() method all of the values in the stack disappear making it empty when it is supposed to remove the first value only 当使用pop()方法时,应该仅删除第一个值时,堆栈中的所有值都会消失,从而使其为空

Here is the LinkedQueue class 这是LinkedQueue类

import java.util.NoSuchElementException;

    public class LinkedQueue
    {
        Node front, rear;
        int size;

        public LinkedQueue()
        {
            front = null;
            rear = null;
            size = 0;
        }

        public boolean isEmpty()
        {
            if(front == null)
                return true;
            else
                return false;
        }

        public int getSize()
        {
            return size;
        }

        public void push(int data)
        {
            Node n = new Node(data);
            if(isEmpty())
                front = rear = n;
            else
            {
                rear.setLink(n);
                rear = n;
            }

            size++;
        }

        public int pop()
        {
            Node temp = new Node(front.getData());
            if(isEmpty())
            {
                throw new IllegalAccessError();
            }
            else
            {
                front = temp.getLink();
                size--;
            }

            return temp.getData();
        }

        public int peek()
        {
            if (isEmpty())
            {
                throw new NoSuchElementException("Stack is empty.");
            }
            else
            {
                return front.getData();
            }
        }

        public String toString()
        {
            Node tempFront = front;
            String returnStr = "Stack: [";
            while(tempFront != null)
            {
                returnStr += tempFront.getData() + ", ";
                tempFront = tempFront.getLink();
            }

            returnStr += "]";

            return returnStr;
        }
    }

Here is the Driver used for the LinkedQueue class: 这是用于LinkedQueue类的驱动程序:

import java.util.Scanner;

    public class Driver
    {
        public static void main(String[] args)
        {
            //declare variables and initialize scanner
            Scanner key = new Scanner(System.in);
            int size, choice, value, end;

            end = 0;

            //declare and initialize the stack
            LinkedQueue queue1 = new LinkedQueue();

            //loop to continue operations
            while(end == 0)
            {
                //print out menu for commands
                System.out.println("\t1) Push \n\t2) Pop \n\t3) Peek \n\t4) Size \n\t5) isEmpty \n\t6) End");
                System.out.print("Please choose an option: ");
                choice = key.nextInt();

                //switch the choice and execute commands
                switch (choice)
                {
                    case 1: System.out.println("Please enter a value: ");
                        value = key.nextInt();
                        queue1.push(value);
                        System.out.println(queue1.toString());
                        break;
                    case 2: queue1.pop();
                        System.out.println(queue1.toString());
                        break;
                    case 3: queue1.peek();
                        System.out.println(queue1.peek());
                        System.out.println(queue1.toString());
                        break;
                    case 4: System.out.println("Size: " + queue1.getSize());
                        System.out.println(queue1.toString());
                        break;
                    case 5: if(queue1.isEmpty())
                    {
                        System.out.println("Stack is empty.");
                    }
                    else
                        System.out.println("Stack is NOT empty.");
                        System.out.println(queue1.toString());
                        break;
                    case 6: end = 1;
                        System.out.println("Goodbye!");
                        break;
                }
            }
        }
    }

I have also made my own Node class 我也做了自己的Node类

public class Node
{
    int data;
    Node link;

    //contructor
    public Node(int d)
    {
        data =  d;
        link = null;
    }

    public int getData()
    {
        return data;
    }

    public Node getLink()
    {
        return link;
    }

    public void setData(int d)
    {
        data = d;
    }

    public void setLink(Node n)
    {
        link = n;
    }
}

As mentioned before the only issue I have is with the pop() method, but if you see any other errors that will also help, it would be much appreciated. 如前所述,我唯一的问题是pop()方法,但是如果您看到任何其他也会有所帮助的错误,将不胜感激。

Replace 更换

front = temp.getLink();

with

front = front.getLink();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java LinkedQueue 未获取前值的打印方法 - Print method For Java LinkedQueue not getting the front value 所有已实现类中的启动方法 - Starting method in all implemented classes 复制LinkedQueue - Copying a LinkedQueue Android 的 Activity Class 实现的哪一种方法不是模板方法? - Which one method implemented by Android's Activity Class is NOT A Template Method? 将所有值从一个类转换为另一个类方法 - get all values from one class to anothed class method 为什么 TreeSet 的第一个方法没有在 O(1) 时间内实现? - Why isn't TreeSet's first method implemented in O(1) time? 删除小数中除前两个数字以外的所有数字 - Removing all but the first two numbers in a decimal 将值从一个Arraylist复制到另一个时,所有值均设置为在第一个Arraylist中输入的最后一个值 - When copying the values from one Arraylist to another, all values get set as the last value entered in the first Arraylist 如何使方法首先在Java Fluent接口中弹出? - How to make a method pop first in java fluent interface? hadoop-所有映射方法为特定键产生的所有值都都发送给一个单一的reduce方法,对吗? - hadoop - is it right that all of the values spawned by all map methods for a specific key are all sent to one single reduce method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM