Singly LinkList in java

As mentioned in the previous post LinkList is a very vital data structure which are declared dynamically. Its of two types Singly and Doubly. Today we are going to discuss on singly link list. Singly Link list consists of two parts the value part and the reference part. The first part or the value part stores the value of node input by user. The second part or the reference part stores the referring value of the next node. This form of link list can be very useful to implement data structures such as stack, queues;etc.
     Here the first node is commonly known as the start node. It points to the first node storing values. The last node is commonly known as last. As it is the last node in the whole list so it doesn't have any node to point towards and hence it contains a null value in the reference part.
     In case of link list if asked in examination we need to just write the function because the programs are too long and hence just to check the caliber one is asked to write the function only.
     It has three basic operations:
  1. Adding
  2. Displaying
  3. Removing/Deleting 

Adding a new node:

   In case of add we first of all will take the input of the first node both its value as well as the reference value. And one important thing to note every next(reference) value must be initially declared with null then comes a do while loop which encounters with a 'k' value means it will take entry until the user wants to have the node to be generated. In the loop we will first take the entry of the info part then the reference will be declared with a null and will copy its initial reference value to the node added previously. After all entry we will declare a call function of it in the main part.
public static void create(link node){
        int k;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter the value of node:");
     node.info=sc.nextInt();
     node.next=null;
   do{
     link temp=new link();
     System.out.println("Enter the temp node:");
     temp.info=sc.nextInt();
     temp.next=null;
     node.next=temp;
     node=temp;
     System.out.println("Enter 0 to continue and 1 to exit:");
     k=sc.nextInt();
       }while(k==0);

Share this

Related Posts

Previous
« Prev Post