简体   繁体   中英

Java Queues and Stacks

I have a small problem that I'm hoping someone could help me with. This is an assignment, so I am not supposed to use classes imported from the java API nor am I supposed to do this in any other way (arraylist would have made this much easier.) I created a Queue class and a Stack class. I am trying to retrieve the head of the Queue, and add it to the Stack. I am guessing I will need to create a method that somehow gets the value of the head of the list and stores it so I can use it. For example, if I enqueued " bob", "jack", and "jill" to the Queue in that order, it will look like:

bob

jack

jill

I want to dequeue bob from the queue list, but add him to the head of the Stack list, but I can't figure out how. I'm sorry if my question is not very precise, I'm having problems wording what I really need. If any other information is needed I will update my post. Thanks for any help.

Here is my Queueclass: (LL is my Link List 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();

   }}

And my Stack 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();
  }

Main 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);
        }
    }

So it won't let me assign apps.dequeue() to temp without the try and catch. but then when I do hired.push(temp); I get an error saying temp may have not been initialized.

I think what you want to do is "To dequeue "bob" from the Queue and add it to the Stack", isn't it? So I think you have already tell what to do:

Queue<String> q = new Queue<String>(); 
Stack<String> s = new Stack<String>();
// ... enqueue three strings
String temp = q.dequeue();
s.push(temp);

Yes - this task has nothing to do with the implementation of your Queue and Stack class. It's only about using the interface. As long as you have implemented them correctly, these code work.

EDIT So maybe this is what you want:

String temp = ""; // explicitly initialize
try {
    temp = q.dequeue();
    s.push(temp);
} catch {
}

I put both dequeue and push in try block: if dequeue fails, nothing is to be pushed. Is this right for you ?

Use iterator (if you need to push value from a random position of queue into stack). For your assignment, simply dequeue method should work fine as pointed in another answer. Before calling the dequeue method, call this iterator and check if hasNext(). if true, get the value using iterator.next() and store it. Now you have the value in 'head' position. Now call dequeue method and delete the head value. Now simply push your stored value into stack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM