Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu;
- import java.util.*;
- class LLNode
- {
- private int data;
- private LLNode next;
- public int getData() {
- return data;
- }
- public void setData(int data) {
- this.data = data;
- }
- public LLNode getNext() {
- return next;
- }
- public void setNext(LLNode next) {
- this.next = next;
- }
- public LLNode(int data) {
- this.data = data;
- this.next = null;
- }
- }
- public class LLDemo {
- LLNode createLL(LLNode head,int data)
- {
- LLNode h= head;
- LLNode p=h;
- LLNode q=h;
- LLNode newNode=null;
- if(h==null)
- {
- h= new LLNode(data);
- }
- else
- {
- while (p!=null)
- {
- q=p;
- p=p.getNext();
- }
- newNode=new LLNode(data);
- q.setNext(newNode);
- }
- return h;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- LLNode head=null;
- LLNode p=null;
- LLNode q=null;
- LLNode node1= new LLNode(20);
- LLNode node2= new LLNode(20);
- LLNode node3= new LLNode(20);
- node2.setNext(node3);
- node1.setNext(node2);
- head=node1;
- p=node1;
- Scanner sc = new Scanner (System.in);
- while(p!=null){
- q=p;
- System.out.println("Enter data");
- q.setData(sc.nextInt());
- p=p.getNext();
- }
- p=node1;
- while(p!=null){
- q=p;
- System.out.println("-->"+q.getData());
- p=p.getNext();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement