Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Best way of implementing a queue's isEmpty() method

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Best way of implementing a queue's isEmpty() method

    You have a queue called someQ
    There are two ways to find out if the queue is empty()
    head == null;
    size()  == 0;
    or
    size == 0;

    where size() is determined by the number of elements enqueued onto the queue by incrementing a global variable called
    size
    .

    Which is the best method and why?
    Last edited by pyler; April 6th, 2014 at 10:28 PM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Best way of implementing a queue's isEmpty() method

    You have some Queue<E> someQ = new Queue<E>()
    invalid syntax, Queue is an interface so you cannot use the new key word in java to make a new object of it.

    if you are determining the size, then use the size() method.
    comparing head == null is an alternative since peek, peekFirst method returns a null when the array is empty, but I think that is not 100% accurate. why?

    what if I have a Queue of String generic. and I pushed these values:
    "me"
    "you"
    null
    the top of stack in Queue is null so if you are determining if the Queue is empty using head == null, then you can conclude that the above Queue is empty, but actually not, it returns a null just because its top of stack is null, but actually it has 3 elements.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    pyler (April 6th, 2014)

Similar Threads

  1. Implementing a Linked List into a Queue class?
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 9th, 2012, 12:07 AM
  2. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  3. Using Queue, cannot find symbol method enqueue
    By firebluetom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2011, 01:41 PM
  4. Implementing a priority queue using a max heap
    By jsinclair1482 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:56 AM
  5. Implementing a queue or stack using a heap
    By tla280 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 1st, 2010, 12:29 AM

Tags for this Thread