Created
March 8, 2021 14:19
-
-
Save pradhumgp/e638223fe8f21e900bc0f484b2fbbde4 to your computer and use it in GitHub Desktop.
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
package march06; | |
public class Employee { | |
public int id; | |
public String name; | |
public String positon; | |
public Employee(int id, String name, String positon) { | |
this.id = id; | |
this.name = name; | |
this.positon = positon; | |
} | |
@Override | |
public String toString() { | |
return "Employee{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
", positon='" + positon + '\'' + | |
'}'; | |
} | |
} |
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
package march06; | |
public class Main { | |
public static void main(String[] args) { | |
SinglyLinkedList obj = new SinglyLinkedList(); | |
obj.insertAtBegin(new Node(new Employee(5,"pra","HR"))); | |
obj.insertAtEnd(new Node(new Employee(8,"adi","CEO"))); | |
obj.insertAtBegin(new Node(new Employee(9,"kshitij","Team Manager"))); | |
obj.insertAtBegin(new Node(new Employee(10,"Anshuman","Chaprasi"))); | |
obj.print(); | |
obj.deleteAtBegin(); | |
obj.insertAtBegin(new Node(new Employee(62,"Anshuman","Head Chaprasi"))); | |
obj.print(); | |
} | |
} |
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
package march06; | |
public class Node { | |
public Node Next; | |
public Employee Data; | |
public Node(Employee Data){ | |
this.Next = null; | |
this.Data = Data; | |
} | |
} |
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
package march06; | |
public class SinglyLinkedList { | |
public Node head; | |
public SinglyLinkedList() { | |
this.head = null; | |
} | |
public void insertAtEnd(Node newNode) { | |
if (head == null) { | |
head = newNode; | |
} else { | |
Node temp = head; | |
while (temp.Next != null) { | |
temp = temp.Next; | |
} | |
temp.Next = newNode; | |
} | |
} | |
public void deleteAtBegin() { | |
if (head == null) { | |
System.out.println("Empty LinkedList"); | |
} else { | |
head = head.Next; | |
} | |
} | |
public void print() { | |
if (head == null) { | |
System.out.println("Empty LinkedList"); | |
} else { | |
Node temp = head; | |
while (temp != null) { | |
System.out.println(temp.Data); | |
temp = temp.Next; | |
} | |
System.out.println("==========================="); | |
} | |
} | |
public void insertAtBegin(Node newNode) { | |
if (head == null) { | |
head = newNode; | |
} else { | |
newNode.Next = head; | |
head = newNode; | |
} | |
} | |
public void deleteAtEnd() { | |
if (head == null) { | |
System.out.println("Empty linkedList"); | |
} else { | |
Node temp = head; | |
while (temp.Next.Next != null) { | |
temp = temp.Next; | |
} | |
temp.Next = null; | |
} | |
} | |
public boolean search(Node newNode) { | |
if (head == null) { | |
return false; | |
} else { | |
Node temp = head; | |
while (temp != null) { | |
if (temp.Data == newNode.Data) { | |
return true; | |
} | |
temp = temp.Next; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment