Skip to main content

Singly Linked List

 Single Linked List : In Single link list each node will have 2 parts , first part will have Data while next part will have a pointer which will keep the address of its next node, while node itself will not be aware of anything apart from its next node address.

Below is sample program without Tail. In this program appending an element to link list will have time complexity as O(n) as we need to travers till end of the link list to find out the last element in link list.

But if we will use Tail node along with Head then time complexity for adding an element will be O(n) but still insertion of a node after a specific node will be O(n).

Sample program without Tail node.

import java.io.IOException;
import java.util.Scanner;

class Node {

Node next = null;
int data;

public Node(int val) {
data = val;
}
}

class LinkList {

Node head = null;

public void append(int val) {
if (head == null) {
head = new Node(val);
} else {
Node n = head;
while (n.next != null) {
n = n.next;
}
n.next = new Node(val);
}
}

public void display(Node node) {
while (node != null) {
System.out.print(node.data+" ");
node = node.next;
}
}
}

public class LinkListImpl {

public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
LinkList linkList = new LinkList();

while (true) {

int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Eneter a element to add in link list");
int in = sc.nextInt();
linkList.append(in);
break;
case 2:
linkList.display(linkList.head);
break;

case 3:
System.out.println("BYE BYE");
System.exit(0);
default:
System.out.println("Wrong input : please focus here");

}
}

}

}


When we will have tail node then adding new element is very simple, just check if this is first node or not if this is first node then head and tail node will be same for first element, if not then just add new node to Tail next link part and move tail node from current node to newly added node.


Node head = null;
Node tail= null;

public void append(int val) {
if (head == null) {
head = new Node(val);
tail=head;
} else {
tail.next = new Node(val);
tail=tail.next;
}
}


Compete Singly linked list Program :


import java.io.IOException;
import java.util.Scanner;

class Node2 {

Node next = null;
int data;

public Node2(int val) {
data = val;
}
}

class LinkList2 {

Node head = null;
Node tail= null;

public void append(int val) {
if (head == null) {
head = new Node(val);
tail=head;
} else {
tail.next = new Node(val);
tail=tail.next;
}
}

public void delete(int val){

if(head.data==val){
head=null;
tail=null;
}
Node temp=head.next;
while(temp.next.data!=val){
temp=temp.next;
}
temp.next=temp.next.next;


}

public void appendAfterNode(int searchVal, int val) {

if (head == null) {
head = new Node(val);
tail = head;
}
Node temp =head;
while(temp.data!=searchVal) {
temp=temp.next;
}
Node temp2=temp.next;
temp.next = new Node(val);
temp.next.next=temp2;

}
public void display(Node node) {
while (node != null) {
System.out.print(node.data+" ");
node = node.next;
}
}
}

public class LinkListImplWithTail {

public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
LinkList2 linkList = new LinkList2();

while (true) {

int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Eneter a element to add in link list");
int in = sc.nextInt();
linkList.append(in);
break;
case 2:
linkList.display(linkList.head);
break;
case 3:
System.out.println("Eneter 2 element to add in link list after specific node");
int in2 = sc.nextInt();
int in3 = sc.nextInt();
linkList.appendAfterNode(in2,in3);
break;
case 4:
System.out.println("Eneter a element to delete from link list");
int in4=sc.nextInt();
linkList.delete(in4);
break;

case 5:
System.out.println("BYE BYE");
System.exit(0);
default:
System.out.println("Wrong input : please focus here");

}
}

}

}



Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT