Created
June 28, 2022 06:04
-
-
Save harshabhadra/92a567cca6a6b9e5ab4a37f53095218d to your computer and use it in GitHub Desktop.
Sample of Custom Single LinkedList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmployeeLinkedList { | |
private EmployeeNode head; | |
private int size; | |
public void addToFront(Employee employee) { | |
EmployeeNode node = new EmployeeNode(employee); | |
node.setNext(head); | |
head = node; | |
size++; | |
} | |
public EmployeeNode removeFromFront() { | |
if (isEmpty()) { | |
return null; | |
} | |
EmployeeNode removedNode = head; | |
head = head.getNext(); | |
size--; | |
removedNode.setNext(null); | |
return removedNode; | |
} | |
public int getSize() { | |
return size; | |
} | |
public boolean isEmpty() { | |
return head == null; | |
} | |
public void printList() { | |
EmployeeNode current = head; | |
System.out.print("HEAD -> "); | |
while (current != null) { | |
System.out.println(current); | |
System.out.print(" -> "); | |
current = current.getNext(); | |
} | |
System.out.println((Object) null); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Employee { | |
private String firstName; | |
private String lastName; | |
private int id; | |
public Employee(String firstName, String lastName, int id) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.id = id; | |
} | |
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 getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Employee employee = (Employee) o; | |
if (id != employee.id) return false; | |
if (!firstName.equals(employee.firstName)) return false; | |
return lastName.equals(employee.lastName); | |
} | |
@Override | |
public int hashCode() { | |
int result = firstName.hashCode(); | |
result = 31 * result + lastName.hashCode(); | |
result = 31 * result + id; | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "Employee{" + | |
"firstName='" + firstName + '\'' + | |
", lastName='" + lastName + '\'' + | |
", id=" + id + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmployeeNode { | |
private Employee employee; | |
private EmployeeNode next; | |
public EmployeeNode(Employee employee) { | |
this.employee = employee; | |
} | |
public Employee getEmployee() { | |
return employee; | |
} | |
public void setEmployee(Employee employee) { | |
this.employee = employee; | |
} | |
public EmployeeNode getNext() { | |
return next; | |
} | |
public void setNext(EmployeeNode next) { | |
this.next = next; | |
} | |
public String toString() { | |
return employee.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment