You are here:   HomeJava ProgrammingJava ArrayDoubly Linked List in Java Example
by Dinesh Thakur Category: array

 

import java.io.*;

class node
{
      public int v;
      public node nxt;
      public node prv;
      public node (int x)
       {
          v=x;
       }
          public void dispval()
           {
              System.out.println(v);
          }
}
 class LinkList
 {
      private node first,p,last;
      public LinkList()
       {
          first=null;
          last=null;
       }
           public void insertval(int x)
             {
                  node p=new node(x);
                  p.nxt=null;
                  if(first==null)
                      {
                         first=p;
                         last=p;
                         p.prv=null;
                      }
                           else
                             {
                                 last.nxt=p;
                                 p.prv=last;
                                 last=p;
                             }
          }
     public void dispfifo()
        {
              System.out.println("Data in doubly linked list in fifo order");
              node r=first;
              while(r !=null)
                     {
                         r.dispval();
                         r=r.nxt;
                      }
       }
         public void displifo()
             {
                 System.out.println("Data in doubly linked list in lifo order");
                 node r=last;
                 while(r !=null)
                       {
                          r.dispval();
                          r=r.prv;
                        }
            }                                                                            
  }
   class DoublyLinkedList
      {
           public static void main(String args[])  throws IOException
           {
               LinkList k=new LinkList();
               BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
               String h;
               int n,i,m;
               System.out.println("How many numbers you want to insert in linked list");
               h=b.readLine();
               n=Integer.parseInt(h);
               System.out.println("Enter numericals in doubly linked list");
                for(i=1;i<=n;i++)
                    {
                        h=b.readLine();
                       m=Integer.parseInt(h);
                       k.insertval(m);
                    }
                        k.dispfifo();
                        k.dispfifo();
          }
     }

Doubly Linked List



What's New and Popular