Develop a template for linked-list class along with its methods in Java.
July 23, 2010 at 12:43 PM
PROGRAM TO IMPLEMENT LINKED LIST
Implementation File:
import java.io.*;
class node /*Class to create a node like structure*/
{
int data;
Object nxt;
}
class linkedlist /*Class that contain the set functions for implementation*/
{
node head;
linkedlist()
{
head=new node();
}
public void insertfirst() throws IOException
/*Function to insert a element in the begining*/
{
try
{
int i;
String s1;
node newnode=new node();
System.out.println("Input your data for your node");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s1=br.readLine();
i=Integer.parseInt(s1);
newnode.data=i;
newnode.nxt=head.nxt;
head.nxt=newnode;
System.out.print("Your node ");
System.out.print(newnode.data);
System.out.println(" is inserted at first");
}
catch(Exception e)
{
System.out.println("Error check ur input");
}
}
public void insertlast() /*To insert an element in the last*/
{
try
{
node temp;
temp=head;
node newnode=new node();
int i;
String s1;
System.out.println("Input your data for your node");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s1=br.readLine();
i=Integer.parseInt(s1);
newnode.data=i;
while(temp.nxt!=null)
{
temp=(node)temp.nxt;
}
temp.nxt=newnode;
System.out.print("Your node ");
System.out.print(newnode.data);
System.out.println(" is inserted at last.");
}
catch(Exception e)
{
System.out.println(" Error check ur input");
}
}
public void insertafter() /*To insert an element after the specified one*/
{
try
{
int i,j;
String s1;
node newnode;
node temp;
System.out.println("Input your data for your node");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s1=br.readLine();
i=Integer.parseInt(s1);
temp=head;
newnode=new node();
newnode.data=i;
System.out.println("Input the data after which your node is to b inserted");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
s1=b.readLine();
j=Integer.parseInt(s1);
while(temp.nxt!=null)
{
if(temp.data==j)
{
newnode.nxt=temp.nxt;
temp.nxt=newnode;
System.out.print("Your node ");
System.out.print(newnode.data);
System.out.print(" is inserted after ");
System.out.println(temp.data);
break;
}
else
{
temp=(node)temp.nxt;
}
}
if(temp.data!=j)
{
System.out.println("Please enter a data present in the list");
}
}
catch(Exception e)
{
System.out.println("Error check ur input");
}
}
public void deletefirst() /*To delete the first element in the list*/
{
if(head.nxt==null)
{
System.out.println("The list is empty so insert data herafter before further deletion.");
}
else
{
node current;
current=head;
current=(node)current.nxt;
head=current;
System.out.print("The node ");
System.out.print(current.data);
System.out.println(" is deleted.");
}
}
public void deletelast() /*To delete the last element*/
{
if(head.nxt==null)
{
System.out.println("The list is empty so insert data herafter before further deletion.");
}
else
{
node current;
current=head;
node previous=new node();
while(current.nxt!=null)
{
previous=current; //during lastnode last but one will be stored in previous
current=(node)current.nxt; //last node is stored in current
}
previous.nxt=null; //last but one's pointer is made to null thus leaving lastnode
System.out.print("The node ");
System.out.print(current.data);
System.out.println(" is deleted.");
}
}
public void deleteafter() /*To delete the element after the one specified*/
{
try
{
String s1;
if(head.nxt==null)
{
System.out.println("The list is empty so insert data herafter before further deletion.");
}
else
{
int j;
node deleted;
node current;
current=head;
System.out.println("Input the data after which your node is to be deleted");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
s1=b.readLine();
j=Integer.parseInt(s1);
while(current.nxt!=null)
{
if(current.data==j)
{
deleted=(node)current.nxt; //link of the given data's node is placed in deleted
current.nxt=deleted.nxt; //link of the given data's node is made to point to the link of the node to be deleted
System.out.print("The node ");
System.out.print(deleted.data);
System.out.println(" is deleted");
break;
}
else {
current=(node)current.nxt;
}
}
System.out.println("Please enter a data present in the list");
}
}
catch(Exception e) {
System.out.println("Error check ur input");
}
}
public void display() /*To display the list of elements*/
{
node temp;
if (head.nxt==null)
{
System.out.println("Your list is empty so insert data ");
}
else
{
System.out.println("The list of elements are");
temp=head;
while(temp.nxt!=null)
{
temp =(node) temp.nxt;
System.out.println(temp.data);
}
}
}
}
Application File:
=============
import java.io.*;
//import linkedlist.*;
class llist
{
public static void main(String args[])
{
try
{
linkedlist f=new linkedlist();
String c;
char d;
int j=0;
do
{
System.out.println("Choice 1-Insert First");
System.out.println("Choice 2-Insert Last");
System.out.println("Choice 3-Delete First");
System.out.println("Choice 4-Delete Last");
System.out.println("Choice 5-Insert After");
System.out.println("Choice 6-Delete After");
System.out.println("Choice 7-Display");
System.out.println("Choose ur choice between 1 and 7");
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int flag=0;
while(flag==0)
{
c=bf.readLine();
try
{
j=Integer.parseInt(c);
flag=1;
}
catch(NumberFormatException e)
{
System.out.println("Incorrect input");
System.out.println("Choice 1-Insert First");
System.out.println("Choice 2-Insert Last");
System.out.println("Choice 3-Delete First");
System.out.println("Choice 4-Delete Last");
System.out.println("Choice 5-Insert After");
System.out.println("Choice 6-Delete After");
System.out.println("Choice 7-Display");
System.out.println("Choose ur choice between 1 and 7");
}
}
switch(j)
{
case 1:
f.insertfirst();
f.display();
break;
case 2:
f.insertlast();
f.display();
break;
case 3:
f.deletefirst();
f.display();
break;
case 4:
f.deletelast();
f.display();
break;
case 5:
System.out.println("Enter a element present in the list");
f.display();
f.insertafter();
f.display();
break;
case 6:
System.out.println("Enter a element present in the list");
f.display();
f.deleteafter();
f.display();
break;
case 7:
f.display();
break;
default:
System.out.println("Enter a choice between 1 and 7"); break;
}
System.out.println("do u want to continue");
BufferedReader bf1=new BufferedReader(new InputSreamReader(System.in));
d=(char)bf1.read();
}while(d=='y');
}
catch(Exception e)
{
System.out.println("Invalid input");
return;
}
}
}
Output:
c:\jdk1.4\bin\Linkedlist>javac linkedlist.java
c:\jdk1.4\bin\Linkedlist>javac llist.java
c:\jdk1.4\bin\Linkedlist>java llist
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
1
Input your data for your node
5
Your node 5 is inserted at first
The list of elements are
5
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
2
Input your data for your node
8
Your node 8 is inserted at last.
The list of elements are
5
8
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
2
Input your data for your node
9
Your node 9 is inserted at last.
The list of elements are
5
8
9
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
5
Enter a element present in the list
The list of elements are
5
8
9
Input your data for your node
6
Input the data after which your node is to b inserted
5
Your node 6 is inserted after 5
The list of elements are
5
6
8
9
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
3
The node 5 is deleted.
The list of elements are
6
8
9
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
4
The node 9 is deleted.
The list of elements are
6
8
do u want to continue
y
Choice 1-Insert First
Choice 2-Insert Last
Choice 3-Delete First
Choice 4-Delete Last
Choice 5-Insert After
Choice 6-Delete After
Choice 7-Display
Choose ur choice between 1 and 7
4
The node 8 is deleted.
The list of elements are
6
do u want to continue?
n
c:\jdk1.4\bin\Linkedlist>
Related Tutorials/Questions & Answers:
javajava diff bt core
java and
java Advertisements
java java why iterator in
java if we for loop
Java Java Whether
Java is pure object oriented Language
JAVAJAVA how the name came for
java language as "
JAVA javajava explain technologies are used in
java now days and structure
java javajava different between
java & core
java javajava is
java open source
javajava what is
java reflection
java java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
javawhat is the size of array in
java ? what is the size of array in
java ?
what is the mean of finalize in
java javajava why to set classpath in
java javajava RARP implementation using
java socket
javajava sample code for RARP using
java JavaJava how to do
java in command prompt
javajava how use
java method
javajava is
java purely object oriented language
javajava why multiple inheritance is not possible in
java javajava give a simple example for inheritance in
java javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava why to set classpath in
java java java What is ?static? keyword
javajava Does
java allows multiline comments
javajava Write a
java code to print "ABABBABCABABBA
javajava write a program in
java to acess the email
javajava send me
java interview questions
javajava what are
JAVA applications development tools
JavaJava Whether
Java is Programming Language or it is SOftware
javajava explain object oriented concept in
java java java difference between class and interface
JavaJava how to draw class diagrams in
java javajava write a
java program using filenotfoundexception
javajava how to edit text document by using
java
then how to edit starting and ending of text document by using
java javajava different between
java & core
java
print("code sample
java java how can use sleep in
java
which book learn of
java language
javajava hi im new to
java plz suggest me how to master
java....saifjunaid@gmail.com
java java How to set
java Policy for applet using jdk 6
javajava pattern code for a given words
java pattern code for a given words pattern
javajava dear,
i want a field for date picker using
java/
java script
javajava create
java program for delete and update the details,without using database, just a normal
java program
javajava why methods in
java raise exceptions
Have a look at the following link:
Java Exceptions
javajava code to search the nodes how to write the
java code to search the nodes using routers
javajava online telephone directory i need coding for online telephone directory..by using
java....pls help me
javajava different between
java & core
java
print("code sample
javajava how to invoke one chart
java file from another
java file
javajava how to prepare the
java
Hi Friend,
If you want to learn how to install
java, creating and running a
java program then go through the following links:
http://www.roseindia.net/
java/beginners/index.shtml
http
javajava
java swing
Swing is a principal GUI toolkit for the
Java programming language. It is a part of the JFC (
Java Foundation Classes), which is an API for providing a graphical user interface for
Java programs
javajava what is the need of
java if
java is not there what will happen... work unless you have
Java installed, and more are created every day.
Java... to scientific supercomputers, cell phones to the Internet,
Java is everywhere!
http