Ok, so I am befuddled. Each individual file compile fine, no errors. When I compile all together and create an executable, no errors. But, when I run the program, I am getting an error. I have it narrowed down to a problem with my void LinkedList::insert(const ElementType &dataValue)
but I have looked at it too many time to figure out what's wrong. The main concept is that a new node will be inserted in a sorted order based on the key of a struct. The linked list is sorted alphabetically. Also attached is a sample of the data being read in.
/*~~~linkedlist.h~~~~*/
#include <iostream>
#include <string>
using namespace std;
typedef struct Articles ElementType;
typedef std::string KeyType;
struct Articles
{
string key;
string name;
string title;
};
class LinkedList
{
private:
class Node
{
public:
ElementType data;
Node * next;
Node(ElementType dataValue){
data = dataValue;
next = NULL;
}
};
typedef Node * NodePointer;
Node *find (const KeyType &key);
NodePointer first;
int mySize;
int index;
public:
LinkedList();
void insert (const ElementType &item);
bool find (const KeyType &key, ElementType &item);
bool erase (const KeyType &key);
};
bool operator==(const ElementType &e1, const KeyType &key);
bool operator>=(const ElementType &e1, const KeyType &key);
/*~~~linkedlist.cpp~~~~*/
#include <iostream>
using namespace std;
#include "linkedlist.h"
LinkedList::LinkedList()
{
first = NULL;
mySize = 0;
}
void LinkedList::insert(const ElementType &dataValue)
{
NodePointer nPtr = new Node(dataValue);
NodePointer predPtr = first;
if (first->next == NULL)
{
nPtr->next = first;
first = nPtr;
}
else
{
while ((dataValue.key) >= predPtr->data.key)
{
predPtr=predPtr->next;
}
if ((dataValue.key) == predPtr->next->data.key)
{
nPtr->next = predPtr->next;
predPtr->next = nPtr;
}
}
}
bool LinkedList::find(const KeyType &key, ElementType &item)
{
}
bool LinkedList::erase(const KeyType &key)
{
}
bool operator==(const ElementType &e1, const KeyType &key)
{
if (e1.key == key)
return true;
else
return false;
}
bool operator>=(const ElementType &e1, const KeyType &key)
{
if (e1.key >= key)
return true;
else
return false;
}
/*~~~program2.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| A driver file which plays a game of Tic Tac Toe with two |
| players. The program incorporates 2 classes (and header |
| files for each): Tictactoe and Player. Tictactoe handles |
| the displayBoard(), setValue(), and getStatus() functions. |
| The Player class handles getName(), getIndex(), and |
| getMove() functions. Definition of the functions are in |
| their corresponding .cpp/.h files. |
| Game play consists of receiving a player's name then |
| determining which player goes first. The players, starting |
| with the first player, will be prompted to enter a position |
| which will be checked for whether: the space is out of the |
| scope and whether the space is already occupied, and if |
| neither is the case, the spot will be marked with the player's |
| mark (X or O). Then the program will check if there is a win, |
| a tie, or other(results in continuing game play). A win can |
| be from either player and is checked after each player's "turn." |
| The game board is displayed at the beginning (with no X's or O's) |
| and after each player's turn. |
| |
| Author: Megan MacDonald |
| Date Completed: ---, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "linkedlist.h"
int main()
{
LinkedList l;
ifstream inFile("cacmpubshort.txt");
Articles s;
if (inFile.is_open())
{
while (!inFile.eof())
{
getline (inFile,s.key);
getline (inFile,s.name);
getline (inFile,s.title);
cout << s.key << " " << s.name << " " << s.title << endl;
l.insert(s);
}
inFile.close();
}
else cout << "Unable to open file";
return 0;
}
~~~~cacmpubshort.txt~~~~~
Crawford:2000:EPa
Diane Crawford
Editorial pointers
Fox:2000:NTCa
Robert Fox
News Track: Celestial backbone; Foreign hands off China's net; Women and the web; DOE says no nuke access; Worst net nightmare; Sidebar: Ethics quiz
Crawford:2000:FTO
Diane Crawford and Brian K. E. Balke and Steve Wilstrom and Jef Raskin and Pat McGee
Forum: Tackling OO Design Principles; Educational Concerns; Building LEGO Robots
Meeks:2000:EFBa
Brock N. Meeks
Electronic frontier: bugging out: Y2K fallout or business as usual?
Soloway:2000:LEK
Elliot Soloway and Cathleen Norris and Phyllis Blumenfeld and Barry Fishman and Joseph Krajcik and Ronald Marx
Log on Education: K--12 and the Internet
Porter:2000:SCL
Tom Porter and Galyn Susman
On site: creating lifelike characters in Pixar movies
Simons:2000:PBB
Barbara Simons
From the President: building Big Brother
Konana:2000:IOI
Prabhudev Konana and Nirup M. Menon and Sridhar Balasubramanian
The implications of online investing
Gorriz:2000:EGC
Cecilia M. Gorriz and Claudia Medina
Engaging girls with computers through software games
Hargittai:2000:RLI
Eszter Hargittai
Radio's lessons for the Internet