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???