Results 1 to 4 of 4
  1. #1
    Java01 is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default Help with Queues in Java

    Im struggling to come up with code on how to find out if the queue is empty/full

    I know it would be if statements but im not sure how i would go about it, any help?

  2. #2
    Java01 is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
        public ArrayQ () { 
    	//The constructor must initiallise the class's attributes 
    	head == 0; 
    	tail == -1;
    }	
     
    
        public boolean qEmpty () {
    	// fill in
    	// queue is empty if head position is bigger than tail position
    	if head > tail
    	{ 
    	qEmpty = true
    	else
    	false;
    	}
        }
    
        public boolean qFull () {
    	//fill in
    	//queue is full if tail position is bigger than SIZE - 2
    	if tail = SIZE -2
    	{ 
    	qFull = true
    	else
    	false;
    	}
    	
        }
        
        public void qAdd(Object x) {
    	// fill in. check the queue is not full. Add the object in the
    	// right position in the queue and alter the local attributes
    	// appropriately
    	if( SIZE == data[].length )
                doubleQueue( );
            tail = increment( tail );
            data[ tail ] = x;
            SIZE++;
        }
    dont know if thats any good?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Does it compile? What do the error messags say? Do you understand them? Copy and paste them here if you need help.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Some observations...

    Just some things that caught my eye...
    • qFull & qEmpty are methods that return booleans and you're not returning anything: add the return statement.

    • The comments indicate that

    Java Code:
    //queue is full if tail position is [B][U]bigger[/U][/B] than SIZE - 2
    and you have
    Java Code:
    if tail = SIZE -2
    which brings me to my next observation:
    • the if statement is incorrect because it's missing it's parentesis: if (statement)... which means that this code doesn't compile.
    • Where did this pseudo code come from?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Implementing a Stack Using two Queues
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 10:28 AM
  2. Replies: 1
    Last Post: 08-07-2007, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •