Could someone please tell me why are pointers head
and tail
different when exiting function reverse
?
struct elem{
int val;
elem* prev;
elem* next;
...
};
void print(elem* head,elem* tail){...}
void insertAtEnd(elem* e,elem* tail){...}
void reverse(elem* head,elem* tail){
elem* headref = head;
elem* temp = head;
while(temp != NULL){
elem* t = temp->prev;
temp->prev = temp->next;
temp->next = t;
temp = temp->prev;
}
head = tail;
tail = headref;
print(head,tail);
}
int main(){
elem* head = new elem();
elem* tail = new elem();
...
print(head,tail);
reverse(head,tail);
print(head,tail);
return 0;
}
print()
inside function reverse
works fine. Next print
(just before return 0
in main
) causes segmentation fault ( head->next
points to NULL
).
With void reverse(elem* head,elem* tail)
, you don't modify pointer (you may modify content).
You probably mean
void reverse(elem*& head, elem*& tail)
to modify head
and tail
.
The pointers in main()
themselves don't change after calling reverse()
, because you pass them to reverse()
by value, and the code in the function only modifies its own copies of the pointers . However, the contents of the elem
objects they point to have been changed by the reverse()
function.
That is, head
in main()
still points to the same elem
object it used to point to before, but now that elem
object is the tail of the list (because you changed its contents in the reverse()
function and now its next
member is nullptr
). Similarly, tail
in main()
points to an elem
that is now the head of the list.
Calling reverse doesn't change head and tail because they are passed by value (reverse only modifies its private copies). If you change the declaration of reverse to
void reverse(elem *&head, elem *&tail)
It should work.
Without analysing your code in detail, When you are reversing you are passing pointers to your elements, so the one called head is now the tail and the one called tail is now the head.
So after you will need to print( tail, head )
Your print
function obviously does not check for null
that is, it will cause an access violation if next
is nullptr before it reaches your tail
. And it will be because head
is the tail so its next
will indeed be nullptr.
If you actually want the reverse
function to change the pointers themselves as well as what they point to, so you get a reversed list with the names head
and tail
swapped, pass them by reference.
Your exchange of pointers inside reverse() is local. It will be discarded once you leave the function scope. To achieve the exchange, yo0u have to use double pointers:
void reverse(elem** head, elem** tail){
elem* headref = *head;
elem* temp = *head;
while(temp != NULL){
elem* t = temp->prev;
temp->prev = temp->next;
temp->next = t;
temp = temp->prev;
}
*head = *tail;
*tail = headref;
print(*head,*tail);
}
And pass addresses of head and tail:
reverse(&head, &tail);
PS Sorry for possible bugs - I didn't test the code
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.