Thread: ADT - destroyQueue; count vs null pointer

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70

    ADT - destroyQueue; count vs null pointer

    When writing the destroyQueue function for my queue ADT, is there really that much of a difference in using

    Code:
    while(queue->count)
    as opposed to

    Code:
    while(queue->front != NULL)
    for the control statement of the loop that steps through the queue and destroys it? Using the assumption that the rest of the code and functions are bug free, and that the queue counts and next pointers are working correctly to facilitate those conditions. I don't see a difference, but then, I'm just a student and may not be seeing some sort of subtle potential pitfall.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well, if you use count, then you help to minimise the scope of knowing that the queue is built on a linked list to only the insert and delete functions.

    In fact, you wouldn't use count directly either, but say
    while ( !queue.empty() )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    Quote Originally Posted by Salem View Post
    In fact, you wouldn't use count directly either, but say
    while ( !queue.empty() )
    Hey, I hadn't thought of that. I guess that adds a third option to try.

    Quote Originally Posted by Salem View Post
    Well, if you use count, then you help to minimise the scope of knowing that the queue is built on a linked list to only the insert and delete functions.
    But how is someone going to know one way or the other if they never see the actual code? If they're seeing the code, they're going to find out anyway. Also, even if the control statement uses either queue->count or !queueEmpty(queue), if they're looking at that then they're also looking at the rest of the code too, so they'll see from the inside of the loop that it's a linked list (as the nodes get deleted and queue->front gets refreshed).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But how is someone going to know one way or the other if they never see the actual code?
    It's not about them, it's about you (as author / maintainer).

    If you wanted to re-implement it using say an array rather than a list, then you would only have to modify insert and delete.
    You don't actually need to know anything about how to delete a particular node inside the delete all function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    Thanks for the input and ideas Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM