I have some code that implements a linked list. The program has the basic implmenetation including:
• IntList() // Constructor
• ~IntList(); // Destructor
• void appendNode(int);
• void insertNode(int);
• void deleteNode(int);
• void displayList();
• void reverseList();


I am trying to get the reverList to work correctly. Below is my code. Can anyone please let me know where I am going wrong?

IntList.h

// Specification file for the IntList class
#ifndef INTLIST_H
#define INTLIST_H

class IntList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      int value;
      struct ListNode *next;
   }; 

   ListNode *head;   // List head pointer

public:
   IntList()   // Constructor
      { head = NULL; }
   ~IntList(); // Destructor
   void appendNode(int);
   void insertNode(int);
   void deleteNode(int);
   void displayList();
   void reverseList();
};

#endif

IntList.cpp

// Implementation file for the IntList class
#include <iostream>  // For cout  and NULL
#include "IntList.h"
using namespace std;

//**************************************************
// appendNode appends a node containing the        *
// value pased into num, to the end of the list.   *
//**************************************************

void IntList::appendNode(int num)
{
   ListNode *newNode, *nodePtr;

   // Allocate a new node & store num
   newNode = new ListNode;
   newNode->value = num;
   newNode->next = NULL;

   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
      head = newNode;
   else  // Otherwise, insert newNode at end
   {
      // Initialize nodePtr to head of list
      nodePtr = head;

      // Find the last node in the list
      while (nodePtr->next)
         nodePtr = nodePtr->next;

      // Insert newNode as the last node
      nodePtr->next = newNode;
   }
}

//**************************************************
// displayList shows the value                     *
// stored in each node of the linked list          *
// pointed to by head.                             *
//**************************************************

void IntList::displayList()
{
   ListNode *nodePtr;

   nodePtr = head;
   while (nodePtr)
   {
      cout << nodePtr->value << endl;
      nodePtr = nodePtr->next;
   }
}

//**************************************************
// reverseList shows the nodes in reverse order    *
//**************************************************

void IntList::reverseList()
{
   ListNode *nodePtr;

   nodePtr = head;
   while (nodePtr)
  {
      int i;
      for (i = nodePtr -1; i < 0; --i)
      cout << nodePtr->value[i] << endl;
      nodePtr = nodePtr->next;
   }
   
}

//**************************************************
// The insertNode function inserts a node with     *
// num copied to its value member.                 *
//**************************************************

void IntList::insertNode(int num)
{
   ListNode *newNode, *nodePtr, *previousNode = NULL;

   // Allocate a new node & store num
   newNode = new ListNode;
   newNode->value = num;
   
   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
   {
      head = newNode;
      newNode->next = NULL;
   }
   else  // Otherwise, insert newNode
   {
      // Initialize nodePtr to head of list and
      // previousNode to NULL.
      nodePtr = head;
      previousNode = NULL;

      // Skip all nodes whose value member is less
      // than num.
      while (nodePtr != NULL && nodePtr->value < num)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If the new node is to be the 1st in the list,
      // insert it before all other nodes.
      if (previousNode == NULL)
      {
         head = newNode;
         newNode->next = nodePtr;
      }
      else  // Otherwise, insert it after the prev node
      {
         previousNode->next = newNode;
         newNode->next = nodePtr;
      }
   }
}

//**************************************************
// The deleteNode function searches for a node     *
// with num as its value. The node, if found, is   *
// deleted from the list and from memory.          *
//**************************************************

void IntList::deleteNode(int num)
{
   ListNode *nodePtr, *previousNode;

   // If the list is empty, do nothing.
   if (!head)
      return;
   
   // Determine if the first node is the one.
   if (head->value == num)
   {
      nodePtr = head->next;
      delete head;
      head = nodePtr;
   }
   else
   {
      // Initialize nodePtr to head of list
      nodePtr = head;

      // Skip all nodes whose value member is 
      // not equal to num.
      while (nodePtr != NULL && nodePtr->value != num)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If nodePtr is not at the end of the list, 
      // link the previous node to the node after
      // nodePtr, then delete nodePtr.
      if (nodePtr)
      {
         previousNode->next = nodePtr->next;
         delete nodePtr;
      }
   }
}

//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

IntList::~IntList()
{
   ListNode *nodePtr, *nextNode;

   nodePtr = head;
   while (nodePtr != NULL)
   {
      nextNode = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNode;
   }
}

labLinkedList.cpp

// Chapter 17, Programming Challenge 1
// Your Own Linked List
#include <iostream>
#include "IntList.h"
using namespace std;

int main()
{
   // Create an instance of IntList
   IntList list;

   // Build the list
   list.appendNode(2);  // Append 2 to the list
   list.appendNode(4);  // Append 4 to the list
   list.appendNode(6);  // Append 6 to the list

   // Display the nodes.
   cout << "Here are the initial values:\n";
   list.displayList();
   cout << endl;

   // Insert the value 5 into the list.
   cout << "Now inserting the value 5.\n";
   list.insertNode(5);
   
   // Display the nodes now.
   cout << "Here are the nodes now.\n";
   list.displayList();
   cout << endl;

   // Delete the node holding 6.
   cout << "Now deleting the last node.\n";
   list.deleteNode(6);
   
   // Display the nodes.
   cout << "Here are the nodes left.\n";
   list.displayList();

   //display the nodes inr evers    
   cout << "Reversing the list...\n";
   list.reverseList();

   return 0;
}

when i try to run the program I get the following errors:
Error 1 error C2440: '=' : cannot convert from 'IntList::ListNode *' to 'int' c:\IntList.cpp 68 montest2

Error 2 error C2109: subscript requires array or pointer type c:\IntList.cpp 69 montest2


Any ideas on how to reverse it???

Recommended Answers

All 10 Replies

I dont think the logic used by you is correct . Check out the method shown in this link...... Its far more simpler as compared to your code

http://www.knowledgesutra.com/index.php/Data-Structures-Linked-List-Reverse_t52777.html

After reading through that link I think I am more confused now than I was before. I tried using the code like this:

Void ReverseList(node* head)
{
    node *temp,*current,*result;
    temp=null;
    result=null;
    current=head;
    while(current!=null)
    {
        temp=current->next;
        current->next=result;
        result=current;
        current=temp;
    }
   head=result;

but still having issues. anyone else have any ideas?

This code segment assume that there is a struct called node, which has the data members called next. I dont think your original solution had these elements ... Did you modify the code segment to fit your solution ?

This code segment assume that there is a struct called node, which has the data members called next. I dont think your original solution had these elements ... Did you modify the code segment to fit your solution ?

I tried changing it to my segment. Here is what I coded for the reverseList function:

void IntList::reverseList(ListNode* head)
{
    node * temp, *current, *result;
    temp=null;
    result=null;
    current=head;
    while(current!=null)
    {
        temp=current->next;
        current->next=result;
        result=current;
        current=temp;
    }
    head=result;
}

gives me an error:

Error	1	error C2511: 'void IntList::reverseList(IntList::ListNode *)' : overloaded member function not found in 'IntList'	c:\IntList.cpp	61	test4 mnoday

I THINK i have the correct elements in my struct:

// Specification file for the IntList class
#ifndef INTLIST_H
#define INTLIST_H

class IntList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      int value;
      struct ListNode *next;
   }; 

   ListNode *head;   // List head pointer

public:
   IntList()   // Constructor
      { head = NULL; }
   ~IntList(); // Destructor
   void appendNode(int);
   void insertNode(int);
   void deleteNode(int);
   void displayList();
   void reverseList();



};

#endif

Your structure is inside the class .... So the struct cannot be accessed without the object of the class
Is it necessary for you to have the struct inside the class ? There are 2 simpler options

1. Avoid the concept of class all together. Just use structs
2. Dont use struct inside the class. Make the linked list from objects of the class

I finally got it!

void IntList::reverseList()
{
    ListNode *end = NULL;
    ListNode *node = NULL;

    while(head != end)
    {
        node = head;
        while(node->next != end)
            node = node->next;
        cout << node->value << endl;
        end = node;
    }
}

Thank you everyone as usual!

only 1 more question:
On the insturctions I am to do the following:
• A print member function: This function should display all the values in the linked list. Test the class by starting with an empty list, adding some elements, and then printing the resulting list.

Not sure what is meant here. Should I just empty the list completely and then add all the elements? Anyone have any ideas?

The print function should print the contents of the linked list. If linked list does not contain any elements nothing should be printed. If the list contains only 1 element then only that element should be printed. If the list contains only 2 elements then only those 2 elements should be printed. So on and so forth

got it thanks again

got it thanks again

Mark the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.