Thread: Creating a Queue using Double Linklist

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Athul
    What I don't understand is how the author getting the answer. Have you seen the video???
    As I mentioned earlier, I could not access your video. Now that I see it... oh gosh, you mean he actually wrote:
    Code:
    temp = head.first;
    if (temp == (LISTITEM*)&head) // if the head of the queue points to itself ...
    This is a terrible idea. LISTITEM and LISTHDR are entirely different structs, so one should not be routinely casting between pointers to them. I don't think the author is actually "getting the answer": the address of head is likely to be the address of its member named first, but that is fundamentally different from the address stored in that member, i.e., the value of head.first. Consequently, if he appears to be "getting the answer", he might be faking it, or perhaps the code that he actually tested is different from what he showed in the video. Either way, I would not recommend this tutorial at all.

    What you should be doing is stuff like:
    Code:
    if (head.first)
    because if you code this sensibly, then when the queue is empty, both head.first and head.last will be null pointers.

    EDIT:
    OHHH... I see now. Look at enqueue:
    Code:
    item->next = (LISTITEM*)&head;
    What this tutorial is doing is basically using (LISTITEM*)&head as a sentinel value, i.e., a special value that denotes the end, but otherwise is not a valid value. It makes no sense, but that's what they are doing. The usual approach is to write:
    Code:
    item->next = NULL;
    Last edited by laserlight; 10-10-2018 at 02:28 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double ended priority queue
    By apatil65 in forum C Programming
    Replies: 8
    Last Post: 06-22-2013, 11:12 AM
  2. creating a queue of stacks
    By AJOHNZ in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2009, 07:47 PM
  3. stack, queue and linklist
    By clairmonte in forum C Programming
    Replies: 1
    Last Post: 07-12-2009, 05:14 AM
  4. double pointer in struct and queue
    By sumdude in forum C Programming
    Replies: 1
    Last Post: 10-21-2008, 04:53 AM
  5. Help creating Message Queue
    By trlpkguy in forum C Programming
    Replies: 5
    Last Post: 10-30-2006, 06:51 PM

Tags for this Thread