Code for Linked List using Java

import java.io.*;
class Linked
{
int a;
Linked next;
Linked(int g)
{
a=g;
next=null;
}

void addNode(int g)
{
Linked temp,node;
temp=this;
while(temp.next!=null)
{
temp=temp.next;
}
node=new Linked(g);
temp.next=node;
}

void displ()
{
Linked temp;
temp=this;
while(temp!=null)
{
System.out.println(temp.a);
temp=temp.next;
}
}
}

class LinkedPrg
{
public static void main(String []at) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


int p;
char ch;
System.out.println("Enter values(less than 0 to exit) : ");
p=Integer.parseInt(br.readLine());
Linked ob=new Linked(p);
for(;;)
{
System.out.println("Enter values (less than 0 to exit) :  ");
p=Integer.parseInt(br.readLine());
if(p<0)
break;
ob.addNode(p);
}
System.out.println("Values in the linked list are");
ob.displ();
}
}

Comments

Popular posts from this blog

JAVA program to add two distance

Print Pattern using C