[I]My Doubly linked list doesnt work when i try to display using backward/reverse traversal...

Will you please help me....[/I]

[IMG]http://www.withfriendship.com/user/images/616/1vijay.jpg[/IMG]

Dani AI

Generated

The symptom reported by — forward traversal fine but reverse traversal failing — usually means the list's prev pointers or the tail/head bookkeeping are not being maintained. 's snark about a null dereference is a useful hint: a NULL where a prev pointer is expected is the most common cause. 's request to post code is also correct: concrete snippets make the bug obvious.

A short checklist to rule out the usual faults:

  • Confirm head is initialized to NULL and nodes are allocated on the heap (not as local stack variables).
  • When inserting, always set both next and prev appropriately (including when inserting at the head or tail).
  • When deleting, update neighbor pointers so there are no stale links.
  • For traversal, either keep a tail pointer or find the tail first by following next until NULL, then follow prev to traverse backward.

A minimal reverse-print pattern in C looks like:

Node *p = head;
if (p) {
  while (p->next) p = p->next;   // move to tail
  while (p) {
    printf("%d\n", p->data);
    p = p->prev;
  }
}

Debugging tips: instrument the list builders to print pointer values and verify the invariant "if node->next != NULL then node->next->prev == node". Add assertions around critical updates. Run the program under Valgrind to catch invalid reads/writes (Valgrind). For a standard implementation to compare against, see the doubly linked list guide (GeeksforGeeks doubly linked list tutorial).

Recommended Answers

All 2 Replies

And don't bother posting any code. We all have psychic powers which allow us to see into your mind and your computer and can tell you where your problem lies. In this case, you're dereferencing a null pointer on line 34.

:icon_wink:

Well, you just have to step forward and post something relevant to the problem(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.