C++ Code 1) Create a linked-list class that holds employees. • Create a new poin
ID: 3761044 • Letter: C
Question
C++ Code 1) Create a linked-list class that holds employees. • Create a new pointer within the linked list class called ‘currentPtr’. This will be a position pointer which points to the current linked list node. • Minimum functions within the linked list class include: • appendNode • reverseList • insertAfterNode • insertBeforeNode • deleteNode • destroyList • traverseList (DO NOT include this method as your main program will use it with the new positionTo and getCurrentPtr methods) • getSize (returns the total number of employees) • findNode (finds a node based on the employee ID) • positionTo (positions the ‘current’ pointer in the class to a specified position in the list - int) • This function will return true or false indicating if the positioning in the linked list was successful • Note that since it takes an int as a position parameter (starting at 1), then it is limited to 32,768 possible positions in the list • getCurrentPtr (returns the list node of the currentPtr private member) • linkedList() • Make sure you set the list’s head pointer to null here. • ~linkedList() • Make sure you call the destroyList method from inside of here. 2. Modify all of your main() code which access the employee vector in your program to now use the linked list • You will use the positionTo() method in the linked list to place yourself in the list for things like for loops in your main(). If it returns false, then there was a problem in your linked list logic. If it returns true, then you found your spot in the linked list. • You will use the getCurrentPtr() method to access the data member of the linked list node to access methods for that data (employee, production worker, team leader, shift supervisor) 3. Add a new menu item to your main menu which allows all employees to be printed off in a nice little listing/report. Include the following data (example below): • ID • Full name • Type of employee • Hire Date
Explanation / Answer
class Employee
{
private String id;
private String name;
public Employee(String id, String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return "Employee [id=" + id + ", name=" + name + "] ";
}
}
class LinkedListEmptyException extends RuntimeException
{
public LinkedListEmptyException()
{
super();
}
public LinkedListEmptyException(String message)
{
super(message);
}
}
class Node<T>
{
public T data;
public Node<T> next;
public Node(T data)
{
this.data = data;
}
public void displayNode()
{
System.out.print( data + " ");
}
}
class LinkedList<T>
{
private Node<T> first;
public LinkedList()
{
first = null;
}
public void insertFirst(T data)
{
Node<T> newNode = new Node<T>(data);
newNode.next = first;
first = newNode;
}
public Node<T> deleteFirst()
{
if(first==null)
{
throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
}
Node<T> tempNode = first;
first = first.next;
return tempNode;
}
public void displayLinkedList()
{
System.out.print("Displaying the LinkedList [first--->last]: ");
Node<T> tempDisplay = first;
while (tempDisplay != null)
{
tempDisplay.displayNode();
tempDisplay = tempDisplay.next;
}
System.out.println();
}
}
public class LinkedListGenericApp
{
public static void main(String[] args)
{
LinkedList<Employee> linkedList = new LinkedList<Employee>();
linkedList.insertFirst(new Employee("21", "jack"));
linkedList.insertFirst(new Employee("25", "jim"));
linkedList.insertFirst(new Employee("94", "shane"));
linkedList.insertFirst(new Employee("38", "pratap"));
linkedList.insertFirst(new Employee("64", "rock"));
linkedList.displayLinkedList();
System.out.print("The Deleted Nodes are: ");
Node<Employee> deletedNode = linkedList.deleteFirst();
deletedNode.displayNode();
deletedNode = linkedList.deleteFirst();
deletedNode.displayNode();
System.out.println();
linkedList.displayLinkedList();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.