0

I am writing a reverse function for my linked list, but I can't figure out how to point head to the new first number in the list. Declaring the head a ListNode* in main and passing it by reference will not work because I have a bunch of other functions I do not want to modify. Is there any other way of doing this? Here is my header file:

// Specification file for the NumberList class
#ifndef NUMBERLIST_H
#define NUMBERLIST_H

class NumberList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      double value;           // The value in this node
      struct ListNode *next;  // To point to the next node
   };

   ListNode *head;            // List head pointer

public:
   // Constructor
   NumberList()
      { head = nullptr; }

   // Destructor
   ~NumberList();

   // Linked list operations
   void appendNode(double);
   void insertNode(double);
   void deleteNode(double);
   int searchList(double);
   void reverseList() const;
   void displayList() const;
};
#endif

Here is the reverse function. Putting head = previousNode will not work:

void NumberList::reverseList() const
{
    ListNode *currentNode;              // points to current node
    ListNode *previousNode = nullptr;   // stores address of previous node
    ListNode *nextNode = nullptr;       // stores the address of next node
    ListNode *tempNode = nullptr;       // 

    currentNode = head;

    while (currentNode != nullptr)
    {
        cout << "current value: " << currentNode->value << endl;
        nextNode = currentNode->next;
        currentNode->next = previousNode;
        previousNode = currentNode;
        currentNode = nextNode;
    }
    //head = previousNode;
}

*edit: added in main()

#include <iostream>
#include <fstream>
using namespace std;
#include "NumberList.h"

int main()
{
    NumberList nList;
    ifstream inFile;
    double number;
    double searchedNum;

    inFile.open("input.txt");

    if (!inFile)
    {
        cout << "File could not be opened!" << endl;
    }
    else
    {
        while (inFile >> number)
        {
            nList.appendNode(number);
        }

        cout << "The list after file reading:" << endl;
        nList.displayList();

        cout << "What number would you like to search for? ";
        cin >> searchedNum;
        cout << "Position: " << nList.searchList(searchedNum) << endl;

        cout << "The list after reversing:" << endl;
        nList.reverseList();
        nList.displayList();
    }



    return 0;
}
1
  • You create first object in main(), then a pointer at it (head) also in main(). It doesn't matter how many new objects you create (linked list), head will stay head. Then, you send a copy of the head every time you want to call a function, or send it by reference BUT take a copy of it within that function BEFORE doing anything. Now, does your linked list look like a cycle? (last node points to first node) Commented Apr 1, 2016 at 16:24

2 Answers 2

1

Here's an iterative version (I marked changes with // Added *):

void NumberList::reverseList()
{
    ListNode *currentNode;              // points to current node
    ListNode *previousNode = nullptr;   // stores address of previous node
    ListNode *nextNode = nullptr;       // stores the address of next node

    currentNode = head;
    ListNode* temp; // Added*
    while (currentNode != nullptr)
    {
        temp = currentNode->next; // Added *
        nextNode = currentNode->next;
        currentNode->next = previousNode;
        previousNode = currentNode;
        currentNode = temp; // Added *
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I think it was the const preventing me from assigning previousNode to head.
0
ListNode * reverse_list(ListNode * list, ListNode * new_head = nullptr)
{
    if (list == nullptr) return new_head;

    ListNode * tmp = list->next;
    list->next = new_head;

    return reverse_list(tmp, list);
}

head = reverse_list(head);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.