java 1 Answer(s) 2 years and 10 months ago
Posted in : Java Beginners
Develop a template for linked-list class along with its methods in Java.
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
java
java hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
java
java dear,
i want a field for date picker using java/java script
java
java create java program for delete and update the details,without using database, just a normal java program
java java online telephone directory i need coding for online telephone directory..by using java....pls help me
java
java how to edit text document by using java
then how to edit starting and ending of text document by using java
java
java different between java & core java
print("code sample
java java pattern code for a given words java pattern code for a given words pattern
java
java how can use sleep in java
which book learn of java language
java java How to set java Policy for applet using jdk 6
java
java why methods in java raise exceptions
Have a look at the following link:
Java Exceptions
java java code to search the nodes how to write the java code to search the nodes using routers
java
java different between java & core java
print("code sample
java
java 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
java
java 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
java
java 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
java
java 1.what type of inheritance supported in java.
2.what is the advantage of java over C++.
Hi Friend,
Java supports following... inheritance
Advantages of Java over C++:
1)Java is pure object oriented
java
java how to run applets in java
Hi,
In Java you can use Applet viewer to run the applet. Read more at Java Applet Viewer tutorial page.
Thanks
java
java what is javaJava is a programming language..., and business applications.
Advantages of Java:
* Java is simple, easy to design... programming languages.
* Java is object-oriented, that is used to build modular
Java Java What is the immediate superclass of the Dialog class