gpt4 book ai didi

java - 实现的 LinkedQueue 中的 pop 方法正在删除所有值,而不是第一个值

转载 作者:行者123 更新时间:2023-12-02 12:11:44 25 4
gpt4 key购买 nike

通过这个 LinkedQueue 的实现,除了 pop() 方法之外的所有方法都可以正常工作。当使用 pop() 方法时,堆栈中的所有值都会消失,当它应该只删除第一个值时,堆栈会变成空

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

这是用于 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;
}
}
}
}

我还制作了自己的 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;
}
}

如前所述,我遇到的唯一问题是 pop() 方法,但如果您看到任何其他也有帮助的错误,我们将不胜感激。

最佳答案

替换

front = temp.getLink();

front = front.getLink();

关于java - 实现的 LinkedQueue 中的 pop 方法正在删除所有值,而不是第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com