Create a program that implements a singly linked list of Emp
Create a program that implements a singly linked list of Employee. BY USING JAVA (NETBEANS)
Each node must contain the following variables:
- Emp_ID
- Emp_Salary
* Create the following list using addTail(). The list must be in order shown below.
* Delete the tail of the previous list.
* Print out the contents of the lists.
*Find the node of particular Employee by giving Employee ID in the above list.
* Get the length of the above list.
| Emp_ID | Emp_Salary |
| 00173 | Sr4500 |
| 02367 | Sr5000 |
| 00996 | Sr5400 |
| 00273 | Sr6500 |
Solution
PROGRAM CODE:
LinkedList.java
package employee;
/**
OrderedLinkedList class is for implementing linked list
Basic functions such as getFirstNode, remove, insert are implemented.
*/
public class LinkedList {
/**
The Node class which contains a payload , a key and a next.
next will hold the next node in the linked list
*/
private class Node{
public String emp_id;
public double salary;
public Node next;
/**
* default constructor for the Node class
*
*/
public Node()
{
this.emp_id = \"\";
this.salary = 0;
this.next = null;
}
/**
* parameterized constructor for the Node class
* @param emp_id - contains the string value
* @param salary - double value which represents the salary
*/
public Node(String emp_id, double salary)
{
this.emp_id = emp_id;
this.salary = salary;
this.next = null;
}
}
private Node first;
public LinkedList()
{
}
/**
* checks whether the list is empty
* @return true or false indicating whether the list is empty
*/
public boolean empty()
{
boolean isEmpty = false;
if(first == null)
{
isEmpty = true;
}
return isEmpty;
}
/**
* find the first element in the list
* @throws throws an exception if the list doesnt contain elements
* @return return the empid for the first node
*/
public String getFirst( ) throws Exception{
String Myemp_id = \"\";
try{
Myemp_id = first.emp_id;
}
catch (Exception e) {
// TODO: handle exception
System.out.println(\"The list is empty.\");
}
return Myemp_id;
}
/**
* find the last element in the list
* @throws throws an exception if the list doesnt contain elements
* @return return the emp_id for the last node
*/
public String getLast( ) throws Exception
{
String Myempid = \"\";
Node nodes = first;
while(nodes.next != null)
{
nodes = nodes.next;
}
try{
Myempid = nodes.emp_id;
}catch(Exception e)
{
System.out.println(\"List is empty\");
}
return Myempid;
}
/**
* inserts element in the list
* @param empid - the string to be inserted in the node
* @param salary - the salary of the employee
*/
public void addTail(String empid, double salary){
Node nodeTraverse = first;
if(first == null)
{
first = new Node(empid, salary);
first.next = null;
}
else
{
while(nodeTraverse.next != null)
{
nodeTraverse = nodeTraverse.next;
}
nodeTraverse.next = new Node(empid, salary);
}
}
/**
* removes an element from the list*/
public void delete(){
Node current = first;
Node prev = first;
while(current.next!=null)
{
prev = current;
current = current.next;
}
prev.next = null;
}
/**
* find an element from the list
* @param empid - the emp_id to be searched
*/
public Node find(String empid){
Node node = null;
Node current = first;
while(current!=null)
{
if(current.emp_id.equals(empid))
{
node = new Node(current.emp_id,current.salary);
break;
}
else
{
current = current.next;
}
}
return node;
}
/**
* finds the total size of the list
* @return integer - count of nodes in the list
*/
public int listCount( ) {
int count = 0;
Node temp = first;
while(temp != null)
{
count++;
temp = temp.next;
}
return count;
}
public void display(Node node)
{
String values = \"\";
while(node != null)
{
values = values + node.emp_id + \" \" + node.salary + \"\ \";
node = node.next;
}
System.out.println(values);
}
/**
* displays the value of the empid and salary by traversing through the list
* @return string - empid and the salary
*/
public String toString( ) {
Node node = first;
String values = \"\";
while(node != null)
{
values = values + node.emp_id + \" \" + node.salary + \"\ \";
node = node.next;
}
return values;
}
}
EmployeeDriverClass.java
package employee;
public class EmployeeDriverClass {
static LinkedList employeeList = new LinkedList();
public static void main(String args[])
{
employeeList.addTail(\"00173\", 4500);
employeeList.addTail(\"02367\", 5000);
employeeList.addTail(\"00996\", 5400);
employeeList.addTail(\"00273\", 6500);
System.out.println(\"Display List: \");
System.out.println(employeeList);
System.out.println(\"Find: 02367\");
employeeList.display(employeeList.find(\"02367\"));
System.out.println(\"List Count: \"+ employeeList.listCount() + \"\ \");
employeeList.delete();
System.out.println(\"Display List after delete: \");
System.out.println(employeeList);
}
}
OUTPUT:
Display List:
00173 4500.0
02367 5000.0
00996 5400.0
00273 6500.0
Find: 02367
02367 5000.0
List Count: 4
Display List after delete:
00173 4500.0
02367 5000.0
00996 5400.0







