Sie sind auf Seite 1von 6

ECE244

Programming Fundamentals

Fall 2010

Lab Assignment #5: Linked Lists


1.

Objectives

The objectives of this assignment are (1) to provide you with more practice on the use of the
various C++ concepts/constructs introduced in the lectures so far, including classes, lists,
dynamic memory allocation, pointers, I/O, and dynamic data structures; and (2) to provide you
with more practice on the use of the Make utility and the debugger.

2.

Problem Statement

In this assignment, you will implement the similar functionality as that for Lab #4, but instead
of using arrays, you are to use linked lists --- in fact a list of lists. Your Lab #5 program will be
tested similar to the way your Lab #4 program was tested.
Your solution will have to implement two types of linked lists, namely a list of divisions, and a
list of employees connected to a division. Your program:
will not make use of the constants MAX_DIVISIONS and MAX_EMPLOYEES, as there
will be no such maximums;
may not use any arrays anywhere in the program, except for character arrays;
must have all class data members be private, accessing them through accessor
functions; and
must not use any global variable except for one instance of a division list.
You will have to create a database of employees that looks as follows:

Division
List

Division
Employee
List

next

division
Employee
List

next

division
Employee
List

Empl_no

Empl_no

Empl_no

next

next

next

next

A division list is organized as a set of linked division nodes. Each division node contains a
division number, which must be unique and which is the index. The division node also contains
an employee list, identifying which employees work for that division. The division nodes are to
be ordered according to the division number.
The employee list is organized as a set of employee nodes. Each employee node contains an
employee number, which is the index, the employee`s first and last name, the hourly wage, and
the hours worked. The employee list is ordered by the employee number.
Lab #5

Page 1 of 6

2.1. Input/Output
The driver function will implement the user interface to the database. It inputs from cin a
sequence of commands. Each command consists of an operation, followed by its parameters.
The command and the parameters are separated by white space. You can assume that all input
provided is syntactically correct. The function should process commands until the end-of-file
(eof) is encounters. The commands and their parameters are as follows:

newdivision divisionNum
This command creates a new division node with the division number set to divNum.
The new record is inserted into the division list at the correct location.
If there already exists a division with the same divNum, then the error message
``rror: division already exists.`` must be output. Otherwise, no
output is produced.

newemployee emplNum divNum firstName lastName wage hours


This command creates a new employee node with emplNum as the employee number;
the employee works for division divNum, has the name firstName lastName,
has the wage wage and works for hours hours a week. The new node is then inserted
into appropriate employee list at its correct location.
If there is no division with divNum, then the error message Error: division
does not exist. must be output. If an employee with emplNum already exists,
then the error message rror: employee already exists. must be
output. Otherwise, no output is produced.

locate emplNum
This command locates the node with the employee number emplNum in the database,
and prints its contents to cout. If no such number exists, the error message rror:
no employee with number <emplNum> exists. must be output.

delete emplNum
This command locates the node with the employee number emplNum in the database
and deletes the node. If no such node exists, then the error Error: no employee
with number <emplNum> exists. must be output. Otherwise, nothing is
output.

deleteall
This command deletes all the nodes in the database, returning it to the empty state.
Nothing is output.

deletedivision divNum
This command deletes the division with division number divNum (and along with it all
its employees). If no such division exists, then the error Error: no division
with number divNum exists.must be output. Otherwise, nothing is output.

printdivision divNum
This command prints out all employes working for the division divNum in order of
employee number. If no such division exists, then the error rror: no division
with number divNum exists. must be output.

Lab #5

Page 2 of 6

printall
This command prints out all employees ordered first by division then by employee
number.

When employee information is to be printed out, then the following format should be used:
<employeeNum> <firstName> <lastName>
for each employee on a separate line. Note that employee numbers are always 5 digits long,
division numbers are exactly 2 digits long, and that there is exactly one space between each
field. When doing this, the employees should be output in order of their employee number. For
printall, the employee information for all employees of each division should be printed out
ordered by increasing division number then increasing employee number. For each division, the
following should be output on a separate line
Division <divisionNum>:
followed by all the employees of that division.
2.2. Global Variables
You may only have one global variable of type DivisionList; e.g.
DivisionList database ;
2.3. DivisionList Class
The DivisionList class only contains as a data member the header which points to the first node
in the list, and NULL otherwise. Most of the class functions are self-explanatory:
class DivisionList {
private:
DivisionNode* head ;
public:
DivisionList() ;
~DivisionList() ;
bool newDivision( int divNum ) ;
// returns FALSE if divNum already exists, TRUE otherwise
bool newEmployee( int emplNum, int divNum, char* firstName,
char* lastName, double hours,
double hourlyWage, int& error ) ;
// if divNum doesnt exist, set error to 1 and return FALSE
// if emplNum already exists, set error to 2 and return FALSE
// return TRUE otherwise
bool deleteDivision( int divNum ) ;
// return FALSE if divNum does not exist; else TRUE
EmployeeNode* locateEmployee( int emplNum ) ;
bool locateEmployee( int emplNum, int& divNum, char*& firstName,
char*& lastName ) ;
// return TRUE if found, setting divNum, firstname and
// lastName; and return FALSE otherwise
bool deleteEmployee( int emplNum ) ;
// if emplNum doesnt exist, return FALSE; TRUE otherwise
void printDivision( int divNum ) ;
void printAll() ;
} ;

Lab #5

Page 3 of 6

2.4. DivisionNode Class


DivisionNode objects are the nodes of the DivisionList class. Note that each DivisionNode
object has embedded an EmployeeList object.
class DivisionNode {
private:
int divisionNum ;
EmployeeList employees ;
DivisionNode* next ;
public:
DivisionNode() ;
DivisionNode( int divNum ) ;
~DivisionNode() ;
int getDivisionNum() ;
DivisionNode* getNext() ;
void setDivisionNum( int divNum ) ;
void setNext( DivisionNode* n ) ;
bool newEmployee( int emplNum, char* firstName,
char* lastName, double hours,
double hourlyWage) ;
// if emplNum already exists return FALSE; TRUE otherwise
EmployeeNode* locateEmployee( int emplNum ) ;
bool locateEmployee( int emplNum, char*& firstName,
char*& lastName ) ;
// if emplNum does not exists return FALSE; TRUE otherwise
bool deleteEmployee( int emplNum ) ;
// if emplNum does not exists return FALSE; TRUE otherwise
void printEmployees() ;
} ;

2.5. EmployeeList Class


The EmployeeList object contains a head pointer that points to the first node of the employee
list. The class functions are all self-explanatory.
class EmployeeList {
private:
EmployeeNode* head ;
public:
EmployeeList() ;
~EmployeeList() ;
EmployeeNode* getHead() ;
void setHead( EmployeeNode * n ) ;
bool newEmployee( int emplNum, char* firstName,
char* lastName, double hrs,
double hourlyWage) ;
// if emplNum already exists return FALSE; TRUE otherwise
EmployeeNode* locateEmployee( int emplNum ) ;
bool locateEmployee( int emplNum, char*& firstName,
char*& lastName ) ;
// if emplNum does not exists return FALSE; TRUE otherwise
bool deleteEmployee( int emplNum ) ;
// if emplNum does not exists return FALSE; TRUE otherwise
void printEmployees() ;
} ;

Lab #5

Page 4 of 6

2.6. EmployeeNode Class


class EmployeeNode {
private:
int employeeNumber ;
char* firstName ;
char* lastName ;
double wage ;
double hours ;
EmployeeNode* next ;
public:
EmployeeNode() ;
EmployeeNode( int emplNum, char* fName, char* lName,
double hourlyWage, double hrs,
EmployeeNode* n ) ;
~EmployeeNode() ;
int getEmplNumber() ;
char* getFirstName() ;
char* getLastName() ;
double getHourlyWage() ;
double getHours() ;
EmployeeNode * getNext() ;
void setEmplNum( int emplNum ) ;
void setFirstName( char* fName ) ;
void setLastName( char* lName ) ;
void setHourlyWage( double hourlyWage ) ;
void setHours( double h ) ;
void setNext( EmployeeNode* n ) ;
void print() ;
} ;

3.

Procedure

Create a sub-directory called lab5 in your ece244 directory, using the mkdir command,
and protect it with the chmod command. Make it your working directory, and then copy the
files you need from your Lab 4 directory. Using these files as a starting point, generate a
solution to this assignment. Additionally, create a Makefile that will produce a lab5
executable when you type make.
You may add your own helper functions to these classes. You may also add member data to
the classes, but they must be private.

4.

Deliverables

Your lab submission should consist of only the following eight files:
main.cpp Contains the main() function for your program.

Lab #5

Page 5 of 6

Driver.cpp A (possibly) modified version of your code from Lab #4.


Driver.h Contains the declaration for your function(s) in Driver.cpp, so that they
can be called from main.cpp.
DivisionNode.h Contains the declaration for the DivisionNode class, including any
additions that you have made.
DivisionNode.cpp Contains the implementation of the DivisionNode class.
EmployeeNode.h Contains the declaration for the EmployeeNode class, including
any additions that you have made.
EmployeeNode.cpp Contains the implementation of all member functions for the
EmployeeNode class.
DivisionList.h Contains the declaration for the Division List.
DivisionList.cpp Contains the implementation of the Division List.
EmployeeList.h Contains the declaration for the Employee List.
EmployeeList.cpp Contains the implementation of the Employee List.
Makefile A makefile that will compile your code and produce an executable called
lab5 when the make command is entered. (The default behavior of your Makefile
should only compile the program when make is run: it should not run the program or
perform any other tasks.)
Please note that it is ESSENTIAL that your Makefile produce an executable called lab5
when it is run, and that your lab5 accept input and produce output EXACTLY as specified,
without adding any additional output. Please submit your files by using the usual
submitece244f command. Before submitting, you should use the usual exercise
and chksubmit commands to verify that your submission is working as expected.

Lab #5

Page 6 of 6

Das könnte Ihnen auch gefallen