import java.io.*;
import java.util.*;
class LinkedList
{
Node head;
static class Node
{
//LinkedList Variable
int no;
Node next;
//Constructors
Node(int i)
{
no = i;
next = null;
}
Node(int i,Node prevNode)
{
no = i;
next = null;
prevNode.next=this;
}
}
//Function to Create LL
public void createLL(int noOfNode) throws IOException
{
Scanner sc = new Scanner(System.in);
Node newNode;
while(noOfNode>0)
{
System.out.println("Enter Value :");
newNode= new Node(sc.nextInt());
this.addNodeAtLast(newNode);
noOfNode--;
}
}
//Function Which Insert Node at Last
public void addNodeAtLast(Node newNode)
{
if(this.head==null)
{
this.head = newNode;
}
else
{
Node first = this.head;
while(first.next != null)
{
first = first.next;
}
first.next = newNode;
}
}
//Function which Insert Node at First
public void addNodeAtFirst(Node newNode)
{
if(this.head == null)
{
this.head = newNode;
}
else
{
newNode.next = this.head;
head = newNode;
}
}
//Function Which Prints LL
public void printNodes()
{
Node first = this.head;
while(first.next!=null)
{
System.out.print(first.no+"->");
first = first.next;
}
System.out.print(first.no+"->");
}
//Function Which Prints LL in Reverese Order
public void printNodesInReverse(Node firstNode)
{
if(firstNode==null)
{
return;
}
//recursion Call
printNodesInReverse(firstNode.next);
System.out.print(firstNode.no+"->");
}
public static void main(String args[]) throws Exception
{
LinkedList l = new LinkedList();
l.createLL(3);
System.out.println("\nAfter Creating Linked List");
l.printNodes();
Node newNode = new Node(4);
l.addNodeAtLast(newNode);
System.out.println("\nAfter Inserting Node at Last in Linked List");
l.printNodes();
l.addNodeAtFirst(new Node(100));
System.out.println("\nAfter Inserting Node at First in Linked List");
l.printNodes();
System.out.println("\nLinked List in Reverse Order");
Node first=l.head;
l.printNodesInReverse(first);
}
}
import java.util.*;
class LinkedList
{
Node head;
static class Node
{
//LinkedList Variable
int no;
Node next;
//Constructors
Node(int i)
{
no = i;
next = null;
}
Node(int i,Node prevNode)
{
no = i;
next = null;
prevNode.next=this;
}
}
//Function to Create LL
public void createLL(int noOfNode) throws IOException
{
Scanner sc = new Scanner(System.in);
Node newNode;
while(noOfNode>0)
{
System.out.println("Enter Value :");
newNode= new Node(sc.nextInt());
this.addNodeAtLast(newNode);
noOfNode--;
}
}
//Function Which Insert Node at Last
public void addNodeAtLast(Node newNode)
{
if(this.head==null)
{
this.head = newNode;
}
else
{
Node first = this.head;
while(first.next != null)
{
first = first.next;
}
first.next = newNode;
}
}
//Function which Insert Node at First
public void addNodeAtFirst(Node newNode)
{
if(this.head == null)
{
this.head = newNode;
}
else
{
newNode.next = this.head;
head = newNode;
}
}
//Function Which Prints LL
public void printNodes()
{
Node first = this.head;
while(first.next!=null)
{
System.out.print(first.no+"->");
first = first.next;
}
System.out.print(first.no+"->");
}
//Function Which Prints LL in Reverese Order
public void printNodesInReverse(Node firstNode)
{
if(firstNode==null)
{
return;
}
//recursion Call
printNodesInReverse(firstNode.next);
System.out.print(firstNode.no+"->");
}
public static void main(String args[]) throws Exception
{
LinkedList l = new LinkedList();
l.createLL(3);
System.out.println("\nAfter Creating Linked List");
l.printNodes();
Node newNode = new Node(4);
l.addNodeAtLast(newNode);
System.out.println("\nAfter Inserting Node at Last in Linked List");
l.printNodes();
l.addNodeAtFirst(new Node(100));
System.out.println("\nAfter Inserting Node at First in Linked List");
l.printNodes();
System.out.println("\nLinked List in Reverse Order");
Node first=l.head;
l.printNodesInReverse(first);
}
}
Comments
Post a Comment