简体   繁体   中英

Reversing a Doubly-Linked List with Values

I am currently unable to get a reverse function of a doubly linked list to properly work for an assignment, I've read up the other threads and searched on google but usually the difference is my problem passes in a constant and that it returns a "dlist". The professor has provided a "code tester" and it says that my code when doing "reverse(reverse(dlist c))" it's not equal to itself being "c". [Reversing it twice does not equal itself].

The dlist class is:

class dlist {
public:
dlist() { }
int sizeOfDlist =0; // To keep track of size 
struct node {
    int value;
    node* next;
    node* prev;
};

node* head() const { return _head; } // _head = beginning of list
node* tail() const { return _tail; } // _tails = end of list
node* _head = nullptr;
node* _tail = nullptr;

And here's the reverse function:

dlist reverse(const dlist& l){
if(l._head == nullptr||l._tail ==nullptr){ // Checks if l list is empty
    dlist newRList;
    return newRList;//return a blank list;
}

if(l.head()!=nullptr){
    dlist::node* temp;
    dlist::node* ptr1 = l._head;
    dlist::node* previous = nullptr;

    while(ptr1 != nullptr){
        temp = ptr1->next;
        ptr1->next = previous;
        previous = ptr1;
        ptr1 = temp;
    }
    dlist newRList;
    newRList._head = previous;
    return newRList;
   }
else //if something passes by, return original list
    return l;
}

Each dlist node has a pointer pointing towards the previous node and a pointer pointing towards the next node. The dlist node also contains an int value.

What I tried to implement was creating a list that starts at original list's "tail" or end. The list would then go backwards and swap the "next" and "prev" pointers as it goes along. What am I doing wrong?

Solved: By using a push_front function which adds a value to the front of a list and pushing everything else behind it, I was able to grab the values from the given constant dlist, and push_front all of the values into "newRList" which reverses the order.

Thanks to user4581301 and Basya Perlman for helping me out, here's the new reverse function:

dlist reverse(const dlist& l){
if(l._head == nullptr||l._tail ==nullptr){ // Checks if l list is empty
    dlist newRList;
    return newRList;//return a blank list;
}

if(l.head()!=nullptr){
   dlist newRList;
   for(int n=0; n<l.size(); n++){ // Size function checks the size of the doubly linked list 

       newRList.push_front(l.valueGetter(n)); // Value Getter is a function that grabs the value at a specific [iteration], push_front pushes said value into the front of the list.
    }
    return newRList;
}
else //if something passes by, return original list 
    return l;
}

Your reverse function looks like it is set up to return a new dlist. It returns an object, not a pointer or a reference.

Also, your parameter is a const dlist, yet you are trying to reverse it in-place, and then point a new pointer to the head of the list and return that. Then the tester is comparing the returned list to the original list; but the original list, which was meant to be const , but which was modified? I am a bit confused, so perhaps the computer running your program is too :-)

From the function definition, it looks as though the idea is to create a new list by copying the elements into the new list in reverse order, and leave the original list unchanged. In your comment, you have a push_back and a push_front function; you can loop forward through your existing list and push_front a copy of each element into the new list, to reverse it (whether you need to explicitly make a copy or not depends on the definition of the push_front function, which I do not have).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM