[英]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.