Hi friends,
i was recently started writing a program on java needed for my practicals
but as i started i found an error which deletes last element of queue
actually i want to delete first element of queue
anybody can solve it

import java.util.*;

class VQueue
{
int size;
int qarr[];
int rear;       
int front;
int i;
VQueue(int n)
{
    size=n;
    qarr=new int[n];
    rear=-1;
    front=0;
    i=0;
}
void insert(int val)
{
    rear++;
    if(rear==size)
    {
        System.out.println("Queue is full!! Remove some items...");
        rear--;
    }
    else
    {
        qarr[rear]=val;
    }
}
int remove()
{
    int item;
    if(rear==-1)
    {
        System.out.println("Queue is Empty!! Add some items first...");
        return 0;
    }
    else
    {
        front=0;
        item=qarr[front];
        front--;
        rear--;
        return item;
    }
}
public void display()
{
    int i;
    if(rear==-1)
    {
        System.out.println("Queue is Empty!! Add some items first...");
    }
    else
    {
        for(i=0;i<=rear;i++)
        {
            System.out.println("Queue element "+(i+1)+" : "+qarr[i]);
        }
    }
}

}//end of class VQueue

class QueueEX
{
public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of queue : ");
    int n=sc.nextInt();
    int item;
    VQueue vq=new VQueue(n);
    int ch;
    do
    {
        System.out.println("\nMenu:\n1.Insert\n2.Remove\n3.Display\n4.Exit\n");
        ch=sc.nextInt();
        switch(ch)
        {
        case 1:
            System.out.print("Enter an elemnt to insert in queue : ");
            int val=sc.nextInt();
            vq.insert(val);
            break;
        case 2:
            item=vq.remove();
            System.out.print("Item deleted from queue is : "+item);
            break;
        case 3:
            vq.display();
            break;
        case 4:
            break;
        default:
            System.out.print("Enter a valid choice");
        }
    }while(ch!=4);
}
}

front--; is not working as required

Edited 4 Years Ago by Ezzaral: Added code markup.

Can you show the debug print out when the problem happens?
Add enough more println statements to show the values of all the variables as they are used.

I am just getting error in remove() method other methods insert() & display() working properly
it does not delete first element
insted it deletes last element but actually show deleting first element
and first element is still in queue but last element deleted

queue

the attachment shows what is the error
the last element is deleted instead first

This question has already been answered. Start a new discussion instead.