Java 队列和堆栈
Java Queues and Stacks
我有一个小问题,希望有人能帮助我解决。
这是一项任务,所以我不应该使用从 java API 导入的 classes,也不应该以任何其他方式执行此操作(arraylist 会让这更容易.)
我创建了一个队列 class 和一个堆栈 class。
我正在尝试检索队列的头部,并将其添加到堆栈中。
我猜我需要创建一个方法,以某种方式获取列表头部的值并存储它,以便我可以使用它。
例如,如果我按顺序将“bob”、"jack" 和 "jill" 加入队列,它将看起来像:
鲍勃
杰克
吉尔
我想将 bob 从队列列表中取出,然后将他添加到 Stack 列表的头部,但我不知道如何做。很抱歉,如果我的问题不是很准确,我在表达我真正需要的内容时遇到了问题。如果需要任何其他信息,我会更新我的 post。感谢您的帮助。
这是我的队列class:
(LL 是我的 Link 列表 class)
public class Queue<T extends Comparable<T>> {
LL<T> theQueue;
public Queue() {
theQueue = new LL<T>();
}
public boolean isEmpty() {
return theQueue.isEmpty();
}
public void enqueue(T value) {
theQueue.insertTail(value);
}
public T dequeue() throws QueueException {
T retval = null;
try {
retval=theQueue.deleteHead();
}catch (LLException e) {
throw new QueueException ("Queue is empty");
}
return retval;}
public String toString() {
return theQueue.toString();
}}
我的堆栈 Class:
public class Stack<T extends Comparable<T>>{
LL<T> theStack;
public Stack()
{
theStack = new LL<T>();
}
public boolean isEmpty()
{
return theStack.isEmpty();
}
public void push(T value)
{
theStack.insertHead(value);
}
public T pop() throws StackException
{
T retval = null;
try
{
retval = theStack.deleteHead();
}
catch (LLException e)
{
throw new StackException("Stack Underflow");
}
return retval;
}
public boolean isFull()
{
return false;
}
public String toStrin()
{
return theStack.toString();
}
主要Class:
public static void main(String[] args) {
Stack <String> hired = new Stack<String>();
Stack <String> fired = new Stack<String>();
Queue <String> apps = new Queue<String>();
String temp;
for (int i = 0; i < 1000; i++) {
System.out.println("Enter the number of the action to perform:");
System.out.println("1. Accept Application");
System.out.println("2. Hire");
System.out.println("3. Fire");
System.out.println("4. Exit");
Scanner kb = new Scanner(System.in);
int key = kb.nextInt();
switch (key) {
case 1:
System.out.println("Enter applicant's name and ID separated by semi-colon:");
String applicant = kb.next() + "\n";
System.out.println("You entered " + applicant);
apps.enqueue(applicant);
break;
case 2:
try{
temp = apps.dequeue();
} catch (QueueException s) {
}
try{ apps.dequeue(); }
catch (QueueException s){
System.out.println("Queue is empty");}
hired.push(temp);
case 3:
System.out.println();
case 4: System.out.println("Bye");
System.exit(0);
}
}
所以它不会让我在没有 try and catch 的情况下将 apps.dequeue()
分配给 temp
。但是当我执行 hired.push(temp);
时,我收到一条错误消息,提示 temp 可能尚未初始化。
使用迭代器(如果您需要将值从队列的随机位置压入堆栈)。对于您的作业,简单的 dequeue 方法应该可以正常工作,正如另一个答案中所指出的那样。
在调用 dequeue 方法之前,调用此迭代器并检查是否有 hasNext()。如果为真,则使用 iterator.next() 获取值并存储它。
现在您的值位于 'head' 位置。现在调用 dequeue 方法并删除 head 值。现在只需将您的存储值压入堆栈
我想你想要做的是 "To dequeue "bob" 从队列中添加到堆栈中",不是吗?
所以我想你已经知道该怎么做了:
Queue<String> q = new Queue<String>();
Stack<String> s = new Stack<String>();
// ... enqueue three strings
String temp = q.dequeue();
s.push(temp);
是 - 此任务与您的 Queue 和 Stack 的实施无关 class。这只是关于使用界面。只要你正确地实现了它们,这些代码就可以工作。
编辑
所以也许这就是您想要的:
String temp = ""; // explicitly initialize
try {
temp = q.dequeue();
s.push(temp);
} catch {
}
我把dequeue和push都放在了try块中:如果dequeue失败,什么都不push。这适合你吗?
我有一个小问题,希望有人能帮助我解决。 这是一项任务,所以我不应该使用从 java API 导入的 classes,也不应该以任何其他方式执行此操作(arraylist 会让这更容易.) 我创建了一个队列 class 和一个堆栈 class。 我正在尝试检索队列的头部,并将其添加到堆栈中。 我猜我需要创建一个方法,以某种方式获取列表头部的值并存储它,以便我可以使用它。 例如,如果我按顺序将“bob”、"jack" 和 "jill" 加入队列,它将看起来像:
鲍勃
杰克
吉尔
我想将 bob 从队列列表中取出,然后将他添加到 Stack 列表的头部,但我不知道如何做。很抱歉,如果我的问题不是很准确,我在表达我真正需要的内容时遇到了问题。如果需要任何其他信息,我会更新我的 post。感谢您的帮助。
这是我的队列class: (LL 是我的 Link 列表 class)
public class Queue<T extends Comparable<T>> {
LL<T> theQueue;
public Queue() {
theQueue = new LL<T>();
}
public boolean isEmpty() {
return theQueue.isEmpty();
}
public void enqueue(T value) {
theQueue.insertTail(value);
}
public T dequeue() throws QueueException {
T retval = null;
try {
retval=theQueue.deleteHead();
}catch (LLException e) {
throw new QueueException ("Queue is empty");
}
return retval;}
public String toString() {
return theQueue.toString();
}}
我的堆栈 Class:
public class Stack<T extends Comparable<T>>{
LL<T> theStack;
public Stack()
{
theStack = new LL<T>();
}
public boolean isEmpty()
{
return theStack.isEmpty();
}
public void push(T value)
{
theStack.insertHead(value);
}
public T pop() throws StackException
{
T retval = null;
try
{
retval = theStack.deleteHead();
}
catch (LLException e)
{
throw new StackException("Stack Underflow");
}
return retval;
}
public boolean isFull()
{
return false;
}
public String toStrin()
{
return theStack.toString();
}
主要Class:
public static void main(String[] args) {
Stack <String> hired = new Stack<String>();
Stack <String> fired = new Stack<String>();
Queue <String> apps = new Queue<String>();
String temp;
for (int i = 0; i < 1000; i++) {
System.out.println("Enter the number of the action to perform:");
System.out.println("1. Accept Application");
System.out.println("2. Hire");
System.out.println("3. Fire");
System.out.println("4. Exit");
Scanner kb = new Scanner(System.in);
int key = kb.nextInt();
switch (key) {
case 1:
System.out.println("Enter applicant's name and ID separated by semi-colon:");
String applicant = kb.next() + "\n";
System.out.println("You entered " + applicant);
apps.enqueue(applicant);
break;
case 2:
try{
temp = apps.dequeue();
} catch (QueueException s) {
}
try{ apps.dequeue(); }
catch (QueueException s){
System.out.println("Queue is empty");}
hired.push(temp);
case 3:
System.out.println();
case 4: System.out.println("Bye");
System.exit(0);
}
}
所以它不会让我在没有 try and catch 的情况下将 apps.dequeue()
分配给 temp
。但是当我执行 hired.push(temp);
时,我收到一条错误消息,提示 temp 可能尚未初始化。
使用迭代器(如果您需要将值从队列的随机位置压入堆栈)。对于您的作业,简单的 dequeue 方法应该可以正常工作,正如另一个答案中所指出的那样。 在调用 dequeue 方法之前,调用此迭代器并检查是否有 hasNext()。如果为真,则使用 iterator.next() 获取值并存储它。 现在您的值位于 'head' 位置。现在调用 dequeue 方法并删除 head 值。现在只需将您的存储值压入堆栈
我想你想要做的是 "To dequeue "bob" 从队列中添加到堆栈中",不是吗? 所以我想你已经知道该怎么做了:
Queue<String> q = new Queue<String>();
Stack<String> s = new Stack<String>();
// ... enqueue three strings
String temp = q.dequeue();
s.push(temp);
是 - 此任务与您的 Queue 和 Stack 的实施无关 class。这只是关于使用界面。只要你正确地实现了它们,这些代码就可以工作。
编辑 所以也许这就是您想要的:
String temp = ""; // explicitly initialize
try {
temp = q.dequeue();
s.push(temp);
} catch {
}
我把dequeue和push都放在了try块中:如果dequeue失败,什么都不push。这适合你吗?