1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
using namespace std;
/*------------------------------------ Class nameList ----------------------------------------------------------------*/
class nameList
{
private:
struct nameListNode // Declares a structure for the list
{
string firstN;
string lastN;
int ages;
int value;
nameListNode *next;
};
nameListNode *head; // Lists the head pointer
public:
int num;
nameList(void) // Constructor
{ head = NULL; }
~nameList(void); // Destructor
void addName(string, string, int); // Add name
void displayList(); // Display list
void insertNode(string, string, int); // Insert nodes
bool deleteNode(string, string, int); // Delete nodes
};
/*------------------------------------------- Vector/Tokenizer ----------------------------------------------------------------*/
vector<string> inline StringSplit(const string &source, const char *delimiter = " ", bool keepEmpty = false)
{
vector <string> results; // Creates vector of strings named results
size_t prev =0;
size_t next=0;
while (( next = source.find_first_of(delimiter, prev)) != string::npos) // Finds null position, or empty spaces
{
if (keepEmpty || (next - prev !=0))
{ results.push_back(source.substr(prev, next-prev));} // Finds empty character and skips it
prev = next +1;
}
if(prev < source.size())
{results.push_back(source.substr(prev));}
return results;
}
struct person
{
string lastName;
string firstName;
int age;
};
/*------------------------------------------ Input Commands from Command Line ---------------------------------------*/
int main(int argc, char *argv[])
{
if(argc != 3) // Usage statement of how to use program via command line
{
cout << "\nUsage: " << argv[0] << " 'Input File Name 1'" << " 'Input File Name 2'\n" << endl;
return(1);
}
ifstream input_File(argv[1], ios::in); // Open first input file to read from
{
cout << "Error: Unable to open input file 1\n" << endl;
return(1);
}
ifstream delete_File(argv[2], ios::in); // Open second input file to read from
{
cout << "Error: Unable to open input file 2\n" << endl;
return(1);
}
/*--------------------------------------------- Main Function ---------------------------------------------------------------------*/
string inputOne;
string inputTwo;
int inputThree;
vector <string> nameVector; // Vector of strings called nameVector
string line;
typedef class nameList *name;
if (input_File.is_open()) // If input_File opens
{
cout << argv[1] << " is open, getting lines." << endl;
getline(input_File, inputOne); // Getlines from input_File
while (input_File.good())
{
if (!line.length())
{
getline(input_File, line);
continue;
}
nameVector = StringSplit(line);
if (nameVector.size()!=3)
{
cout << line;
getline(input_File, line);
continue;
}
inputOne = nameVector[0]; // Fills vector
inputTwo = nameVector[1];
inputThree = atoi(nameVector[2].c_str());
getline(input_File, line);
}
}
else
{
cout << "Error: input_File did not open!" << endl; // If file does not open
return (1);
}
/*-------------------------------------------------- Output List after inserts ----------------------------------------------*/
cout << "List after insertions: " << endl << endl;
cout << &nameList::displayList;
cout << endl;
/*-------------------------------------------------- Delete Names ----------------------------------------------------------*/
getline(delete_File, line);
while(delete_File.good())
{
if(!line.length())
{
getline(delete_File, line);
continue;
}
nameVector = StringSplit(line); // Tokenizing
if(nameVector.size()!=3)
{
cout << line << endl;
getline(delete_File, line);
continue;
}
inputOne = nameVector[0]; // Adds Names and Ages from Vector
inputTwo = nameVector[1];
inputThree = atoi(nameVector[2].c_str()); // ASCII to Integer conversion
if (!deleteNode(&inputOne, &inputTwo, &inputThree)) // Why is there an error here? *************** Error: identifier "deleteNode" is undefined **
{
cout << "Error: Could not delete node" << endl;
}
getline(input_File, line);
}
cout << "List after deletions: " << endl; // Displays list after making deletions
cout << &nameList::displayList;
cout << endl;
input_File.close(); // Closing files
delete_File.close();
return 0;
}
/*--------------------------------------------- Adding Nodes to List (void) -----------------------------------------*/
void nameList::addName(string lastName, string firstName, int age) // adds the node to the end of a list
{
nameListNode *newNode, *nodePtr; //Allocates a new node & stores num
newNode = new nameListNode;
newNode-> lastN = lastName;
newNode-> firstN = firstName;
newNode-> ages = age;
newNode-> next = NULL;
if (!head) // If there are no nodes in the list makes newNode the first node
{
head = newNode;
newNode-> next = NULL;
}
else
{
nodePtr = head; // Initializes nodePtr to head of list
while (nodePtr->next) // Finds the last node in the list
{
nodePtr = nodePtr-> next;
nodePtr-> next = newNode; // Inserts newNode as the last node
}
}
}
/*------------------------------------------ Display List (void) -----------------------------------------------------*/
void nameList::displayList(void) // Displays list node by node
{
nameListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr-> lastN << endl;
cout << nodePtr-> firstN << endl;
cout << nodePtr-> ages << endl;
nodePtr = nodePtr-> next;
}
}
nameList::~nameList(void) // Destructor
{
nameListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr-> next;
delete nodePtr;
nodePtr = nextNode;
}
}
/*------------------------------------------- Adding Names to List (void) ------------------------------------------*/
void nameList::insertNode(string lastName, string firstName, int age)
{
nameListNode *newNode, *nodePtr, *previousNode;
newNode = new nameListNode; // Allocate a new node & store your data
newNode-> lastN = lastName;
newNode-> firstN = firstName;
newNode-> ages = age;
if (!head) // If there are no nodes in the list, make newNode the first node
{
head = newNode;
newNode-> next = NULL;
}
else
{
nodePtr = head;// Initialize nodePtr to head of list
while (nodePtr != NULL && nodePtr-> value < num) // Skip all nodes whose value is less than data
{
previousNode = nodePtr;
nodePtr = nodePtr-> next;
}
if (previousNode == NULL) // Moving nodePtr
{
head = newNode;
newNode-> next = nodePtr;
}
else
{
previousNode-> next = newNode;
newNode-> next = nodePtr;
}
}
}
/*---------------------------------------------- Delete List (bool) -------------------------------------------------*/
bool nameList::deleteNode(string lastName, string firstName, int age)
{
nameListNode *nodePtr, *previousNode;
if (!head) // If the list is empty, return
return false;
if (head->firstN == firstName) // Determine if the first node is the
{
nodePtr = head-> next;
delete head;
head = nodePtr;
return true;
}
else
{
nodePtr = head; // Initialize nodePtr to head of list
while (nodePtr != NULL && nodePtr-> value != num) // Skip all nodes whose value member is not equal to num.
{
previousNode = nodePtr;
nodePtr = nodePtr-> next;
}
previousNode-> next = nodePtr-> next; // Link the previous node to the node afternodePtr, then delete nodePtr.
delete nodePtr;
return true;
}
return false;
}
/*-------------------------------------------------------------------------------------------------------------*/
|