Linked List to fill in

Copy this and implement the insertInOrder() method.


package linkedlist;
import java.util.Random;

/**
 *
 * @author robertsonj
 */
public class LinkedList {
    
    Node head;

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        
        list.createDummyList();
        list.print();
        System.exit(0);
        
        
        Random r = new Random();
        for (int i = 0; i < 20; i++){
            list.insertInOrder(r.nextInt(1000));
        }
        list.print();
        // the list should be in ascending order
    }
    
    void createDummyList(){
        // this might be useful for testing your print method
        for (int i = 20; i > 0; i--){
            Node tmp = new Node();
            tmp.data = i;
            tmp.next = head;
            head = tmp;
        }
        
    }
    
    void insertInOrder(int n){
        // You need to code this
    }
    
    void print(){
        // You need to code this
        Node tmp = head;
        while(tmp != null){
            System.out.println(tmp.data);
            tmp = tmp.next;
        }
    }
}

class Node{
    // blah data is int
    int data;
    Node next;
    
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s