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



Home Answers Viewqa Java-Interview-Questions java program developing
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


zohour al-zoubi
java program developing
1 Answer(s)      2 years and 7 months ago
Posted in : Java Interview Questions


how to write a java program that implementing a double link list and use it to insert a list of names and print them to screen.


Advertisement
View Answers

March 29, 2012 at 1:28 PM


 class DoublyLinkedNode {
  Object data;
  DoublyLinkedNode prev;
  DoublyLinkedNode next;

  public DoublyLinkedNode( ) { }
   public DoublyLinkedNode( Object data, DoublyLinkedNode prev, DoublyLinkedNode next ) {
    this.data = data;
    this.prev = prev;
    this.next = next;
  }
   public DoublyLinkedNode( Object data ) {
    this( data, null, null );
  }
}

public class DoublyLinkedList {

  DoublyLinkedNode head;
  DoublyLinkedNode tail;

  public DoublyLinkedList( ) { }
  public void insertAfter( DoublyLinkedNode node, DoublyLinkedNode newNode ) {
    newNode.prev = node;
    newNode.next = node.next;
    if( node.next == null ) {
      tail = newNode;
    }
    else {
      node.next.prev = newNode;
    }
    node.next = newNode;
  }

  public void insertBefore( DoublyLinkedNode node, DoublyLinkedNode newNode ) {
    newNode.prev = node.prev;
    newNode.next = node;
    if( node.prev == null ) {
      head = newNode;
    } else {
      node.prev.next = newNode;
    }
     node.prev = newNode;
  }
   public void insertFront( DoublyLinkedNode newNode ) {
     if( head == null ) {
      head = newNode;
      tail = newNode;
      newNode.prev = null;
      newNode.next = null;
     }
     else {
      insertBefore( head, newNode );
    }
  }
   public void insertEnd( DoublyLinkedNode newNode ) {
     if ( tail == null ) {
      insertFront( newNode );
    }
    else {
      insertAfter( tail, newNode );
    }
  }

  public void remove( DoublyLinkedNode node ) {
    if( node.prev == null ) {
      head = node.next; 
    }
    else {
      node.prev.next = node.next;
    }
    if( node.next == null ) {
      tail = node.prev; 
    } 
    else {
      node.next.prev = node.prev;
    }
  }
   public void removeFront( ) {
    if( head != null ) {
      remove( head );
    }
  }
   public void removeEnd( ) {
    if( tail != null ) {
      remove( tail );
    }
  }
   public DoublyLinkedNode get( Object data ) {
     for( DoublyLinkedNode cursor = head; cursor != null; cursor = cursor.next ) {
      if( cursor.data.equals( data ) ) {
        return cursor;
      }
    }
     return null;
  }
   public static void main( String[] args ) {
    DoublyLinkedList list = new DoublyLinkedList( );
    DoublyLinkedNode node1 = new DoublyLinkedNode( "A" );
    DoublyLinkedNode node2 = new DoublyLinkedNode( "B" );
    DoublyLinkedNode node3 = new DoublyLinkedNode( "C" );
    DoublyLinkedNode node4 = new DoublyLinkedNode( "D" );

    list.insertFront( node1 );
    list.insertEnd( node2 );
    list.insertEnd( node3 );
    list.insertEnd( node4 );
    for( DoublyLinkedNode i = list.head; i != null; i = i.next ) {
      System.out.println( i.data ); 
    } 
  }
}


Related Tutorials/Questions & Answers:
java program developing
java program developing  how to write a java program that implementing a double link list and use it to insert a list of names and print them to screen.    class DoublyLinkedNode { Object data; DoublyLinkedNode
Developing a program to test
Developing a program to test       Developing a program to test Given below program is to test the script : Qunit.html <
Advertisements
developing skills in java , j2ee - Java Beginners
developing skills in java , j2ee   How to understand or to feel the flow of java or j2ee programme what is the way to become a expert programmer can you please give me tips thanking you
Java How to Program
Java program on your computer and also how to write and run your first java... bytecode into machine language on the target processor. Java How to Program... steps of Java How to Program: Download and setup a Java Development environment
Developing Search Engine in Java
Developing Search Engine in Java   ... in Java technologies. We will be using Hibernate Search for developing the search engine. What is a Search Engine? A Search Engine is typically a program
Introduction To Enterprise Java Bean(EJB). Developing web component.
Developing web component  ...   Introduction To Java Beans... on the web server. Enterprise Bean consists of all the program
java program for
java program for   java program for printing documents,images and cards
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
Create First Program
program. For developing a java program you just need  a simple text editor like... to the directory ("C:\vinod" in this example) where your program is saved. Java... the "java" command to execute your program (Type java createfirstprogram
java program
java program  write a program to print 1234 567 89 10
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a program to create text area and display the various mouse handling events
java program
java program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
program in java
program in java  write a reverse program in java using string buffer.the input and out put as follows. input- hi good mornig out put-ih doog ginrom
Java program
Java program  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
java program
java program  . Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
java program
java program  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
 

 

 

DMCA.com