The java code to create a Singly Linked List
1)First create a class which has two data members,
package singly_linked_list;
public class NODE
{
int data;
NODE next;//self pointer
}
2)Then utilize the above class by using reference variables as pointers and to insert the data create a instance of it. I have created a class i.e "AscendingLinkedList" this class will insert the data in a ascending order.
package singly_linked_list;
import java.util.Scanner;
public class AscendingLinkedList
{
NODE head = null;
public static void main(String[] args)
{
AscendingLinkedList n1 = new AscendingLinkedList();
boolean conti =true;
while(conti)
{
System.out.println("singly linked list pgm started");
System.out.println("enter your choice");
System.out.println("1.insert");
System.out.println("2.display");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice)
{
case 1: n1.insert();
break;
case 2: n1.display();
break;
default: System.exit(0);
break;
}
System.out.println("do you want to continue, if yes press 1 else 0");
int ch = sc.nextInt();
if(ch==1){conti = true;}else{conti = false;}
}//end of while
System.out.println("sinlgy linked list pgm ended");
}//end of main
public void insert()
{
System.out.println("enter the number to insert");
Scanner sc1 = new Scanner(System.in);
int number = sc1.nextInt();
NODE cur =null;
NODE new_node=null;
if(head==null)
{
System.out.println("The list is empty inserting the first element");
head =new NODE();
head.data= number;
head.next=null;
System.out.println("insertion successfull!!!!!!!!!");
}
else if(number<=head.data)
{
insert_front(number);
}
else
{
new_node = new NODE();
System.out.println("inserting at the rear");
cur = head;
while(cur.next!=null)
{
cur=cur.next;
}//end of while
cur.next = new_node;
new_node.data = number;
new_node.next = null;
System.out.println("insertion successfull!!!!!!!!!");
}
}//end of insert
public void insert_front(int number)
{
System.out.println("inserting at the front");
NODE cur =null;
NODE new_node=new NODE();
cur = head;
new_node.data = number;
new_node.next=cur;
head=new_node;
System.out.println("insertion successfull!!!!!!!!!");
}//end of inser_front
public void display()
{
NODE cur =null;
if(head.next==null)
{
System.out.print(head.data+"-->");
System.out.print("null");
}
else{
System.out.println("the content of the list are:");
cur=head;
while(cur.next!=null)
{
System.out.print(cur.data+"-->");
cur=cur.next;
}//end of while
System.out.print(cur.data+"-->");
System.out.println("null");
}//end of if_else
}//end of display
}//end of class Nodes
1)First create a class which has two data members,
package singly_linked_list;
public class NODE
{
int data;
NODE next;//self pointer
}
2)Then utilize the above class by using reference variables as pointers and to insert the data create a instance of it. I have created a class i.e "AscendingLinkedList" this class will insert the data in a ascending order.
package singly_linked_list;
import java.util.Scanner;
public class AscendingLinkedList
{
NODE head = null;
public static void main(String[] args)
{
AscendingLinkedList n1 = new AscendingLinkedList();
boolean conti =true;
while(conti)
{
System.out.println("singly linked list pgm started");
System.out.println("enter your choice");
System.out.println("1.insert");
System.out.println("2.display");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice)
{
case 1: n1.insert();
break;
case 2: n1.display();
break;
default: System.exit(0);
break;
}
System.out.println("do you want to continue, if yes press 1 else 0");
int ch = sc.nextInt();
if(ch==1){conti = true;}else{conti = false;}
}//end of while
System.out.println("sinlgy linked list pgm ended");
}//end of main
public void insert()
{
System.out.println("enter the number to insert");
Scanner sc1 = new Scanner(System.in);
int number = sc1.nextInt();
NODE cur =null;
NODE new_node=null;
if(head==null)
{
System.out.println("The list is empty inserting the first element");
head =new NODE();
head.data= number;
head.next=null;
System.out.println("insertion successfull!!!!!!!!!");
}
else if(number<=head.data)
{
insert_front(number);
}
else
{
new_node = new NODE();
System.out.println("inserting at the rear");
cur = head;
while(cur.next!=null)
{
cur=cur.next;
}//end of while
cur.next = new_node;
new_node.data = number;
new_node.next = null;
System.out.println("insertion successfull!!!!!!!!!");
}
}//end of insert
public void insert_front(int number)
{
System.out.println("inserting at the front");
NODE cur =null;
NODE new_node=new NODE();
cur = head;
new_node.data = number;
new_node.next=cur;
head=new_node;
System.out.println("insertion successfull!!!!!!!!!");
}//end of inser_front
public void display()
{
NODE cur =null;
if(head.next==null)
{
System.out.print(head.data+"-->");
System.out.print("null");
}
else{
System.out.println("the content of the list are:");
cur=head;
while(cur.next!=null)
{
System.out.print(cur.data+"-->");
cur=cur.next;
}//end of while
System.out.print(cur.data+"-->");
System.out.println("null");
}//end of if_else
}//end of display
}//end of class Nodes
3) Create an another class by name "DescendingLinkedList" that should insert in a descending order.
