1

I was required to create a simple queue array implementation with basic methods as enqueue, dequeue, isEmpty, and stuff like that. My only problem is that Im stuck when it comes to the resize method, because if I want to add more values to my queue (with fixed size because is an array) I do not know how to make it work and keep all the values in place. Everything works just in case you were wondering, the only thing is that doesnt work is my resize (the method wrote in here wasn't the only one I tried). I'm going to put my main method as well if you want to try it, hope you can help, thanks.

Main Method:

  public class MainQueue {
              public static void main(String[] args) {

            int capacity=10;
            Queue<Integer> queue = new Queue<Integer>(capacity);

            queue.enqueue(1);
            queue.enqueue(2);
            queue.enqueue(3);
            queue.enqueue(4);
            queue.enqueue(5);
            queue.enqueue(6);
            queue.enqueue(7);
            queue.enqueue(8);
            queue.enqueue(9);
            queue.enqueue(10);

            System.out.println("Queue: "+ queue);

                            //WORKS SO FAR
                            queue.enqueue(11);
                            //11 is placed at the beginning of the queue  
                            //instead at the end and my last value is null (?)                              

Class queue:

 import java.util.NoSuchElementException;


 public class Queue <E>{
 private E[] elements;//array in generic
 private int front;//first element or front of the queue
 private int back;//last element or back of the queue
 private int capacity; //capacity of the queue
 private int count; //indicates number of elements currently stored in the queue

        @SuppressWarnings("unchecked")
        public Queue(int size)
        {
            capacity = size;
            count = 0;
            back = size-1;
            front = 0;
            elements =(E []) new Object[size];  //array empty   
        }

    //Returns true if the queue is empty or false
    public boolean isEmpty()
        {
            return count==0;//means its true
        }

    //Add elements to the queue 
    public void enqueue(E item)
        {  
            if(count == capacity) 
            {  
                 resize(capacity*2);
                  // System.out.println("Queue is full");  

             }

            back =(back+1) % capacity;    //example back=(0+1)%10=1
            elements[back]=item;
            //elements[0]=0
            //item=elements[count];
            count++;
            }


    //Public resize
    public void resize(int reSize){
        E[] tmp = (E[]) new Object[reSize];

           int current = front;
           for (int i = 0; i < count; i++)
              {
              tmp[i] = elements[current];
              current = (current + 1) % count;
              }
         elements = tmp;

           }


    //Dequeue method to remove head
    public E dequeue() 
            {
                if(isEmpty())
                    throw new NoSuchElementException("Dequeue: Queue is empty");
                else
                {
                    count--;
                    for(int x = 1; x <= count; x++)
                    {
                        elements[x-1] = elements[x];
                    }
                    capacity--;
                    return (E) elements;
                }
            }

//peek the first element
    public E peek()   
        {
            if(isEmpty())  
               {  
                   throw new NoSuchElementException("Peek: Queue is empty");
               }

               else  
                   return elements[front];
         }


//Print queue as string
     public String toString()    
           {  

           if(isEmpty()) {
               System.out.println("Queue is empty.");
            //throw new NoSuchElementException("Queue is empty");
           }

               String s = "[";  
               for(int i = 0; i <count; i++)  
               {  
                   if(i != 0)  
                       s += ", ";  
                   s = s + elements[i];// [value1,value2,....]  
               }  

               s +="]";  
              return s;  
           }  

       public void delete() {   //Delete everything
           count = 0;
       }
       }
2
  • Ups, sorry I wrote queue.enque(11); instead of queue.enqueue(11); Commented Nov 22, 2013 at 5:15
  • What your method reSize should do? Commented Nov 22, 2013 at 5:26

2 Answers 2

1

you forgot to update stuff when resizing: front, capacity and back .

public void resize(int reSize){
    E[] tmp = (E[]) new Object[reSize];

       int current = front;
       for (int i = 0; i < count; i++)
          {
          tmp[i] = elements[current];
          current = (current + 1) % count;
          }
     elements = tmp;
     front = 0;
     back = count-1;
     capacity=reSize;
       }
Sign up to request clarification or add additional context in comments.

1 Comment

remember to add amalds bugfix and use capacity on the mod operation :)
1

You have few mistakes in resizing when enqueing item which expand queue.

  1. in resize algorithm current = (current + 1) % count; should be (current + 1) % capacity

  2. You have to change capacity value in resize function capacity = resize;

Why are you changing capacity when dequeing?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.