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

Q. 1) Write a program to implement the singly linked list data structure, which

ID: 3752158 • Letter: Q

Question

Q. 1) Write a program to implement the singly linked list data structure, which has following operations:


-  A Linked list node should contain data, which stores first name, last name, age, and dob of a person.
- A user should be allowed to add a node at the head, tail, and after a node with given data.
- A user should be able to search is a particular person’s information is stored in the linked list or not.
- A user should be able to remove a particular person’s information from the linked list.
- A user should be able to print all the information of all the people stored in the linked list.

Explanation / Answer

public class PersonLinkedList { class Node { Person employee; Node next = null; public Node(Person employee) { this.employee = employee; } } Node head = null; // to add some employee in the list void addEmployeeAtHead(Person e) { Node newNode = new Node(e); if(head != null) { newNode.next = head; } head = newNode; } void addEmployeeAtTail(Person e) { Node x = head; if(x == null) { addEmployeeAtHead(e); return; } Node newNode = new Node(e); while(x.next != null) { x = x.next; } x.next = newNode; } // Find method, which searches the list to find employee with given id void addEmployeeAfter(String name, Person e) { Node p = head; Node prev = null; while(p != null) { if(p.employee.getFirstName().toLowerCase().contains(name.toLowerCase()) || p.employee.getFirstName().toLowerCase().contains(name.toLowerCase())) { break; } prev = p; p = p.next; } if(p == null) { // add at last prev.next = new Node(e); } else { Node x = new Node(e); x.next = p.next; p.next = x; } } // this method finds a name in first or last name and removes node. void remove(String name) { Node p = head; Node prev = null; while(p != null) { if(p.employee.getFirstName().toLowerCase().contains(name.toLowerCase()) || p.employee.getFirstName().toLowerCase().contains(name.toLowerCase())) { // Node p need to be deleted if(prev == null) { // Change head head = head.next; } else { prev.next = p.next; } } prev = p; p = p.next; } } // Find method, which searches the list to find employee with given id Person find(String name) { Node p = head; while(p != null) { if(p.employee.getFirstName().toLowerCase().contains(name.toLowerCase()) || p.employee.getFirstName().toLowerCase().contains(name.toLowerCase())) { // To create a copy of the original object return new Person(p.employee); } p = p.next; } return null; } void printList() { Node p = head; while(p != null) { System.out.println(p.employee); p = p.next; } } } ================ import java.util.Date; public class Person { private String firstName, lastName; private int age; private Date birthDate; // main constructor public Person(String firstName, String lastName, int age, Date birthDate, double rateOfPay) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.birthDate = birthDate; } // copy constructor public Person(Person other) { this.firstName = other.getFirstName(); this.lastName = other.getLastName(); this.age = other.getAge(); this.birthDate = other.getBirthDate(); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } @Override public String toString() { return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", birthDate=" + birthDate + "]"; } }