Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Linked List In this program you will create a linked list. Each node will ha

ID: 3821583 • Letter: C

Question

C++ Linked List

In this program you will create a linked list. Each node will have the following information:

Employee Number

1.First name

2.Last Name

3.Age

This program will have the following functions:

1. Display List

- List the entries in the nodes.

2. Add Employees

- Create new nodes in the list

3. 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

4. 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.

5. 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

// A linked list node
struct node
{
char firstName[100];
char lastName[100];
int age;
int empId;
struct node *next;
};

/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
void add(struct node** head_ref, char first[100], char last[100], int age, int empId)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
strcpy(new_node->firstName, first);
strcpy(new_node->lastName, last);
new_node->age = age;
new_node->empId = empId;
  
new_node->next = (*head_ref);
(*head_ref) = new_node;
  
}

/* Given a reference (pointer to pointer) to the head of a list
and a key, deletes the first occurrence of key in linked list */
void deleteNode(struct node **head_ref, int key)
{
// Store head node
struct node* temp = *head_ref, *prev;

// If head node itself holds the key to be deleted
if (temp != NULL && temp->empId == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}

// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->empId != key)
{
prev = temp;
temp = temp->next;
}

// If key was not present in linked list
if (temp == NULL) return;

// Unlink the node from linked list
prev->next = temp->next;

free(temp); // Free memory
}

/* Given a reference (pointer to pointer) to the head of a list
and a key, deletes the first occurrence of key in linked list */
void change(struct node **head_ref, int key, char *first, char *last, int age)
{
// Store head node
struct node* temp = *head_ref, *prev;

// If head node itself holds the key to be deleted
if (temp != NULL && temp->empId == key)
{
temp->age = age;
strcpy(temp->firstName, first);
       strcpy(temp->lastName, last);
return;
}

// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->empId != key)
{
prev = temp;
temp = temp->next;
}

// If key was not present in linked list
if (temp == NULL)
{
cout << "No employee with give employee number" << endl;
return;
}

temp->age = age;
strcpy(temp->firstName, first);
   strcpy(temp->lastName, last);
}


// This function prints contents of linked list starting from
// the given node
void printList(struct node *node)
{
while (node != NULL)
{
cout << " Employee: Id: " << node->empId;
cout << " Name: "<<node->firstName << " " << node->lastName;
cout << " Age: " << node->age << endl;
node = node->next;
}
}

int countList(struct node *node)
{
int count = -1;
while (node != NULL)
{
count++;
       node = node->next;
}
return count;
}

/* Drier program to test above functions*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
   char first[100] = "first1";
   char last[100] = "last1";
add(&head, first, last, 25, 1);
  
   char first2[100] = "first2";
   char last2[100] = "last2";
add(&head, first2, last2, 35, 2);
  
   char first3[100] = "first3";
   char last3[100] = "last3";
add(&head, first3, last3, 45, 3);
  
   char first4[100] = "first4";
   char last4[100] = "last4";
add(&head, first4, last4, 55, 4);

cout << "Created List: ";
printList(head);
  
cout << head->firstName << " " << head->lastName << " has a linked list with " << countList(head) << " entries" << endl;
  
deleteNode(&head, 1);
cout <<" Linked List after Deletion of 1: ";
printList(head);
  
   char first6[100] = "first";
   char last6[100] = "last";
change(&head, 2, first6, last6, 49);
cout << " Linked List after changing id 2: ";
printList(head);
  
change(&head, 1, first6, last6, 49);
return 0;
}

sample output

Created List: Employee: Id: 4 Name: first4 last4 Age: 55
Employee: Id: 3 Name: first3 last3 Age: 45
Employee: Id: 2 Name: first2 last2 Age: 35
Employee: Id: 1 Name: first1 last1 Age: 25
first4 last4 has a linked list with 3 entries

Linked List after Deletion of 1: Employee: Id: 4 Name: first4 last4 Age: 55
Employee: Id: 3 Name: first3 last3 Age: 45
Employee: Id: 2 Name: first2 last2 Age: 35

Linked List after changing id 2: Employee: Id: 4 Name: first4 last4 Age: 55
Employee: Id: 3 Name: first3 last3 Age: 45
Employee: Id: 2 Name: first last Age: 49
No employee with give employee number

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote