Question: //class node class node{ String emp_id; String emp_salary; node next; public node(String emp_id,String emp_salary){ this.emp_id=emp_id; this.emp_salary=emp_salary; next=null; } } //class linked list class LinkedList{ private
//class node class node{
String emp_id; String emp_salary; node next;
public node(String emp_id,String emp_salary){ this.emp_id=emp_id; this.emp_salary=emp_salary; next=null; }
}
//class linked list class LinkedList{ private node head; private int length; //constructor public LinkedList(){ head=null; length=0; } //print function void print(){ node temp=head; while(temp!=null){ System.out.println(temp.emp_id+" "+temp.emp_salary); temp=temp.next; } }
//insert element at the end void insertToEnd(String emp_id,String emp_salary){ node newnode=new node(emp_id,emp_salary); if(head==null){ newnode.next=head; head=newnode; } node temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=newnode; length++; }
//delete the tail of the previous list void deleteTail(){ if(head==null){ System.out.println("List is empty"); } else{ node temp=head,prev=null; if(head.next==null){ head=null; } else{ while(temp.next!=null){ prev=temp; temp=temp.next; } prev.next=null; } length--; } } //find the node by giving Employee id node find(String id){ if(head==null){ System.out.println("List is empty"); return head; } else{ node temp=head; while(temp!=null){ if(temp.emp_id.equals(id)){ return temp; } temp=temp.next; } System.out.println("Record not found"); return null; } } //size function int length(){ return length; }
}
//Driver program public class Employee{
public static void main(String []args){ LinkedList l1=new LinkedList(); l1.insertToEnd("00173","Sr4500"); l1.insertToEnd("02367","Sr5000"); l1.insertToEnd("00996","Sr5400"); l1.insertToEnd("00273","Sr6500"); l1.print(); l1.deleteTail(); l1.print(); node f1=l1.find("00173"); System.out.println(f1.emp_id+" "+f1.emp_salary); System.out.println("Length of the list is: "+l1.length());
} }
I need picture output for this cod
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
