For the following code, the author only disclosed lines 3 to 28.
NumberList.cpp
#include "NumberList.h"
void NumberList::appendNode(double num)
{
ListNode *newNode; // To allocate & point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL; //ERROR:: Identifer "NULL" is undefined
// If there are no nodes in the list make newNode the 1st 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;
}
}
NumberList.h
// 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: notice the constructor initializes the head pointer to NULL
// This establishes an empty linked list.
NumberList()
{ head = NULL; }
// Destructor: destroys the list by deleting all its nodes.
~NumberList();
// Linked list operations
// These functions are defined in NumberList.cpp.
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;
};
#endif
Pr17-01.cpp
// This program demonstrates a simple append
// operation on a linked list .
#include <iostream>
#include "NumberList.h"
using namespace std;
int main()
{
// Define a NumberList object.
NumberList list;
// Append some values to the list.
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
system("pause");
return 0 ;
}
This post has been edited by r.stiltskin: 06 March 2012 - 08:40 PM
Reason for edit:: Fixed the code tags.

New Topic/Question
Reply



MultiQuote







|