Reversing the order of nodes in a doubly linked list (Solution provided)

In summary, the conversation discusses the task of writing an algorithm for reversing the order of nodes in a doubly linked list, assuming a head and tail reference and knowledge of how a linked list works. The conversation also addresses potential flaws in a given code and the differences between a head and header, as well as a tail and trailer in a linked list.
  • #1
s3a
818
8

Homework Statement


Write an algorithm for reversing the order of nodes in a doubly linked list. You may assume a head and tail reference, and you may directly get and set the next field of any node, but you may not assume any add or remove methods are defined. You may assume a header or trailer rather than head or tail, if you wish.

Homework Equations


How a (Doubly) Linked List works.


The Attempt at a Solution


The teacher gave this code:
Code:
public void reverse() {

        DNode cur = header.getNext();
        DNode tmp;

        while (cur != trailer) {
            //  swap the next and prev referneces
            tmp = cur.getNext();
            cur.setNext(cur.getPrev());
            cur.setPrev(tmp);
            //  advance to the next node in the sequence
            cur = tmp;
        }

        //   swap the references of header.next and trailer.prev
        header.setNext(trailer.getPrev());
        trailer.setPrev(header.getNext());
        tmp = header;
        trailer = header;
        header = trailer;
    }

[U]However, I believe it has flaws, so here is my version, please tell me if mine is 100% correct or not[/U]:
void reverse() {
        DNode cur = header.getNext();
        DNode tmp;

        while (cur != trailer) {
            tmp = cur.getNext();
            cur.setNext(cur.getPrev());
            cur.setPrev(tmp);

            cur = tmp;
        }

        tmp = trailer.getPrev();
        trailer.setPrev(header.getNext());
        header.setNext(tmp);

        tmp = trailer;
        trailer = header;
        header = tmp;
    }

Other question:
Also, what's the difference between a head and a header as well as a between a tail and a trailer? Or, are they the exact same thing?

Any input would be GREATLY appreciated!
Thanks in advance!
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
s3a said:
I believe it has flaws ...
looks like you solved the issue of using header.getNext() after having just modified it with header.setNext().

s3a said:
Also, what's the difference between a head and a header as well as a between a tail and a trailer?
I did a web search for link list header, and from this web site:

http://pages.cs.wisc.edu/~vernon/cs367/notes/4.LINKED-LIST.html

a header and a trailer are dummy nodes. An "empty" list would include two nodes, a dummy header node and a dummy trailer node. Considering the example code does not check for NULL after the statement DNode cur = header.getNext(), it's apparently using header and trailer dummy nodes.

head and tail are pointers to the first and last nodes of a list. head and tail pointers would normally be part of a list class as opposed to a node class. A list class could include other information such as the number of nodes on the list (for standard template library, this is list.size()). If a list is empty, then head == tail == NULL. If a list has one node, then head == tail == ptr to node, and node.prev() == node.next() == NULL. These conditions require special handling that having a header node and trailer node somewhat eliminate.
 
Last edited:
  • #3
Thank you very much. :)
 

Related to Reversing the order of nodes in a doubly linked list (Solution provided)

What is a doubly linked list?

A doubly linked list is a type of data structure that consists of a sequence of nodes. Each node contains a value and pointers to the previous and next nodes in the list. This allows for efficient insertion and deletion of nodes at any position in the list.

Why would I need to reverse the order of nodes in a doubly linked list?

Reversing the order of nodes in a doubly linked list can be useful in certain situations, such as when you need to traverse the list in reverse order or when you want to optimize certain operations on the list.

How do I reverse the order of nodes in a doubly linked list?

The process of reversing a doubly linked list involves changing the pointers of each node to point to the previous node instead of the next node. This can be done iteratively or recursively, depending on your preference and the complexity of the list.

Is there a time or space complexity associated with reversing a doubly linked list?

The time complexity of reversing a doubly linked list is O(n), where n is the number of nodes in the list. The space complexity is also O(n) as the algorithm requires creating new pointers for each node in the list.

Are there any edge cases to consider when reversing the order of nodes in a doubly linked list?

Yes, there are a few edge cases to consider, such as when the list is empty or has only one node. These cases should be handled separately to ensure the correct reversal of the list.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
17
Views
7K
Back
Top