Insert and delete Employee object from Singly LinkedList in java


In this Data structures tutorial we will learn what is Singly LinkedList in java with example, diagrams and program. We will learn how to implement your own Single LinkedList in java. We will learn how to insert and delete Employee object at first of Singly LinkedList in java. We will also learn complexity of inserting and deleting operations at first of Singly LinkedList in java. We will use generic implementation of LinkedList to achieve so.



Full Program/Example to insert and delete Employee object at first of Singly LinkedList in java >
class Employee {
   private String id;
   private String name;
  
   /**
   * Employee constructor
   */
   public Employee(String id, String name) { // constructor
          this.id = id;
          this.name = name;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + ", name=" + name + "]   ";
   }
  
}
/**
*Exception to indicate that LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException{
     public LinkedListEmptyException(){
       super();
     }
  
   public LinkedListEmptyException(String message){
       super(message);
     }  
}
/**
*Node class, which holds data and contains next which points to next Node.
*/
class Node<T> {
   public T data; // data in Node.
   public Node<T> next; // points to next Node in list.
   /**
   * Constructor
   */
   public Node(T data){
          this.data = data;
   }
   /**
   * Display Node's data
   */
   public void displayNode() {
          System.out.print( data + " ");
   }
}
/**
* Singly LinkedList class (Generic implementation)
*/
class LinkedList<T> {
   private Node<T> first; // ref to first link on list
   /**
   * LinkedList constructor
   */
   public LinkedList(){
          first = null;
   }
   /**
   * Insert New Node at first position
   */
   public void insertFirst(T data) {
          Node<T> newNode = new Node<T>(data); //Creation of New Node.
          newNode.next = first;   //newLink ---> old first
          first = newNode; //first ---> newNode
   }
   /**
   * Deletes first Node
   */
   public Node<T> deleteFirst()
   {
          if(first==null){ //means LinkedList in empty, throw exception.             
                 throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
          }
          Node<T> tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.
          first = first.next; // delete first Node (make first point to second node)
          return tempNode; // return tempNode (i.e. deleted Node)
   }
  
         
   /**
   * Display LinkedList
   */
   public void displayLinkedList() {
          System.out.print("Displaying LinkedList [first--->last]: ");
          Node<T> tempDisplay = first; // start at the beginning of linkedList
          while (tempDisplay != null){ // Executes until we don't find end of list.
                 tempDisplay.displayNode();
                 tempDisplay = tempDisplay.next; // move to next Node
          }
          System.out.println();
         
   }
}
 
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* Main class - To test LinkedList.
*/
public class SinglyLinkedListGenericExample {
   public static void main(String[] args) {
          LinkedList<Employee> linkedList = new LinkedList<Employee>(); // creation of Linked List
         
          linkedList.insertFirst(new Employee("11", "sam"));
          linkedList.insertFirst(new Employee("21", "amy"));
          linkedList.insertFirst(new Employee("59", "katy"));
          linkedList.insertFirst(new Employee("14", "sai"));
          linkedList.insertFirst(new Employee("39", "pat"));
          linkedList.displayLinkedList(); // display LinkedList
                      
          System.out.print("Deleted Nodes: ");
          Node<Employee> deletedNode = linkedList.deleteFirst(); //delete Node
          deletedNode.displayNode();                                 //display deleted Node.
          deletedNode = linkedList.deleteFirst();      //delete Node.
          deletedNode.displayNode();                                 //display deleted Node.
         
          System.out.println();// sysout used to format output
          linkedList.displayLinkedList(); //Again display LinkedList
         
         
         
   }
}
/*OUTPUT
Displaying LinkedList [first--->last]: Employee [id=39, name=pat] Employee [id=14, name=sai] Employee [id=59, name=katy] Employee [id=21, name=amy] Employee [id=11, name=sam]
Deleted Nodes: Employee [id=39, name=pat] Employee [id=14, name=sai]
Displaying LinkedList [first--->last]: Employee [id=59, name=katy] Employee [id=21, name=amy] Employee [id=11, name=sam]
*/


So in this Data structures tutorial we learned what is Singly LinkedList in java with example, diagrams and program. We learned how to implement your own Single LinkedList in java. We learned how to insert and delete Employee object at first of Singly LinkedList in java. We also learned complexity of inserting and deleting operations at first of Singly LinkedList in java. We used generic implementation of LinkedList to achieve so.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Stacks, Queues in java


Singly LinkedList implementations in java:-



Doubly LinkedList implementations in java:-



Some of the tricky and interesting Singly LinkedList implementations in java





Implement Stack, Queue using LinkedList.


No comments:

Post a Comment