All pastes #900570 Raw Edit

MyLinkedList

public java v1 · immutable
·published 2008-02-12 00:20 UTC
rendered paste body
/* * Write an algorithm for concatenating two singly link lists, L and M,  with header sentinels into a single list, N, that contains all the  nodes of L followed by all the nodes to M.    Hint:    Use a temporary node to walk to the end of list L. Then, make the    last element of L point to the first element of M as its “next” node.    Concatenate ()    Create a new node v    v = L.getHead()    while v! = null do    v = V.getNext()    end while    v.setNext(M.getHead())    N = L */package org.lurey.cs2b.assignments.linkedListAlgorithim;/** * @author Matan Lurey */public class LinkedListAlgorithim {    /**     * @param args the command line arguments     */    public static void main(String[] args)     {        System.out.println("Starting application...");                System.out.println("Creating new MyLinkedList(); list1");        MyLinkedList list1 = new MyLinkedList();        System.out.println("Adding 3 elements: 4, 5, and 6");        list1.add(4);        list1.add(5);        list1.add(6);                System.out.println("Printing contents of list1...");        list1.print();                System.out.println("Creating new MyLinkedList(); list2");        MyLinkedList list2 = new MyLinkedList();        System.out.println("Adding 3 elements: 1, 2, and 3");        list2.add(1);        list2.add(2);        list2.add(3);                System.out.println("Printing contents of list2...");        list2.print();                System.out.println("Concatenating list1 to the end of list2...");        list2.concat(list1);                System.out.println("Printing contents of list2...");        list2.print();    }}package org.lurey.cs2b.assignments.linkedListAlgorithim;import java.util.LinkedList;/** * Custom LinkedList class that supports a custom .concat() * method, which takes another linkedlist and imports all the nodes * at the end *  * @author Matan Lurey */public class MyLinkedList extends LinkedList {    /**     * Add's nodes to the end of the list     * @param list     */    public void concat(LinkedList list) {        int length = list.size();                for (int i=0; i<length; i++)            add(list.get(i));    }        /**     * Print's the contents of the array to the screen     */    public void print() {        int length = size();                System.out.println("Printing LinkedList (" + length + " elements)");                for (int i=0; i<length; i++)            System.out.println(get(i));                System.out.println("\n-- End of List --\n\n");    }}