Problem with Circular Linked Queue Program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shanknbake

    #1

    Problem with Circular Linked Queue Program

    Here is my code. I've noted where the program crashes. I'm doing this
    program as a project for school.

    //cqueue.h file
    //HEADER FILE

    --------------------------------------------------------------------
    //cqueue.cpp file
    //IMPLEMENTATION OF cqueue CLASS

    -------------------------------------------------------------------
    //driver.cpp file
    //DRIVER FILE


    The problem appears to be in my driver file under Deletepassenger and
    showpassenger functions.
    Plz let me know if something pops out at u
    thanks
    -Kevin

  • doug turnbull

    #2
    Re: Problem with Circular Linked Queue Program


    shanknbake wrote:
    Here is my code. I've noted where the program crashes. I'm doing this
    program as a project for school.
    >
    //cqueue.h file
    //HEADER FILE

    --------------------------------------------------------------------
    //cqueue.cpp file
    //IMPLEMENTATION OF cqueue CLASS

    -------------------------------------------------------------------
    //driver.cpp file
    //DRIVER FILE

    >
    The problem appears to be in my driver file under Deletepassenger and
    showpassenger functions.
    Plz let me know if something pops out at u
    thanks
    -Kevin
    Is it kosher to get help from others like this on your school project,
    I know for many schools it is an honor code violation. The kinds of
    help and who you can ask may be restricted by your school or instructor.

    Comment

    • shanknbake

      #3
      Re: Problem with Circular Linked Queue Program

      I agree completely doug, however I'm not asking for someone to write
      the code for me..but merely to look over the code I've written and
      point out an obvious mistake...which I'm over looking.

      On Nov 1, 2:56 pm, "doug turnbull" <pythag...@gmai l.comwrote:
      shanknbake wrote:
      Here is my code. I've noted where the program crashes. I'm doing this
      program as a project for school.
      >
      //cqueue.h file
      //HEADER FILE

      --------------------------------------------------------------------
      //cqueue.cpp file
      //IMPLEMENTATION OF cqueue CLASS

      -------------------------------------------------------------------
      //driver.cpp file
      //DRIVER FILE
      http://rafb.net/paste/results/iZbqug40.html
      >
      The problem appears to be in my driver file under Deletepassenger and
      showpassenger functions.
      Plz let me know if something pops out at u
      thanks
      -KevinIs it kosher to get help from others like this on your school project,
      I know for many schools it is an honor code violation. The kinds of
      help and who you can ask may be restricted by your school or instructor.

      Comment

      • Alan Johnson

        #4
        Re: Problem with Circular Linked Queue Program


        shanknbake wrote:
        Here is my code. I've noted where the program crashes. I'm doing this
        program as a project for school.
        >
        //cqueue.h file
        //HEADER FILE

        --------------------------------------------------------------------
        //cqueue.cpp file
        //IMPLEMENTATION OF cqueue CLASS

        -------------------------------------------------------------------
        //driver.cpp file
        //DRIVER FILE

        >
        The problem appears to be in my driver file under Deletepassenger and
        showpassenger functions.
        Plz let me know if something pops out at u
        thanks
        -Kevin
        Your enqueue logic seems suspect to me. You have:
        tempPtr->next=NULL;

        if(IsEmpty())
        {
        rear = tempPtr;
        count++;
        }
        else if(!IsFull())
        {
        rear->next=rear;
        rear = tempPtr;
        count++;
        }

        If your queue is empty, then you wind up with a queue with a single
        node whose next pointer points to NULL, which doesn't fit the
        definition of a circular queue.

        Assuming that first problem is fixed, the else case doesn't seem
        correct either. You wind up with rear pointing to the new node, and
        the rest of the queue is leaked.

        Just off the top of my head (meaning not compiled or tested) I would
        expect it to look something like:

        if (IsEmpty())
        {
        rear = tempPtr->next = tmpPtr;
        count++;
        }
        else if (!IsFull())
        {
        tempPtr->next = rear->next;
        rear = tempPtr;
        count++;
        }

        Note especially the base case of 1 node. It's next pointer should
        point back to itself, or else there is nothing circular about the
        queue.

        --
        Alan Johnson

        Comment

        • Alan Johnson

          #5
          Re: Problem with Circular Linked Queue Program


          Alan Johnson wrote:
          Note especially the base case of 1 node. It's next pointer should
          point back to itself, or else there is nothing circular about the
          queue.
          This also seems to plague your deque function and your destructor.
          Mentally step through your dequeue function for the case when count==1
          and think about what happens.

          Also, your destructor tries to clean up by looping through your list
          until it hits NULL, which just won't ever happen in a circular queue.
          I suggest reusing your dequeue logic (after you fix it) for the
          destructor. That is, just keep calling dequeue until count == 0.

          --
          Alan Johnson

          Comment

          • shanknbake

            #6
            Re: Problem with Circular Linked Queue Program

            thanks Alan,
            Let me put these ideas into effect and see what i come up with. I'll
            keep you posted of any success.
            -Kevin

            On Nov 1, 4:44 pm, "Alan Johnson" <a...@yahoo.com wrote:
            Alan Johnson wrote:
            Note especially the base case of 1 node. It's next pointer should
            point back to itself, or else there is nothing circular about the
            queue.This also seems to plague your deque function and your destructor.
            Mentally step through your dequeue function for the case when count==1
            and think about what happens.
            >
            Also, your destructor tries to clean up by looping through your list
            until it hits NULL, which just won't ever happen in a circular queue.
            I suggest reusing your dequeue logic (after you fix it) for the
            destructor. That is, just keep calling dequeue until count == 0.
            >
            --
            Alan Johnson

            Comment

            Working...