Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


prabu
java
1 Answer(s)      4 years and 5 months ago
Posted in : Java Beginners


Develop a template for linked-list class along with its methods in Java.

Advertisement
View Answers

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:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com