Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: request

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default request

    • implement doubly linked list and include a method to insert new node in the middle of the list
    • implement singly linked list and include a method to check whether two single linked lists have the same contents

    please help me in these program..


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: request

    Quote Originally Posted by aegr View Post
    • implement doubly linked list and include a method to insert new node in the middle of the list
    • implement singly linked list and include a method to check whether two single linked lists have the same contents

    please help me in these program..
    These are assignment requirements and not a forum question. Surely no one is going go do your work for you, nor should they, but at present, it is quite hard to figure out exactly how to help you until you ask a specific question or two.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    aegr (March 24th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    Quote Originally Posted by curmudgeon View Post
    These are assignment requirements and not a forum question. Surely no one is going go do your work for you, nor should they, but at present, it is quite hard to figure out exactly how to help you until you ask a specific question or two.
    i tried for the first question
    but i have errors
    can you help me please??
    PHP Code:
    import java.util.Random;
     
    public class 
    DLinkedList {

    private class 
    Node {
    int item;
    Node next;
    Node previous;
    // protected Node top;
     
    }

    private 
    Node head null;

    public 
    void Insert(int item) {
    if( 
    head==null){
    head = new Node();
    head.item item;
    head.next head;
    head.previous head;
    }else{
    Node newNode = new Node();
    newNode.item item;
    Node tail head.previous;
    tail.next newNode;
    newNode.previous tail;
    newNode.next head;
    head.previous newNode;
    }
    }
    public 
    boolean insertAfter(int keyint item) {
       
    Node first=null;
      
    Node last=null;
        
    Node current first;
        while (
    current.item !=key) {
          
    current current.next;
          if (
    current == null){
            return 
    false;
          }
        }
       
    Node newNode = new Node();

        if (
    current == last) {
          
    newNode.next null;
         
    last newNode;
       } else {
          
    newNode.next current.next;

          
    current.next.previous newNode;
        }
        
    newNode.previous current;
        
    current.next newNode;
        return 
    true;
      }
    public 
    int count() {
    if( 
    head==null){
    return 
    0;
    }
    Node node head;
    int count 0;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    count++;
    }else{
    break;
    }
    }
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    //Node node = head.previous.next;
        
    int i=1;
    while(
    <= count/2){
    node node.next;
    i++;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return 
    count;
    }

    public 
    int getLargest() {
    //number below is the bottom negative range of integer
    int largest = -2147483648;
    if( 
    head==null){
    return 
    largest;
    }
    Node node head;
    largest head.item;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    if( 
    node.item largest){
    largest node.item;
    }
    }else{
    break;
    }
    }
    return 
    largest;
    }

    public 
    void print(){
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    Node node head.previous.next;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }


        
    public static 
    void mainString[] args){
    DLinkedList list = new DLinkedList();
    Random random = new Random();

    //randomly add between 5 to 14 numbers to list
    for(  int i=0irandom.nextInt10)+5i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insertrandom.nextInt100));
    }

    list.print();
    System.out.println"There are " + list.count() + " items in the list");
    System.out.println"The larget number in the list is " + list.getLargest());
    list.
    insertAfter(random.nextInt(100), 77);

    }

    i can found the middle of the list
    but i cant insert new node

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: request

    What errors? Where?

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    when i try to use the insertafter function

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    aegr (March 24th, 2013)

  9. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    java.lang.NullPointerException
    at DLinkedList.insertAfter(DLinkedList.java:37)
    at DLinkedList.main(DLinkedList.java:148)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
    >

    --- Update ---

    PHP Code:
    import java.util.Random;
     
    public class 
    DLinkedList {

    private class 
    Node {
    int item;
    Node next;
    Node previous;

    }

    private 
    Node head null;


    public 
    void Insert(int item) {
    if( 
    head==null){
    head = new Node();
    head.item item;
    head.next head;
    head.previous head;
    }else{
    Node newNode = new Node();
    newNode.item item;
    Node tail head.previous;
    tail.next newNode;
    newNode.previous tail;
    newNode.next head;
    head.previous newNode;
    }
    }


    public 
    boolean insertAfter(int keyint item) {
       
    Node first=null;
      
    Node last=null;
        
    Node current first;
        while (
    current.item !=key) {
          
    current current.next;
          if (
    current == null){
            return 
    false;
          }
        }
       
    Node newNode = new Node();

        if (
    current == last) {
         
    newNode.next null;
         
    last newNode;
     } 
        else {
          
    newNode.next current.next;

          
    current.next.previous newNode;
        }
        
    newNode.previous current;
        
    current.next newNode;
        return 
    true;
     }


    public 
    int count() {
    if( 
    head==null){
    return 
    0;
    }
    Node node head;
    int count 0;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    count++;
    }else{
    break;
    }
    }
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    //Node node = head.previous.next;
        
    int i=1;
    while(
    <= count/2){
    node node.next;
    i++;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return 
    count;
    }



    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}

    public void print(){
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    Node node head.previous.next;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }


        
    public static 
    void mainString[] args){
    DLinkedList list = new DLinkedList();
    Random random = new Random();

    //randomly add between 5 to 14 numbers to list
    for(  int i=0irandom.nextInt10)+5i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insertrandom.nextInt100));
    }

    list.print();
    System.out.println"`the middle of list at item no. " + list.count()/);
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(random.nextInt(100), 77);

    }

    here is the code again

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    java.lang.NullPointerException
    at DLinkedList.insertAfter(DLinkedList.java:37)
    There is a variable with a null value on line 37. Look at line 37 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 37 and print out the values of all the variables on that line.

    The posted code is very hard to read because of its formatting.
    The posted code needs to be properly formatted. Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    PHP Code:
    [SIZE=4][LEFT]import java.util.Random;
     
    public class 
    DLinkedList {

    private class 
    Node {
    int item;
    Node next;
    Node previous;

    }

    private 
    Node head null;


    public 
    void Insert(int item) {
    if( 
    head==null){
    head = new Node();
    head.item item;
    head.next head;
    head.previous head;
    }else{
    Node newNode = new Node();
    newNode.item item;
    Node tail head.previous;
    tail.next newNode;
    newNode.previous tail;
    newNode.next head;
    head.previous newNode;
    }
    }


    public 
    boolean insertAfter(int keyint item) {
       
    Node first=null;
      
    Node last=null;
        
    Node current first;
        while (
    current.item !=key) {
          
    current current.next;
          if (
    current == null){
            return 
    false;
          }
        }
       
    Node newNode = new Node();

        if (
    current == last) {
         
    newNode.next null;
         
    last newNode;
     } 
        else {
          
    newNode.next current.next;

          
    current.next.previous newNode;
        }
        
    newNode.previous current;
        
    current.next newNode;
        return 
    true;
     }


    public 
    int count() {
    if( 
    head==null){
    return 
    0;
    }
    Node node head;
    int count 0;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    count++;
    }else{
    break;
    }
    }
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    //Node node = head.previous.next;
        
    int i=1;
    while(
    <= count/2){
    node node.next;
    i++;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return 
    count;
    }



    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}

    public void print(){
    if( 
    head==null){
    System.out.println" List is empty");
    }else{
    Node node head.previous.next;
    while( 
    true){
    node node.next;
    if( 
    node!=head){
    System.out.print( node.item " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }


        
    public static 
    void mainString[] args){
    DLinkedList list = new DLinkedList();
    Random random = new Random();

    //randomly add between 5 to 14 numbers to list
    for(  int i=0irandom.nextInt10)+5i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insertrandom.nextInt100));
    }

    list.print();
    System.out.println"`the middle of list at item no. " + list.count()/);
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(random.nextInt(100), 77);

    }
    }
    [/
    LEFT][/SIZE

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    Java code should be enclosed in code tags, not PHP tags.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    aegr (March 24th, 2013)

  14. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    [SIZE=4][LEFT]import java.util.Random;
     
    public class DLinkedList {
     
    private class Node {
    int item;
    Node next;
    Node previous;
     
    }
     
    private Node head = null;
     
     
    public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }
     
     
    public boolean insertAfter(int key, int item) {
       Node first=null;
      Node last=null;
        Node current = first;
        System.out.println( key);
        while (current.item !=key) {
     
          current = current.next;
          if (current == null){
            return false;
          }
        }
       Node newNode = new Node();
     
        if (current == last) {
         newNode.next = null;
         last = newNode;
     } 
        else {
          newNode.next = current.next;
     
          current.next.previous = newNode;
        }
        newNode.previous = current;
        current.next = newNode;
        return true;
     }
     
     
    public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
        int i=1;
    while(i <= count/2){
    node = node.next;
    i++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }
     
     
     
    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}
     
    public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }
     
     
     
    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    Random random = new Random();
     
    //randomly add between 5 to 14 numbers to list
    for(  int i=0; i< random.nextInt( 10)+5; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert( random.nextInt( 100));
    }
     
    list.print();
    System.out.println( "`the middle of list at item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(random.nextInt(100), 77);
     
    }
    }
    [/LEFT][/SIZE]



    sorry for disturbance
    i want to insert after the middle node

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    That's the right tags to use,
    the next thing to fix:
    The posted code is very hard to read because of its formatting.
    The posted code needs to be properly formatted. Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    [SIZE=4][FONT=Microsoft Sans Serif][INDENT][INDENT]import java.util.Random;
     
    public class DLinkedList {
     
    private class Node {
    int item;
    Node next;
    Node previous;
     
    }
     
    private Node head = null;
     
     
    public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }
     
     
    public boolean insertAfter(int key, int item) {
       Node first=null;
      Node last=null;
        Node current = first;
      //  System.out.println( key);
        while (current.item !=key) {
     
          current = current.next;
          if (current == null){
            return false;
          }
        }
       Node newNode = new Node();
     
        if (current == last) {
         newNode.next = null;
         last = newNode;
     } 
        else {
          newNode.next = current.next;
     
          current.next.previous = newNode;
        }
        newNode.previous = current;
        current.next = newNode;
        return true;
     }
     
     
    public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
        int i=1;
    while(i <= count/2){
    node = node.next;
    i++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }
     
     
     
    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}
     
    public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }
     
     
     
    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    //Random random = new Random();
     
    //randomly add between 5 to 14 numbers to list
    for(  int i=1; i<10; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert(i);
    }
     
    list.print();
    System.out.println( "`the middle of list after item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(5, 77);
    list.print();
    }
    }[/INDENT][/INDENT][/FONT][/SIZE]

  17. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    The code still needs:
    The posted code is very hard to read because of its formatting.
    The posted code needs to be properly formatted. Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    import java.util.Random;

    public class DLinkedList {

    private class Node {
    int item;
    Node next;
    Node previous;

    }

    private Node head = null;


    public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }


    public boolean insertAfter(int key, int item) {
    Node first=null;
    Node last=null;
    Node current = first;
    // System.out.println( key);
    while (current.item !=key) {

    current = current.next;
    if (current == null){
    return false;
    }
    }
    Node newNode = new Node();

    if (current == last) {
    newNode.next = null;
    last = newNode;
    }
    else {
    newNode.next = current.next;

    current.next.previous = newNode;
    }
    newNode.previous = current;
    current.next = newNode;
    return true;
    }


    public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
    int x=1;
    while(x <= count/2){
    node = node.next;
    x++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }



    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}

    public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }



    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    //Random random = new Random();

    //randomly add between 5 to 14 numbers to list
    for(int i=1; i<10; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert(i);
    }

    list.print();
    System.out.println( "`the middle of list after item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(5, 77);
    list.print();
    }
    }

    --- Update ---

    is it clear now??

  19. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    Now the code tags are missing.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    what can i do??

  21. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    Add the code tags like you used in post#13

    Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #19
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    [INDENT][INDENT][SIZE=5]import java.util.Random;
     
    [INDENT][INDENT]public class DLinkedList {
     
    private class Node {
    int item;
    Node next;
    Node previous;
     
    }[/INDENT][/INDENT]
     
    private Node head = null;
     
     
    [QUOTE][INDENT][INDENT]public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }
    [/INDENT][/INDENT][/QUOTE]
    [INDENT][INDENT][QUOTE]public boolean insertAfter(int key, int item) {
       Node first=null;
      Node last=null;
        Node current = first;
      //  System.out.println( key);
        while (current.item !=key) {
     
          current = current.next;
          if (current == null){
            return false;
          }
        }[/QUOTE][/INDENT][/INDENT]
       [INDENT][INDENT][QUOTE]Node newNode = new Node();
     
        if (current == last) {
         newNode.next = null;
         last = newNode;
     } 
        else {
          newNode.next = current.next;
     
          current.next.previous = newNode;
        }
        newNode.previous = current;
        current.next = newNode;
        return true;
     }
    [/QUOTE][/INDENT][/INDENT]
     
    [INDENT][INDENT][QUOTE]public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
        int x=1;
    while(x <= count/2){
    node = node.next;
    x++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }
     
    [/QUOTE][/INDENT][/INDENT]
     
    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}
     
    [INDENT][INDENT][QUOTE]public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }
     
     
     
    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    //Random random = new Random();
     
    //randomly add between 5 to 14 numbers to list
    for(int i=1; i<10; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert(i);
    }
     
    list.print();
    System.out.println( "`the middle of list after item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(5, 77);
    list.print();
    }
    }[/QUOTE][/INDENT][/INDENT]
    [/SIZE][/INDENT][/INDENT]


    --- Update ---

    are there any another method to put my code??

    --- Update ---

    [INDENT][INDENT]import java.util.Random;
     
    public class DLinkedList {
     
    private class Node {
    int item;
    Node next;
    Node previous;
     
    }
     
    private Node head = null;
     
     
    public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }
     
     
    public boolean insertAfter(int key, int item) {
       Node first=null;
      Node last=null;
        Node current = first;
      //  System.out.println( key);
        while (current.item !=key) {
     
          current = current.next;
          if (current == null){
            return false;
          }
        }
       Node newNode = new Node();
     
        if (current == last) {
         newNode.next = null;
         last = newNode;
     } 
        else {
          newNode.next = current.next;
     
          current.next.previous = newNode;
        }
        newNode.previous = current;
        current.next = newNode;
        return true;
     }
     
     
    public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
        int x=1;
    while(x <= count/2){
    node = node.next;
    x++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }
     
     
     
    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}
     
    public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }
     
     
     
    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    //Random random = new Random();
     
    //randomly add between 5 to 14 numbers to list
    for(int i=1; i<10; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert(i);
    }
     
    list.print();
    System.out.println( "`the middle of list after item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(5, 77);
    list.print();
    }
    }[/INDENT][/INDENT]


    --- Update ---

    ah..is it clear now??
    i'm so tired..

  23. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    The code still needs to be edited and formatted properly:

    Statements nested inside {}s need to be indented 3-4 spaces.
    The statements should NOT all start in the left column.


    There are no tags to do that. You (or your IDE) must insert spaces to make the indentations.

    There is one section of the code that is formatted: the insertAfter() method.
    The rest of the code needs to look like the code in that method.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #21
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    [INDENT][INDENT][INDENT]import java.util.Random;
     
    public class DLinkedList {
     
    private class Node {
    int item;
    Node next;
    Node previous;
     
    }
     
    private Node head = null;
     
     
    public void Insert(int item) {
    if( head==null){
    head = new Node();
    head.item = item;
    head.next = head;
    head.previous = head;
    }else{
    Node newNode = new Node();
    newNode.item = item;
    Node tail = head.previous;
    tail.next = newNode;
    newNode.previous = tail;
    newNode.next = head;
    head.previous = newNode;
    }
    }
     
     
    public boolean insertAfter(int key, int item) {
       Node first=null;
      Node last=null;
        Node current = first;
      //  System.out.println( key);
        while (current.item !=key) {
     
          current = current.next;
          if (current == null){
            return false;
          }
        }
       Node newNode = new Node();
     
        if (current == last) {
         newNode.next = null;
         last = newNode;
     } 
        else {
          newNode.next = current.next;
     
          current.next.previous = newNode;
        }
        newNode.previous = current;
        current.next = newNode;
        return true;
     }
     
     
    public int count() {
    if( head==null){
    return 0;
    }
    Node node = head;
    int count = 0;
    while( true){
    node = node.next;
    if( node!=head){
    count++;
    }else{
    break;
    }
    }
    if( head==null){
    System.out.println( " List is empty");
    }else{
    //Node node = head.previous.next;
        int x=1;
    while(x <= count/2){
    node = node.next;
    x++;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    //System.out.println();
    }
    return count;
    }
     
     
     
    //public int getLargest() {
    //number below is the bottom negative range of integer
    //int largest = -2147483648;
    //if( head==null){
    //return largest;
    //}
    //Node node = head;
    //largest = head.item;
    //while( true){
    //node = node.next;
    //if( node!=head){
    //if( node.item > largest){
    //largest = node.item;
    //}
    //}else{
    //break;
    //}
    //}
    //return largest;
    //}
     
    public void print(){
    if( head==null){
    System.out.println( " List is empty");
    }else{
    Node node = head.previous.next;
    while( true){
    node = node.next;
    if( node!=head){
    System.out.print( node.item + " ");
    }else{
    break;
    }
    }
    System.out.println();
    }
    }
     
     
     
    public static void main( String[] args){
    DLinkedList list = new DLinkedList();
    //Random random = new Random();
     
    //randomly add between 5 to 14 numbers to list
    for(int i=1; i<10; i++){
    //random insert numbers between 0 and 100, not including 100
    list.Insert(i);
    }
     
    list.print();
    System.out.println( "the middle of list after item no. " + list.count()/2 );
    //System.out.println( "The larget number in the list is " + list.getLargest());
    list.insertAfter(5, 77);
    list.print();
    }
    }[/INDENT][/INDENT][/INDENT]

  25. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    I don't see any changes in the formatting. Too many statements start in the left column.

    BTW There is no [INDENT] tag.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #23
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    i changed all the font and formatting in my IDE

  27. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: request

    Please post properly formatted code.
    If you don't understand my answer, don't ignore it, ask a question.

  28. #25
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: request

    can i make it as attachment file

Page 1 of 2 12 LastLast

Similar Threads

  1. Http Request
    By nillson in forum Loops & Control Statements
    Replies: 3
    Last Post: March 20th, 2013, 06:42 PM
  2. xml request in java code
    By ridg18 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 9th, 2012, 08:39 AM
  3. Clueless on request.getContextPath()
    By ram.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 29th, 2011, 12:07 PM