tags:

views:

223

answers:

5

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
+1  A: 

I suspect that's what confusing you most is your display method...:

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("");
}

in this method you're completely ignored the "internal logical structure" of the queue you have coded, which depends crucially on the front and rear indices: indeed, note you don't even mention either of those indices here -- you just print the "physically first" item_num elements, which have no relationship whatsoever with the "logically present" ones.

You need, instead, to start with the frontth element (not the 0th one!) and show the item_num elements starting there (with wraparound at the end if you meet it, of course). This will display the logical contents of your queue instead of a "random-oid" slice of the array that implements it!-)

Alex Martelli
please more explain):
MANAL
+1  A: 

The implementation of display iterates through the backing array from 0 until item_num, so it prints the wrong values. It should iterate from front to rear instead (with checks for wrapping the index around at the end of the buffer) to print out the actual contents of the queue:

public void display() {
  int j = front;
  for (int count = 0; count < item_num; count++) {
    System.out.print(dqarr[j] + "  ");
    j = increment(j);
  }
  System.out.println();
}

private int increment(int index) {
  if (index == fullsize - 1)
    return 0;
  return ++index;
}

I defined an increment method to implement the index stepping in one single place - you have this in multiple places within your code.

Further note that there is no overflow check in your implementation: if I add fullsize + 1 elements to the queue, the first element will be silently overwritten.

Update: I realized that in your queue implementation rear indexes the last element of the queue (not the one after the last element, as is usual in dequeue implementations I have seen), so I modified the code example and my answer accordingly.

Péter Török
still there is problemwhen i used your method display make remove from both right and rear:( :(like thisi insert 12 34 45 67and make remove from leftout put become like this 34 45 ): ):please help me
MANAL
@MANAL, see my update.
Péter Török
A: 

If this is not homework and you're using Java 6 or newer, why not just use on the built-in Deques?

Those consist of ArrayDeque, LinkedList (retrofitted with this interface in Java 6), and the concurrent LinkedBlockingDeque.

R. Bemrose
A: 

this is homework and must do like this

help please

A: 

THANK YOU VERY MATCH MR.Péter Török