I wrote a function that should reverse a list.
So far, I can reverse only two items, but no more. I checked and double checked and still can't find the problem. I even used the Debugger to see the value of each pointer. When running the debugger, I received the message:
An access violation (segmentation fault) raised in your program.
This is my first assignment with linked lists so I am still learning.
Here is the code I wrote in Dev-C++:
List::ListNode *List::Reverse_List(ListNode *head)
{
ListNode *cur = head;
ListNode *forward = NULL;
ListNode *previous = NULL;
while (cur != NULL)
{
head = cur; //set the head to last node
forward = head->next; //save the next pointer in forward
cur->next = previous; //change next to previous
previous = cur;
cur = forward;
cout << "cur= " << cur->item << endl; //this is just to display the current value of cur
return head;
}
}