In: Computer Science
Objective: Learning linked list.
Problem Specification:
An employer would like to maintain a linked list for employees, the data stored is
·An employee number (a positive integer)
·A yearly salary (a float).
·Number of dependents (a short positive integer)
The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses a menu driven program with options to handle choices (methods). The methods are:
·Insert: Which inserts elements at the beginning of the list, which is the most recent input is at the beginning of the list.
·Remove: which deletes the last element in the list.
·Display: its purpose is to display the list but needs the assistance of a Print function.
·Print: a recursive function that prints all the elements of the list, first to last.
·Clear: a recursive function that deletes every Node from the list and leaves the list empty.
Requirements:
·Define a class Node containing the employee’s data and a pointer to the next Node.
·Define the necessary functions to access, instantiate, and set the data in the class Node.
·Define a class LinkedList that has only one data member, a pointer to a Node, and the necessary member functions in addition to the member functions above.
Grading criteria:
10 points Sufficient comments including specifications
5 points Menu is used to display options and calls methods.
5 points Guards are used.
10 points Insert performs it task correctly.
10 points Remove performs it task correctly.
10 points Display performs it task correctly.
10 points print is recursive and performs it task correctly.
10 points Clear performs it task correctly in a recursive manner.
10 points UML class diagrams are submitted and each is correct.
15 points Program runs correctly and performs its task correctly.
5 points test run is handed-in and demonstrates all activities.
Submission Details:
Submit a print-out of:
·The source program
·Demonstration of all activities.
#include<iostream>
#include<string>
using namespace std;
//Class Node to hold data for single employee.
class Node
{
private:
string number;
int year;
unsigned short int
dependents;
Node *next;//Pointer to
next node 1.e. next Employee data
public:
Node()//default constructor
to set default values into data members
{
number="";
year=0;
dependents=0;
}
//Setters
void setNumber(string
number_)
{
number=number_;
}
void setYear(int year_)
{
year=year_;
}
void setDependencies(unsigned short
int dependents_)
{
dependents=dependents_;
}
void setNext(Node* next_)
{
next=next_;
}
//Gettres
string getNumber()
{
return
number;
}
int getYear()
{
return
year;
}
unsigned short int
getDependencies()
{
return
dependents;
}
Node *getNext()
{
return
next;
}
};
//Class List
class Employees
{
private:
Node *head;//To hold the
start of the list
public:
Employees()//default
constructior to set head to NULL
{
head=NULL;
}
void Insert(string no,int
year_,unsigned short int dep)
{
//Create new node
Node
*newNode=new Node();
newNode->setNumber(no);
newNode->setYear(year_);
newNode->setDependencies(dep);
//Set New node to the front of the node.
newNode->setNext(head);
head=newNode;
}
void Remove()
{
if(head==NULL)
{
cout<<"\nNo element in the list\n";
}
else
{
if(head->getNext()==NULL)
{
delete(head);
head=NULL;
}
else
{
Node *trev=head;
while(trev->getNext()->getNext()!=NULL)//Traverse
till the second last node
{
trev=trev->getNext();
}
//Rempove the right neighbour of the second last node,
which is the last node
Node
*temp=trev->getNext();
trev->setNext(NULL);
delete(temp);
}
}
}
//Print
void Display()
{
if(head==NULL)
{
cout<<"\nNo element in the list\n";
}
else
{
cout<<"\n\t\tEmployee Data\n";
Print(head);//call assistance function
Print
}
}
void Print(Node *node)
{
if(node!=NULL)//print the Employee data if given node is
not null
{
cout<<"\n\t Employee
Number="<<node->getNumber()<<" Employee
year:"<<node->getYear()<<" Employee number of
dependencies="<<node->getDependencies()<<endl;
Print(node->getNext());//call
Recursive function by bassing next node
}
}
//Reverse
void Clear(Node *node)
{
if(node!=NULL)//call the recursive function Clear till
given node is not NULL
{
cout<<"\n\t\tDeleting....\n";
Clear(node->getNext());
delete(node);//delete the current
provided node
}
head=NULL;//make head as NULL
}
//getter for head
Node *getHead()
{
return
head;
}
};
int main()
{
Employees lst;
int ch=-1;
string no;
int year_;
unsigned short int dep;
while(ch!=0)//print menu till user enter 0
which is the exut state
{
cout<<"1. Insert\n";
cout<<"2. Remove\n";
cout<<"3. Display\n";
cout<<"4. Clear\n";
cout<<"0. Exit\n";
cout<<"\nPlease choose a menu
option:\n";
cin>>ch;
switch(ch)
{
case 1:
//Get data from user to pass the data to Insert
function
cout<<"\nEnter Employee number:";
cin>>no;
cout<<"\nEnter year:";
cin>>year_;
cout<<"\nEnter Number of
Dependicies:";
cin>>dep;
lst.Insert(no,year_,dep);
break;
case 2:
lst.Remove();
break;
case 3:
lst.Display();
break;
case 4:
lst.Clear(lst.getHead());
break;
default:
if(ch!=0)//if user does not enter 0 then
other choices are invalid. So priunt error message
{
cout<<"\nInvalid menu
option.\n";
}
break;
}
}
return 0;
}