C++ Linked List In this program you will create a linked list. Each node will ha
ID: 3816265 • Letter: C
Question
C++ Linked List
In this program you will create a linked list. Each node will have the following information:
Employee Number
First name
Last Name
Age
This program will have the following functions:
Display List
List the entries in the nodes.
Add Employees
Create new nodes in the list
Change Employee info
Edit and change information in a node in the list
User must specify the Employee number
Produce a meaningful message if the employee number is not in the list
All numeric input must be validated.
Delete Employees :
remove the node from the list.
Do not forget that if a node is removed from the middle you must reconnect the prior node with the next node.
Count List :
This function will count the number of items in a linked list NOT including the Head. Display the following message:
NNNN has a linked list with ### entries.
where NNNN is your first and last name and ### is the number of entries
e.g. John Smith has a linked list with 4 entries.
Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
struct node
{
int empNumber;
String firstName;
String lastName
Int age;
}*start;
class empLinkedList
{
public:
node* createNode(int);
void display();
void addEmp();
empLinkedList()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
empLinkedList emp;
start = NULL;
while(1)
{
cout<<"1.Display Employee List"<<endl;
cout<<"2.Add Employee Data"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Display Employee List: "<<endl;
emp.display();
cout<<endl;
break;
case 2:
cout<<"Add Employee: "<<endl;
emp.addEmp();
cout<<endl;
break;
}
}
}
//Display Employee List
void empLinkedList::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"Employee List is void."<<endl;
return;
}
temp = start;
cout<<"Employees List is as follows: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
// create a new node to add data
node * empLinkedList::createNode(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
//Add Employee Data
void empLinkedList::addEmp()
{
int empId,age;
string firstName,lastName;
cout<<"Enter the Employee Id: ";
cin>>empId;
cout<<"Enter the Employee First Name: ";
cin>>firstName;
cout<<"Enter the Employee Last Name: ";
cin>>lastName;
cout<<"Enter the Employee Age: ";
cin>>age;
struct node *temp, *p;
temp = createNode(empId);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Employee Added."<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.