import java.util.Scanner; class Node { int data; Node next; } //**********************LinkedList Class**********************// public class LinkedList { Node head; public void insert(int d) { Node temp=new Node(); temp.data=d; temp.next=null; if(head==null) { head=temp; } else { Node n; n=head; while(n.next!=null) { n=n.next; } n.next=temp; } } public void show() { Node s; s=head; while(s.next!=null) { System.out.println(s.data); s=s.next; } System.out.println(s.data); } } //*********************Driver Class***********************// import java.util.Scanner; public class Driver { public static void main(String []args) { Scanner s=new Scanner(System.in); LinkedList l=new LinkedList(); int x; do{ System.out.println("Enter number in Linkedlist:"); int n=s.nextInt(); l.insert(n); System.out.println("Do you want to enter another number(1=yes): "); x=s.nextInt(); }while(x==1); System.out.println("The numbers you entered in Linkedlist are:"); l.show(); } } import java.util.Scanner; class Node { int data; Node next; } //**********************LinkedList Class**********************// public class LinkedList { Node head; public void insert(int d) { Node temp=new Node(); temp.data=d; temp.next=null; if(head==null) { head=temp; } else { Node n; n=head; while(n.next!=null) { n=n.next; } n.next=temp; } } public void show() { Node s; s=head; while(s.next!=null) { System.out.println(s.data); s=s.next; } System.out.println(s.data); } } //*********************Driver Class***********************// import java.util.Scanner; public class Driver { public static void main(String []args) { Scanner s=new Scanner(System.in); LinkedList l=new LinkedList(); int x; do{ System.out.println("Enter number in Linkedlist:"); int n=s.nextInt(); l.insert(n); System.out.println("Do you want to enter another number(1=yes): "); x=s.nextInt(); }while(x==1); System.out.println("The numbers you entered in Linkedlist are:"); l.show(); } } |
0 comments:
Post a Comment