#ifndef __lab4b__taskList__
#define __lab4b__taskList__
#define MAX_CHAR 100
#include <iostream>
usingnamespace std;
// Define a structure to use as the list item
struct taskEntry_S
{
int priority; // Task priority and key
char theTask[128]; // Task string;
char theCourse[128]; // Task string;
char theDate[128]; // Task string;
};
#define MAX_ENTRY 50 // Define maximum number of tasks in list
class taskList {
private:
taskEntry_S taskEntry[MAX_ENTRY]; // List implemented as an array
int lastIndex; // Index where last item was inserted
public:
taskList(); // Class constructor
~taskList(); // Class destructor
void ClearList(); // Remove all items from the list
void Insert(char *course, char *task, char *date, int priority); // Add a task to the list
void Delete(char priority); // Delete given a task string
void Delete(int priority); // Delete given a priority
char *Search(int priority); // Search given a priority
int ListLength(); // Return number of items in list
bool isEmpty(); // Return true if list is empty
bool isFull(); // Return true if list is full
char *GetNextTask(); // Return the next task
void PrintList(); // Print all items in the list
};
#endif /* defined(__lab4b__taskList__) */
#include "taskList.h"
#include <cstdlib> // To get access to malloc()
#include <string.h>
//--------------------------------------------
// Function: taskList()
// Purpose: default constructor: Sets last index -1
// so the first entry will be index key 0;
// Returns: N/A
//--------------------------------------------
taskList::taskList()
{
lastIndex = -1;
}
//--------------------------------------------
// Function: taskList()
// Purpose: Destuctor
// Returns: N/A
//--------------------------------------------
taskList::~taskList()
{
}
//--------------------------------------------
// Function: ClearList()
// Purpose: Removes all items from the list.
// Returns: N/A
//--------------------------------------------
void taskList::ClearList()
{
lastIndex = -1; // Reseting count.
}
//--------------------------------------------
// Function: Insert()
// Purpose: insert task into the list correct order.
// Returns: N/A
//--------------------------------------------
void taskList::Insert(char *course, char *task, char *date, int priority)
{
int index = 0;
if (isFull())
{
cout << "Cannot add the task: " << task;
cout << "\n to the list. List full.\n";
return;
}
// Increment lastIndex
lastIndex++;
// Move all tems down list until correct location is found.
while ((index > 0) && taskEntry[index-1].priority > priority) {
taskEntry[index] = taskEntry[index -1];
index--;
}
taskEntry[index].priority = priority;
strcpy(taskEntry[index].theCourse, course);
strcpy(taskEntry[index].theTask, task);
strcpy(taskEntry[index].theDate, date);
}
//--------------------------------------------
// Function: Delete
// Purpose: deletes a task from the list
// Returns: N/A
//--------------------------------------------
void taskList::Delete(int priority)
{
int i, k = 0;
while ((k < lastIndex) && (taskEntry[k].priority != priority))
{
k++;
}
if (k > lastIndex) {
cout << "Unable to find task to delete!? Please try again!!" << endl;
}
elsefor (i = k ;i < lastIndex; i++)
{
taskEntry[i] = taskEntry[i+1];
}
lastIndex--;
}
// _____CHANGE TO COURSE_________
//--------------------------------------------
// Function: Search()
// Purpose: searchs for task by priortiy.
// Returns: Char pointer pointing to a new
// string containing a copy of the task.
//--------------------------------------------
char *taskList::Search(int priority)
{
int s = 0;
char *returnStr;
while ((s <= lastIndex) && (taskEntry[s].priority))
{
s++;
}
if (s > lastIndex) {
cout << "Task not found.\n\n";
return""; // could not find task
}
else
{
returnStr = (char *)malloc(strlen(taskEntry[s].theTask)+1);
strcpy(returnStr, taskEntry[s].theTask);
return returnStr;
}
}
//--------------------------------------------
// Function: ListLength()
// Purpose: Returns number of items in list
// Returns: Number of items in list
//--------------------------------------------
int taskList::ListLength()
{
return lastIndex+1;
}
//--------------------------------------------
// Function: isEmpty()
// Purpose: Returns true if list is empty.
// Returns: True if empty otherwise returns false.
//--------------------------------------------
bool taskList::isEmpty()
{
return (lastIndex == -1);
}
//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the list is full
// Returns: True if full, otherwise false
//--------------------------------------------
bool taskList::isFull()
{
return (lastIndex >= MAX_ENTRY);
}
//--------------------------------------------
// Function: GetNextTask()
// Purpose: Return the task string of the task
// at the head of the list.
// Returns: char pointer pointing to a new
// string containing a copy of the task.
//--------------------------------------------
char *taskList::GetNextTask()
{
int i = 0;
char *returnStr;
if (isEmpty())
{
cout << "List is empty.\n\n";
return"";
}
else
{
returnStr = (char *)malloc(strlen(taskEntry[0].theTask) + 1);
strcpy(returnStr, taskEntry[0].theTask);
}
// move other tasks up
while (i < lastIndex)
{
taskEntry[i] = taskEntry[i+1];
}
lastIndex--;
return returnStr;
}
//--------------------------------------------
// Function: PrintList()
// Purpose: Print all tasks in the list with
// their priority.
// Returns: void
//--------------------------------------------
void taskList::PrintList()
{
int i;
cout << "\n\nTasks in the List\n";
cout << "-----------------------------------------------------------\n";
cout << "Priority\t\tTask\n";
cout << "-----------------------------------------------------------\n";
for (i = 0; i < lastIndex ; i++)
{
cout << taskEntry[i].priority << "\t\t" << taskEntry[i].theCourse << "\t\t" << taskEntry[i].theTask << "\t\t" << taskEntry[i].theDate << "\n";
}
}
Priority Task
-----------------------------------------------------------
5 This is task 3 This is task 3 This is task 3
0
List now contains 3 items.
expected output
1 2 3 4 5 6 7
Simple List Demonstration
Tasks in the List
Priority Task
-----------------------------------------------------------
5 This is task 1 This is task 1 This is task 1
5 This is task 2 This is task 2 This is task 2
5 This is task 3 This is task 3 This is task 3
List now contains 3 items.