Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

single linked list in java

import java.util.Scanner;

public class SLL {
  class Node{
	  int data;
	  Node next;
  public Node(int data) {
	  this.data = data;    
      this.next = null;   
  }}
  
  
  
  
  
  
  
  
  public Node head = null;   
  
  
  
  
  public void addnodeatpositon(int data,int n) {
	  Node newNode = new Node(data);
	  int i=1;
	  newNode.data=data;
	  Node current=head;

	  while(i<(n-1)) {
       current=current.next;
       i++;
       }
	   newNode.next=(current.next);
	  current.next=newNode;
  }
  
  public void deleteatposition(int n) {
	  
	  int i=1;
	  Node current=head;

	  while(i<(n-1)) {
       current=current.next;
       i++;
       }
	  current.next=(current.next).next;
	  
	  
  }
 
  public void addNode(int data) {    
      
      Node newNode = new Node(data);    
      
      if(head == null) {     
          head = newNode;    
      }    
      else { 
          newNode.next=head;
          head=newNode;
      }    
  }    
   
  public void delete(){
	      
	      if(head == null) {     
	          System.out.println("List is Empty");
	          
	      }else {
	    	  
	    	  head=head.next;
	      }
	   
   }
  
  public void displayreverseorder() {    
	      
	      Node current = head;    
	          
	      if(head == null) {    
	          System.out.println("List is empty");    
	          return;    
	      }    
	      System.out.println("Nodes of singly linked list: ");    
	      while(current != null) {    
	         
	          System.out.print(current.data + " ");    
	          current = current.next;    
	      }    
	      System.out.println();    
	  }  
  
  public void search(int key) {
	  Node current=head;
	  while (current.next!=null) {
		  if(current.data==key)
		  {	  System.out.println("Item is found");
			  break;
	  }	  current= current.next;

	 if(current.next==null)
	   System.out.println("Item is not found");

		  
	  }
  }

	  
  
  
      
  public static void main(String[] args) {    
          
      SLL list=new SLL();
          
   list.addNode(7);
   list.addNode(6);
   list.addNode(5);
   list.addNode(3);
   list.addNode(2);
   list.addNode(1);

   list.displayreverseorder();
   
   list.addnodeatpositon(8,4);

   list.displayreverseorder();
   list.deleteatposition(4);

   list.displayreverseorder();
  }    
}   //This code is contributed by Pronay(Bangladesh)
Comment

PREVIOUS NEXT
Code Example
Java :: binary tree level traversal 
Java :: treeset java descending order using comparator 
Java :: cannot find symbol iterator in java 
Java :: javac encoding utf-8 
Java :: functional interface java 
Java :: javafx get actionevent id 
Java :: check if two lists are equal java 
Java :: treemap java entryset 
Java :: java inner function 
Java :: how to print all package in java 
Java :: java string reduce 
Java :: java set font 
Java :: how use a if in java 
Java :: spring security antmatchers id 
Java :: hangfire list recurring jobs 
Java :: spring logback configuration 
Java :: seek bar 
Java :: how to change background color of grid in grid layout in android 
Java :: How do I make my Android app scrollable? 
Java :: java packages example 
Java :: sending a excel in an attachment in email java 
Java :: java int 0/0 
Java :: observer design pattern 
Java :: como apanhar caracter de uma string em java 
Java :: next line java does not take input 
Java :: multiple inheritance in java 
Java :: each loop in java 
Java :: splash full screen android 
Java :: list of list in java 
Java :: java spring mvc 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =