Okay. I've posted the related classes below. Thanks again!
Graph.cpp
Code:
//================================================
// Main method
//================================================
int main()
{
AdjacencyList list;
list.add(new AdjacencyListNode(2));
list.display();
list.add(new AdjacencyListNode(3));
list.display();
list.add(new AdjacencyListNode(4));
list.display();
cout << list.length << endl;
return 0;
}
AdjacencyList.h
Code:
#pragma once
#include "AdjacencyListNode.h"
#ifndef ADJLIST_DEF
#define ADJLIST_DEF
class AdjacencyList
{
public:
int length; // Size of the list
AdjacencyListNode * first; // Pointer to the front of the list
AdjacencyList(); // Constructor
void add(AdjacencyListNode *); // Adds a new node to the front of list
void display(); // Displays the nodes of the list
};
#endif
AdjacencyList.cpp
Code:
#include "StdAfx.h"
#include "AdjacencyList.h"
#include "AdjacencyListNode.h"
#include <iostream>
using namespace std;
//============================================
// Default Constructor for an AdjacencyList
//
// PRE: AdjacencyList is undefined
// POST: AdjacencyList is defined with first
// set to null and length zero.
//============================================
AdjacencyList::AdjacencyList()
{
first = NULL;
length = 0;
}
//=============================================
// Prints the contents of the list.
//
// PRE: List is defined.
// POST: Contents of the list are printed.
//=============================================
void AdjacencyList::display()
{
//Temproary node to iterate through the list with
AdjacencyListNode * tmp;
tmp = first;
cout << "Displaying List: " << endl;
first->display();
// Iterate through list and display each node
while (tmp->next != NULL)
{
tmp->display();
tmp = tmp->next;
}
}
//============================================
// Adds a new node to the beginning of
// the list. Parameter node is the pointer to
// the node that will be added to the list.
//
// PRE: AdjacencyList with size n is defined
// POST: AdjacencyList with node added to the
// front of the list. Length is n + 1.
//============================================
void AdjacencyList::add(AdjacencyListNode * node)
{
// Check if list is empty
if (first == NULL)
first = node;
else //List not empty
{
//Hook up new node's next
node->next = first;
// Set first to new node
first = node;
}
//Increment length
length++;
}
AdjacencyListNode.h
Code:
#pragma once
#ifndef ADJNODE_DEF
#define ADJNODE_DEF
class AdjacencyListNode
{
public:
AdjacencyListNode(); // Creates a new empty node
AdjacencyListNode(int); // Creates node with int as data
AdjacencyListNode * next; // Pointer to next item in list
void display(); // Prints the node's data
int data; // Number to identify the node in the
// graph
};
#endif
AdjacencyListNode.cpp
Code:
#include "StdAfx.h"
#include "AdjacencyListNode.h"
#include <iostream>
using namespace std;
//===========================================
// Default constructor for AdjacencyListNode
//
// PRE: Node is undefined.
// POST: The node is defined with data set
// to -1 and next set to null.
//===========================================
AdjacencyListNode::AdjacencyListNode()
{
data = -1;
next = NULL;
}
//=============================================
// Alternate Constructor for AdjacencyListNode.
// Parameter num is the value for the data to
// be set to.
//
// PRE: Node is undefined.
// POST: Node is defined with data = num and
// next set to null.
//=============================================
AdjacencyListNode::AdjacencyListNode(int num)
{
data = num;
next = NULL;
}
//===================================
// Displays the node's data
//
// PRE: Node is defined.
// POST: Node's data is printed.
//===================================
void AdjacencyListNode::display()
{
cout << "Node Data: " << data << endl;
}