Skip to content

Instantly share code, notes, and snippets.

@pethaniakshay
Created May 20, 2017 10:38
Show Gist options
  • Save pethaniakshay/7b615e95a21e89d07ca4b41e4bfc97fa to your computer and use it in GitHub Desktop.
Save pethaniakshay/7b615e95a21e89d07ca4b41e4bfc97fa to your computer and use it in GitHub Desktop.
Java Implementation of the Linked List Data Structure.
import java.util.Scanner;
public class LinkedList {
static Node First =new Node();
static Node current,last;
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int i=0;
System.out.println("Enter Any number: ");
while(i<6){
i++;
LinkedList.insert(sc.nextInt());
}
System.out.println("Elements of Linked List:");
LinkedList.displayList();
}
static void displayList(){
current = First;
while(current.next!=null){
current.display();
current=current.next;
}
}
static void insert(int data){
if(First.next == null){
First.n=data;
First.next= new Node();
current=First.next;
}
else{
current.n=data;
current.next= new Node();
current=current.next;
}
}
}
class Node{
int n=0;
Node previous=null ,next=null;
Node(){}
Node(int sn){
n = sn;
}
public void display(){
System.out.println(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment