Essentially needed to use a linked list to implement a very basic program which would read in data stored in a text file, display it as a "ToDo List", let the user add to it, then on saving write all data to the file.
Below is the code I submitted for the linked list, just wanted to get an insight on whether or not it implements a linked list as this is the only assignment I failed to receive a perfect score on.
#ifndef TASKLIST_H
#define TASKLIST_H
#include "data.h"
class TaskList
{
public:
TaskList();
TaskList(char fileName[]);
~TaskList();
void readAllTasks(char fileName[]);
/* Function to read data stored in file. */
void addTask(const Task& theTask);
/* Adds a task to the array */
void printTask()const;
/* Function to print out all task s*/
void printSpecificTask(const char query[])const;
/* Overloaded print task function only prints out tasks for a specific course */
void writeAllTasks(char fileName[])const;
/* Function to write all tasks to the output fill when program is closed */
private:
struct Node
{
Task theTask;
Node * next;
Node(const Task& inTask)
{
theTask = inTask;
next = NULL;
}
};
Node * head;
Node * tail;
int index;
int courseCol;
int infoCol;
int dateCol;
char query[MAX_COURSE];
void addSortedByCourse(const Task& inTask);
void setCourseCol(char course[]);
void setInfoCol(char info[]);
void setDateCol(char date[]);
};
#endif
#include <iostream>
#include <iomanip>
#include <fstream>
#include "TaskList.h"
using namespace std;
TaskList::TaskList()
{
head = NULL;
tail = NULL;
index = 0;
courseCol = 0;
infoCol = 0;
dateCol = 0;
}
TaskList::TaskList(char fileName[])
{
head = NULL;
tail = NULL;
index = 0;
courseCol = 0;
infoCol = 0;
dateCol = 0;
readAllTasks(fileName);
}
TaskList::~TaskList()
{
Node * curr = head;
while(curr)
{
head = curr->next;
delete curr;
curr = head;
}
}
void TaskList::readAllTasks(char fileName[])
{
/* This function opens the associated file. Reads in the data stored there
and uses it to fill an array of type task. As it fills the array it
also counts the number of tasks read in.*/
ifstream read;
read.open(fileName);
if(!read)
{
cerr << "Unable to open " << fileName << " for input!" << endl;
exit(1);
}
char course[MAX_COURSE],
info[MAX_INFO],
date[MAX_DATE];
Task currTask;
read.get(course, MAX_COURSE, ';');
while(!read.eof())
{
read.ignore(100,';');
read.get(info, MAX_INFO, ';');
read.ignore(100,';');
read.get(date, MAX_DATE, '\n');
read.ignore(100,'\n');
currTask.storeCourse(course);
currTask.storeInfo(info);
currTask.storeDate(date);
addTask(currTask);
/* After reading in the task information it copys the information into
the array using addTask*/
read.get(course, MAX_COURSE, ';');
}
read.close();
return;
}
void TaskList::addTask(const Task &theTask)
{
/* Function passes the new task to function addSortedByCourse which will
insert the new task into the linked list sorted by course name */
addSortedByCourse(theTask);
return;
}
void TaskList::addSortedByCourse(const Task& inTask)
{
char course [MAX_INFO];
char info [MAX_INFO];
char date [MAX_INFO];
char currCourse [MAX_INFO];
inTask.getCourse(course);
inTask.getInfo(info);
inTask.getDate(date);
setCourseCol(course);
setInfoCol(info);
setDateCol(date);
Node * newNode = new Node(inTask);
/* New Node to hold the new Task */
Node * prev = NULL;
Node * curr = head;
while(curr)
{
curr->theTask.getCourse(currCourse);
if(strcmp(currCourse, course) > 0)
break;
prev = curr;
curr = curr->next;
}
newNode->next = curr;
/* Code to insert the newNode */
if(!prev)
{
head = newNode;
tail = newNode;
}
else
{
prev->next = newNode;
tail = newNode;
}
index++;
}
void TaskList::printTask() const
{
/* This function simply outputs what is stored in the array.
If the list is empy then it will output the appropriate message.*/
char course[MAX_COURSE],
info[MAX_INFO],
date[MAX_DATE];
Node * curr;
cout << left;
if(index == 0)
{
cout << "There is nothing currently in your Task List." << endl;
return;
}
for(curr = head; curr; curr = curr->next)
{
curr->theTask.getCourse(course);
curr->theTask.getInfo(info);
curr->theTask.getDate(date);
cout << setw(courseCol) << course << setw(infoCol) << info << setw(dateCol)
<< date << endl;
}
return;
}
void TaskList::printSpecificTask(const char query[])const
{
/* This function does the same as print task list above accept it also accepts
a search paramater and will output only the information related to the
search paramaters. If nothing matches the search paramaters then an
appropriate message will be displayed. */
bool found = false,
shown = false;
char taskQuery[MAX_COURSE],
course[MAX_COURSE],
info[MAX_INFO],
date[MAX_DATE];
Node * curr;
cout << left;
if(index == 0)
{
cout << "There is nothing currently in your Task List." << endl;
return;
}/* Tests if there is information in the linked list */
for(curr = head; curr; curr = curr->next)
{
curr->theTask.getCourse(taskQuery);
if(strcmp (query, taskQuery) == 0)
{
found = true;
if(shown == false)
{
cout << "\nHere are the tasks matching your search: " << endl << endl;
shown = true;
}
curr->theTask.getCourse(course);
curr->theTask.getInfo(info);
curr->theTask.getDate(date);
cout << setw(courseCol) << course << setw(infoCol) << info << setw(dateCol)
<< date << endl;
}
}
if(!found)
{
cout << "Course: " << query << " not found." << endl;
}
found = false;
shown = false;
return;
}
void TaskList::writeAllTasks(char fileName[]) const
{
/* This program executes before the program quits and writes all the
information to the associated file. */
ofstream write;
Node * curr;
char course[MAX_COURSE],
info[MAX_INFO],
date[MAX_DATE];
write.open(fileName);
if(!write)
{
cerr << "Failed to open " << fileName << " for output." << endl;
exit(1);
}
for(curr = head; curr; curr = curr->next)
{
curr->theTask.getCourse(course);
curr->theTask.getInfo(info);
curr->theTask.getDate(date);
write << course << ';' << info << ';'
<< date << endl;
}
write.close();
return;
}
void TaskList::setCourseCol(char course[])
{
int test = strlen(course) + 1;
if (test > courseCol)
courseCol = test;
return;
}
void TaskList::setInfoCol(char info[])
{
int test = strlen(info) + 1;
if (test > infoCol)
infoCol = test;
return;
}
void TaskList::setDateCol(char date[])
{
int test = strlen(date) + 1;
if (test > dateCol)
dateCol = test;
return;
}

New Topic/Question
Reply




MultiQuote



|