Linked List Implementation

Double checking that my code uses a linked list.

Page 1 of 1

2 Replies - 3044 Views - Last Post: 08 June 2010 - 08:49 AM Rate Topic: -----

#1 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Linked List Implementation

Posted 08 June 2010 - 06:40 AM

I'll be honest this is a homework assignment but with that said it's already been submitted and graded.

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Implementation

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked List Implementation

Posted 08 June 2010 - 07:19 AM

I would say that your TaskList class "is a" linked list. In other words it's a special-purpose linked list that can't readily be used for any other application. So, if the assignment was to implement a linked list and then use it to implement the TaskList application, that would imply a "has a" rather than an "is a" relationship, using a generic linked list class with generic methods such as insert_front, insert_back, insert_after, sort, print, is_empty, and so on. The TaskList class would have a linked list as one of its data members, and the methods that are specifically related to the TaskList application would be members of the TaskList class and not the linked list class.
Was This Post Helpful? 1
  • +
  • -

#3 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Linked List Implementation

Posted 08 June 2010 - 08:49 AM

Thanks r.stiltskin! The assignment only states to use a linear linked list to model TaskList so unless I misunderstood that's what I did(I think).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1