Mistake in display and insert methods (double-ended queue)

Posted by MANAL on Stack Overflow See other posts from Stack Overflow or by MANAL
Published on 2010-05-23T15:03:23Z Indexed on 2010/05/23 15:30 UTC
Read the original article Hit count: 158

Filed under:
|

1) My problem

when i make remove from right or left program will be remove true but when i call diplay method the content wrong

like this I insert 12 43 65 23 and when make remove from left program will remove 12 but when call display method show like this 12 43 65

and when make remove from right program will remove 23 but when call display method show like this 12 43

Why ?????? );

and when i try to make insert after remove write this

Can not insert right because the queue is full . first remove right and then u can insert right

where is the problem ??

Please Help me

please

2) My code

FIRST CLASS

class dqueue
{
    private int fullsize;                    //number of all cells
    private int item_num;                    // number of busy cells only
    private int front,rear;
    public int j;
    private double [] dqarr;
//==========================================
    public dqueue(int s)                      //constructor
    {
        fullsize = s;
        front = 0;
        rear = -1;
        item_num = 0;
        dqarr = new double[fullsize];
    }
//==========================================
    public void insert(double data)
    {
        if (rear == fullsize-1)
            rear = -1;
        rear++;
        dqarr[rear] = data;
        item_num++;

    }
   public double removeLeft() // take item from front of queue
     {
   double temp = dqarr[front++]; // get value and incr front
   if(front == fullsize) 
   front = 0;
   item_num --; // one less item
   return temp;
    }
    public double removeRight() // take item from rear of queue
     {
   double temp = dqarr[rear--]; // get value and decr rear
   if(rear == -1) // 
   rear = item_num -1;
   item_num --; // one less item
   return temp;
    }
//=========================================

     public void display ()                //display items
{
for (int j=0;j<item_num;j++)               // for every element 
System.out.print(dqarr[j] +"  " );          // display it 
System.out.println("");
}

//=========================================
public int size()              //number of items in queue
{
return item_num;
}
//==========================================
public boolean isEmpty()       // true if queue is empty
{
return (item_num ==0);
}


} 

SECOND CLASS

import java.util.Scanner;
class dqueuetest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("              ***** Welcome here***** ");
        System.out.println("              *****  Mind Of Programming Group***** ");
       System.out.println("               _____________________________________________ ");
        System.out.println("enter size of your dqueue");
        int size = input.nextInt();
        dqueue mydq = new dqueue(size);
        System.out.println("");
        System.out.println("enter your itemes");
//=====================================
        for(int i = 0;i<=size-1;i++)
        {
           System.out.printf("item %d:",i+1);
           double item = input.nextDouble();
           mydq.insert(item);
           System.out.println("");
        }    

//=====================================  
int queue =size ;         
int c = 0 ;
while (c != 6)
{
    System.out.println("");
    System.out.println("************************************************");
    System.out.println("               MAIN MENUE");
    System.out.println("1- INSERT RIGHT  ");
    System.out.println("2- REMOVE LEFT");
    System.out.println("3- REMOVE RIGHT");
    System.out.println("4- DISPLAY");
    System.out.println("5- SIZE");
    System.out.println("6- EXIT");  
    System.out.println("************************************************");
    System.out.println("choose your operation by number(1-6)");  
    c = input.nextInt();
    switch  (c)
    {
    case 1:
   if (queue == size)
    System.out.print("Can not insert right because the queue is full . first remove right and then u can insert right   ");
          else { System.out.print("enter your item: ");
           double item = input.nextDouble();
           mydq.insert(item);}
          break;

   case 2:
       System.out.println("REMOVE FROM REAR :");
              if( !mydq.isEmpty() )
    {
  double item = mydq.removeLeft();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();

    break;

   case 3:
    System.out.println("REMOVE FROM FRONT :");
            if( !mydq.isEmpty() )
    {
  double item = mydq.removeRight();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();
     break;

    case 4:
    System.out.println("The items in Queue are :");  
        mydq.display();
    break;

    case 5:
     System.out.println("The  Size of the Queue is :"+mydq.size());
    break;

    case 6:
    System.out.println("Good Bye");

    break;

    default:
    System.out.println("wrong chiose enter again");
  }       //end switch
 }       //end while
}        // end main     

}//end class

© Stack Overflow or respective owner

Related posts about java

Related posts about deque