Advertisement
CastelShal

Untitled

Jan 2nd, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package edu;
  2. import java.util.*;
  3. class LLNode
  4. {
  5.     private int data;
  6.     private LLNode next;
  7.     public int getData() {
  8.         return data;
  9.     }
  10.     public void setData(int data) {
  11.         this.data = data;
  12.     }
  13.     public LLNode getNext() {
  14.         return next;
  15.     }
  16.     public void setNext(LLNode next) {
  17.         this.next = next;
  18.     }
  19.     public LLNode(int data) {
  20.         this.data = data;
  21.         this.next = null;
  22.     }
  23.    
  24.    
  25. }
  26.  
  27. public class LLDemo {
  28.     LLNode createLL(LLNode head,int data)
  29.     {
  30.         LLNode h= head;
  31.         LLNode p=h;
  32.         LLNode q=h;
  33.         LLNode newNode=null;
  34.         if(h==null)
  35.         {
  36.             h= new LLNode(data);
  37.         }
  38.         else
  39.         {
  40.             while (p!=null)
  41.             {
  42.                 q=p;
  43.                 p=p.getNext();
  44.             }
  45.             newNode=new LLNode(data);
  46.             q.setNext(newNode);
  47.         }
  48.         return h;
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         // TODO Auto-generated method stub
  53.        
  54.         LLNode head=null;
  55.         LLNode p=null;
  56.         LLNode q=null;
  57.         LLNode node1= new LLNode(20);
  58.         LLNode node2= new LLNode(20);
  59.         LLNode node3= new LLNode(20);
  60.         node2.setNext(node3);
  61.         node1.setNext(node2);
  62.         head=node1;
  63.         p=node1;
  64.         Scanner sc =  new Scanner (System.in);
  65.        
  66.         while(p!=null){
  67.             q=p;
  68.             System.out.println("Enter data");
  69.             q.setData(sc.nextInt());
  70.             p=p.getNext();
  71.         }
  72.         p=node1;
  73.         while(p!=null){
  74.             q=p;
  75.             System.out.println("-->"+q.getData());
  76.             p=p.getNext();
  77.  
  78.         }
  79.  
  80. }
  81. }
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement