Lnked lists-data structure using java(part-2)
step 1 : at first we have to create this "Employeelinkedlist" class for performing actions on linked lists .you can use any normal datatype also instead of using this specific class .
package linked_list;
public class Employeelinkedlist {
private Employeenode head;
private Employeenode tail;
public void addtofront(Employee_1 employee) {
Employeenode node = new Employeenode(employee);
node.setNext( head);
if(head== null) {
tail = node;
}
else {
head.setPrev(node);
}
head = node;
}
public void printlist() {
Employeenode current = head;
System.out.print("Head ->");
while(current != null) {
System.out.print(current);
System.out.print("<->");
current=current.getNext();
}
System.out.println("null");
}
}
package linked_list;
public class Employeelinkedlist {
private Employeenode head;
private Employeenode tail;
public void addtofront(Employee_1 employee) {
Employeenode node = new Employeenode(employee);
node.setNext( head);
if(head== null) {
tail = node;
}
else {
head.setPrev(node);
}
head = node;
}
public void printlist() {
Employeenode current = head;
System.out.print("Head ->");
while(current != null) {
System.out.print(current);
System.out.print("<->");
current=current.getNext();
}
System.out.println("null");
}
}
STEP 2 :- create "Employeenode" class for employees .
package linked_list;
public class Employeenode {
private Employee_1 employee;
private Employeenode next;
private Employeenode prev;
public Employeenode(Employee_1 employee) {
this.employee= employee;
}
/**
* @return the employee
*/
public Employee_1 getEmployee(){
return employee;
}
public void setEmployee(Employee_1 employee) {
this.employee = employee;
}
/**
* @return the next
*/
public Employeenode getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(Employeenode next) {
this.next = next;
}
/**
* @return the prev
*/
public Employeenode getPrev() {
return prev;
}
/**
* @param prev the prev to set
*/
public void setPrev(Employeenode prev) {
this.prev = prev;
}
public String toString() {
return employee.toString();
}
}
step 3:this is main class(for singly linked list) -
package linked_list;
public class Linked_list {
public static void main(String[] args) {
Employee_1 sundaram = new Employee_1( "Sundaram","dubey", 15);
Employee_1 saurabh = new Employee_1( "saurabh","dubey", 16);
Employee_1 tom = new Employee_1("tom", "cruise", 19);
Employee_1 messi = new Employee_1( "leonel", "messi", 20);
Employeelinkedlist list = new Employeelinkedlist();
list.addtofront(sundaram);
list.addtofront(saurabh);
list.addtofront(tom);
list.addtofront(messi);
list.printlist();
}
}
(class for doubly linked list)--
package linked_list;
public class Doubly {
public static void main(String[] args) {
Employee_1 sundaram = new Employee_1( "Sundaram","dubey", 15);
Employee_1 saurabh = new Employee_1( "saurabh","dubey", 16);
Employee_1 tom = new Employee_1("tom", "cruise", 19);
Employee_1 messi = new Employee_1( "leonel", "messi", 20);
Employeelinkedlist list = new Employeelinkedlist();
list.addtofront(messi);
list.addtofront(tom);
list.addtofront(saurabh);
list.addtofront(sundaram);
list.printlist();
}
}
(use of generics)JDK SUPPORT for linked lists :--
package linked_list;
import java.util.Iterator;
import java.util.LinkedList;
public class Linkedlist_JDK {
public static void main(String[] args) {
Employee_1 sundaram = new Employee_1( "Sundaram","dubey", 15);
Employee_1 saurabh = new Employee_1( "saurabh","dubey", 16);
Employee_1 tom = new Employee_1("tom", "cruise", 19);
Employee_1 messi = new Employee_1( "leonel", "messi", 20);
Employee_1 bill = new Employee_1("bill","gates",1);
LinkedList<Employee_1> list= new LinkedList<>();
list.addFirst(messi);
list.addFirst(tom);
list.addFirst(saurabh);
list.addFirst(sundaram);
Iterator<Employee_1> iter = list.iterator();
while(iter.hasNext()) {
System.out.print(iter.next());
System.out.print("<->");
}System.out.println("null");
list.addLast(bill);//add is used for adding an element in the end of the list
iter = list.iterator();
while(iter.hasNext()) {
System.out.print(iter.next());
System.out.print("<->");
}
System.out.println("null");
list.removeLast();
iter = list.iterator();
while(iter.hasNext()) {
System.out.print(iter.next());
System.out.print("<->");
}
System.out.println("null");
}
}
--this is a whole overview of linked lists , hope you like it follow for more .
@mazerunner
Comments