Circular deck without a sentry

Hey Stackoverflow I am working on my homework and I am trying to change the circular snap without a sentry. Here are my data structures:

struct DLink {

 TYPE value;

 struct DLink * next;

 struct DLink * prev;
};

struct cirListDeque {

 int size;
 struct DLink *back;
};

      

Here's my approach for reversing a deque:

void reverseCirListDeque(struct cirListDeque* q) {

struct DLink* current;

struct DLink* temp;



temp = q->back->next;

q->back->next = q->back->prev;

q->back->prev = temp;



current = q->back->next;

while(current != q->back) {

    temp = current->next;

    current->next = current->prev;

    current->prev = temp;



    current = current->next;

}

}

      

However, when I run it and put the values ​​1, 2 and 3 in it (TYPE is just an alias for int in this case) and reverse it, I get 2, 1, 3. Does anyone have any ideas as to whether what could I be wrong?

Thanks in advance.

+2


a source to share


4 answers


current = q->back->next;

while(current != q->back->next)
{
    /* This code will never run, cause you guaranteed right before the while
     * that current == q->back->next .
     */
}

      



Update. What you need to do now, once you've reversed all the pointers (which seem to work now, judging by your results), set your "back" pointer to back-> prev.

+1


a source


Whenever you are working with abstract data types - lists, queues, comments, etc., whenever pointers are used, it really helps to bring your data structure and its pointers into a chart on paper. Label everything. Then the code is what you see. This really makes the process easier. I haven't used deques since college, but make sure you don't confuse prev, next and back as this could be a problem. Also, be sure to check for null pointers before dereferencing them.



Hope this helps without a direct answer. Your professor can appreciate this .; -)

+2


a source


Not a direct answer to your problem, but back in school I found the Data Debugger to be invaluable for debugging problems like this.

+1


a source


The easiest and fastest way to do this is just to change your interpretation of the direction of the queue.

The direction will be stored in cirListDeque and your movement from node to node will be done knowing the current direction of the queue.

0


a source







All Articles