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 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#ifndef LISTNODE_H
#define LISTNODE_H
#include <cstdlib>
using namespace std;
template<typename NODETYPE> class LinkedList;
template<typename NODETYPE>
class ListNode {
friend class LinkedList<NODETYPE>;
public:
ListNode(const NODETYPE &, const NODETYPE&, const NODETYPE&);
ListNode(const NODETYPE &, const NODETYPE &, const NODETYPE &, ListNode<NODETYPE> *);
NODETYPE getName();
NODETYPE getDesc();
NODETYPE getDate();
void setName(NODETYPE &);
void setDesc(NODETYPE &);
void setDate(NODETYPE &);
ListNode getNextNode() ;
void setNextNode(ListNode <NODETYPE>*);
private:
NODETYPE name, desc, date;
ListNode<NODETYPE> *nextNode;
};
template<typename NODETYPE>
ListNode <NODETYPE> :: ListNode(const NODETYPE &newData1, const NODETYPE &newData2,
const NODETYPE &newData3/*, NODETYPE &ListNode<NODETYPE> *nextPtr*/)
{
nextNode = NULL;
name = newData1;
desc = newData2;
date = newData3;
}
template <typename NODETYPE>
ListNode <NODETYPE>:: ListNode (const NODETYPE &newData1, const NODETYPE &newData2,
const NODETYPE &newData3, ListNode<NODETYPE> *newNextPtr) {
name = newData1;
desc = newData2;
date = newData3;
nextNode = newNextPtr;
}
template <typename NODETYPE>
NODETYPE ListNode<NODETYPE>::getName() {
// This function returns the value of data
return name;
}
template <typename NODETYPE>
NODETYPE ListNode<NODETYPE>::getDesc() {
// This function returns the value of data
return desc;
}
template <typename NODETYPE>
NODETYPE ListNode<NODETYPE>::getDate() {
// This function returns the value of data
return date;
}
template <typename NODETYPE>
void ListNode <NODETYPE>:: setName( NODETYPE& newData) {
// This function sets data to the value of newData
name = newData;
}
template <typename NODETYPE>
void ListNode <NODETYPE>:: setDesc( NODETYPE& newData) {
// This function sets data to the value of newData
desc = newData;
}
template <typename NODETYPE>
void ListNode <NODETYPE>:: setDate( NODETYPE& newData) {
// This function sets data to the value of newData
date = newData;
}
template <typename NODETYPE>
ListNode<NODETYPE> ListNode<NODETYPE>:: getNextNode() {
// This function returns the nextNode pointer
return nextNode;
}
template <typename NODETYPE>
void ListNode<NODETYPE>:: setNextNode(ListNode<NODETYPE> *newNextPtr) {
// This function sets nextNode pointer to newNextPtr
nextNode = newNextPtr;
}
#endif
NEXT .H FILE: LINKEDLIST.H
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "ListNode.h"
#include <string>
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
template <typename NODETYPE>
class LinkedList {
public:
LinkedList();
~LinkedList();
int getSize();
bool isEmpty();
//static const int SIZE = 60;
bool readDoc();
void setNextLine(LinkedList* next);
void printList() const;
void addLine(NODETYPE& name,NODETYPE& desc,NODETYPE& date);
void menu();
void deleteLine();
protected:
int size;
ListNode<NODETYPE> *firstNode;
ListNode<NODETYPE> *getNewName(const NODETYPE &);
ListNode<NODETYPE> *getNewDesc(const NODETYPE &);
ListNode<NODETYPE> *getNewDate(const NODETYPE &);
};
//Constructor with Paramaters
template <typename NODETYPE>
LinkedList <NODETYPE> :: LinkedList() {
firstNode = NULL;
size = 0;
}
template <typename NODETYPE>
int LinkedList<NODETYPE>:: getSize() {
return size;
}
template <typename NODETYPE>
LinkedList <NODETYPE>:: ~LinkedList() {
}
template <typename NODETYPE>
bool LinkedList<NODETYPE> :: readDoc() {
ListNode<NODETYPE> *nextPtr = firstNode -> nextNode;
// Open input file
ifstream readFile("list.txt");
string tempName, tempDesc, tempDate;
string tempRead;
//firsitNode->;
if (!readFile)
{
cout << "I am sorry but we could not process"<<
"the file information."<<endl;
}
while (readFile != NULL){
readFile >> tempRead;
for (int i =0; i<size; i++){
firstNode = new ListNode<string>(tempName, tempDesc,
tempDate, firstNode);
} //end for
} //end while
return true;
}//end function
template <typename NODETYPE>
void LinkedList<NODETYPE> :: addLine(NODETYPE & name,NODETYPE & desc,NODETYPE & date){
readDoc();
ListNode<NODETYPE> *NewNode = new ListNode<NODETYPE>(getNewName(name), getNewDesc(desc),getNewDate(date));
if(!firstNode) {
firstNode = NewNode;
}
else {
NewNode -> nextNode = firstNode;
firstNode = NewNode;
}
size++;
}
template <typename NODETYPE>
void LinkedList<NODETYPE> :: deleteLine() {
// Reads text document
readDoc();
// creates a new pointer
ListNode<NODETYPE> *currentPtr = firstNode;
// new variables to use for user input
char descriptionInput[40], option;
int choice;
cout << "Enter Full Description of Assignment: ";
scanf("%s", &descriptionInput);
getchar ();
while (firstNode != NULL)
{
// Prints off Assignment that is similar to the entered description from user
printf("Class Name: %s\n", currentPtr->name);
printf("Descrption: %s\n", currentPtr->desc);
printf("Date: %s\n", currentPtr->date);
printf("Is the listed Assignment above what you wanted ? <Y/N>\n");
// Takes in option chosen by user
scanf("%c", &option);
getchar ();
if(option == 'y' || option == 'Y')
choice = 1;
// Looks for node and deletes
if(choice == 1){
ListNode<NODETYPE> *tempPtr = currentPtr;
currentPtr = currentPtr -> nextNode;
delete tempPtr;
size --;
}
//delete tempPtr;
//size --;
//}//if end
}//end while
}
template <typename NODETYPE>
void LinkedList<NODETYPE>:: printList() const
{
ListNode<NODETYPE> *currentPtr = firstNode;
// prefer while loop due to changing size
// cout << name << ", "<< desc << ", " << date;
while (currentPtr != NULL){
cout << currentPtr -> name;
cout << currentPtr -> desc;
cout << currentPtr -> date;
currentPtr = currentPtr -> nextNode;
}
cout <<endl;
}
template <typename NODETYPE>
void LinkedList<NODETYPE> :: menu() {
cout << endl;
cout <<"Accept from the following options to proceed."<<endl;
cout << "1) Add Assignment" <<endl;
cout << "2) Remove Assignment" <<endl;
cout << "3) Print Listing" <<endl;
cout << "4) Quit" <<endl;
}
#endif
MAIN FILE : MAIN.CPP
#include <iostream>
#include "LinkedList.h"
#include "ListNode.h"
#include <cstdlib>
using namespace std;
int main() {
LinkedList<string> *objrun = new LinkedList<string>();
int options;
objrun -> menu();
.... //Add
objrun -> addLine(pnum, title ,date);
}
// Remove Assignments
if (options == 2){
objrun -> deleteLine();
}
//print the listing
if (options == 3){
objrun -> printList();
}
// Add Assignments
if (options == 1){
char date[20];
char pnum[30];
char title[256];
cout <<"Enter: Project <space> Project Number "<<endl;
//cin.getline(pnum, 20, '\n');
//cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin >> pnum;
cout<<"Enter the Project's title next: "<<endl;
//cin.getline(title, 256, '\n');
//cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin >> title;
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
//cin.getline(date, 256, '\n');
//cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin >> date;
// add the inputed variables into the addLine function
objrun -> addLine(pnum, title ,date);
}
// Remove Assignments
if (options == 2){
objrun -> deleteLine();
}
objrun -> menu();
cin >>options;
cout <<endl;
}//end while
//readFile.close();
}
|