Linked String Linear List Implementation

The Linked String Linear List Implementation assignment requires you to enter a given implementation and also enter a given test program.

Demos

init:
deps-jar:
compile-single:
run-single:

>>> testAddB
>>> Add red, yellow, blue, green, orange, purple.
( purple orange green blue yellow red )

>>> testAddE
>>> Add red, yellow, blue, green, orange, purple.
( red yellow blue green orange purple )

>>> testAddN
>>> Create ( one two three four five six )
( one two three four five six )

>>> testEmptyp
>>> a = ()
>>> b = (alligator)
a is empty
b is not empty

>>> testLength
>>> a = ()
>>> b = ( cat rat bat )
>>> display a and its length
( )
length of a = 0
>>> display b and its length
( cat rat bat )
length of b = 3

>>> testMemberp
>>> l = (red yellow blue green orange purple)
aqua is not in the list
blue is in the list
red is in the list
purple is in the list

>>> testNth
>>> l = ( un deux trois quatre cinq )
>>> display the elements in reverse order
cinq
quatre
trois
deux
un

>>> testDeleters
>>> test delete by content ...
>>> l = (red yellow blue green orange purple)
>>> display l
( red yellow blue green orange purple )
>>> delete purple and display the list
( red yellow blue green orange )
>>> delete green and display the list
( red yellow blue orange )
>>> delete red and display the list
( yellow blue orange )
>>> test delete by position ...
>>> l = (red yellow blue green orange purple)
>>> display l
( red yellow blue green orange purple )
>>> delete element 1 and display the list
( yellow blue green orange purple )
>>> delete element 3 and display the list
( yellow blue orange purple )
>>> delete element 4 and display the list
( yellow blue orange )
BUILD SUCCESSFUL (total time: 1 second)

Code

/*
 * StringLinearList.java
 *
 * Created on June 15, 2006, 6:43 AM
 */

package datatypes.list.string.linked;

import datatypes.list.string.*;

public class StringLinearList implements StringLinearListADT
{
    private String data;
    private StringLinearList link;
    
    public StringLinearList()
    {
        data = null;
    }
    
    private StringLinearList(String first, StringLinearList rest)
    {
        data = first;
        link = rest;
    }
    
    // referencers
    
    private String first()
    {
        return data;
    }
    
    private StringLinearList rest()
    {
        return link;
    }
    
    // additional referencers
    
    private String second()
    {
        return rest().first();
    }
    
    private String last()
    {
        if ( length() == 1 ) {
            return first();
        } else {
            return rest().last();
        }
    }
    
    // required methods
    
    public String toString()
    {
        return "( " + toStringHelper() + ")";
    }
    
    private String toStringHelper()
    {
        if ( emptyp() ) {
            return "";
        } else {
            return first().toString() + " " + rest().toStringHelper();
        }
    }
    
    public boolean emptyp()
    {
        return data == null;
    }
    
    public int length()
    {
        if ( emptyp() ) {
            return 0;
        } else {
            return 1 + rest().length();
        }
    }
    
    public String nth(int n)
    {
        if ( n == 1 ) {
            return first();
        } else {
            return rest().nth(n-1);
        }
    }
    
    public void addB(String s)
    {
        if ( emptyp() ) {
            data = s; link = new StringLinearList();
        } else {
            link = new StringLinearList(data,link);
            data = s;
        }
    }
    
    public void addE(String s)
    {
        if ( emptyp() ) {
            addB(s);
        } else {
            rest().addE(s);
        }
    }
    
    public void addN(String s, int n)
    {
        if ( n == 1 ) {
            rest().addB(s);
        } else {
            rest().addN(s,n-1);
        }
    }
    
    public boolean memberp(String s)
    {
        if ( emptyp() ) { 
            return false; 
        } else if ( first().equals(s) ) { 
            return true; 
        } else {
            return rest().memberp(s);
        }
    }
    
    // delete methods assume the element is there
    
    public void delete(String s)
    {
        if ( first().equals(s) ) {
            data = rest().first();
            link = rest().rest();
        } else {
            rest().delete(s);
        }
    }
    
    public void delete(int n)
    {
        if ( n == 1 ) {
            data = rest().first();
            link = rest().rest();
        } else {
            rest().delete(n-1);
        }
    }
       
}


/*
 * LinkedStringLinearListTest.java
 *
 * Created on June 15, 2006, 8:31 AM
 */

package testers;

import datatypes.list.string.linked.*;

class LinkedStringLinearListTest {
    
    static public void main(String args[]) {
        // note:  toString() is tested throughout the program
        testAddB();
        testAddE();
        testAddN();
        testEmptyp();
        testLength();
        testMemberp();
        testNth();
        testDeleters();
    }

    static private void testAddB() {
        System.out.println("\n>>> testAddB");
        StringLinearList b = createB();
        display(b);
    }

    static private void testAddE() {
        System.out.println("\n>>> testAddE");
        StringLinearList e = createE();
        display(e);
    }

    static private void testAddN() {
        System.out.println("\n>>> testAddN");
        StringLinearList n = createN();
        display(n);
    }

    static private StringLinearList createB() {
        System.out.println(">>> Add red, yellow, blue, green, orange, purple.");
        StringLinearList sll = new StringLinearList();
        sll.addB("red");
        sll.addB("yellow");
        sll.addB("blue");
        sll.addB("green");
        sll.addB("orange");
        sll.addB("purple");
        return sll;
    }

    static private StringLinearList createE() {
        System.out.println(">>> Add red, yellow, blue, green, orange, purple.");
        StringLinearList sll = new StringLinearList();
        sll.addE("red");
        sll.addE("yellow");
        sll.addE("blue");
        sll.addE("green");
        sll.addE("orange");
        sll.addE("purple");
        return sll;
    }

    static private StringLinearList createN() {
        System.out.println(">>> Create ( one two three four five six )");
        StringLinearList sll = new StringLinearList();
        sll.addB("one");
        sll.addN("five",1);
        sll.addN("six",2);
        sll.addN("three",1);
        sll.addN("four",2);
        sll.addN("two",1);
        return sll;
    }

    static private void testNth() {
        System.out.println("\n>>> testNth");
        StringLinearList l = new StringLinearList();
        l.addE("un");
        l.addE("deux");
        l.addE("trois");
        l.addE("quatre");
        l.addE("cinq");
        System.out.println(">>> l = ( un deux trois quatre cinq )");
        System.out.println(">>> display the elements in reverse order");
        for ( int i = l.length(); i >= 1; i-- ) {
            System.out.println(l.nth(i));
        }
    }

    static private void testEmptyp() {
        System.out.println("\n>>> testEmptyp");
        StringLinearList a = new StringLinearList();
        StringLinearList b = new StringLinearList();
        b.addE("alligator");
        System.out.println(">>> a = ()");
        System.out.println(">>> b = (alligator)");
        if ( a.emptyp() ) {
            System.out.println("a is empty");
        } else {
            System.out.println("a is not empty");
        }
        if ( b.emptyp() ) {
            System.out.println("b is empty");
        } else {
            System.out.println("b is not empty");
        }
    }

    static private void testLength() {
        System.out.println("\n>>> testLength");
        StringLinearList a = new StringLinearList();
        StringLinearList b = new StringLinearList();
        b.addE("cat");
        b.addE("rat");
        b.addE("bat");
        System.out.println(">>> a = ()");
        System.out.println(">>> b = ( cat rat bat )");
        System.out.println(">>> display a and its length");
        display(a);
        System.out.println("length of a = " + a.length());
        System.out.println(">>> display b and its length");
        display(b);
        System.out.println("length of b = " + b.length());
    }

    static private void testMemberp() {
        System.out.println("\n>>> testMemberp");
        StringLinearList l = new StringLinearList();
        l.addE("red");
        l.addE("yellow");
        l.addE("blue");
        l.addE("green");
        l.addE("orange");
        l.addE("purple");
        System.out.println(">>> l = (red yellow blue green orange purple)");
        if ( l.memberp("aqua") ) {
            System.out.println("aqua is in the list");
        } else {
            System.out.println("aqua is not in the list");
        }
        if ( l.memberp("blue") ) {
            System.out.println("blue is in the list");
        } else {
            System.out.println("blue is not in the list");
        }
        if ( l.memberp("red") ) {
            System.out.println("red is in the list");
        } else {
            System.out.println("red is not in the list");
        }
        if ( l.memberp("purple") ) {
            System.out.println("purple is in the list");
        } else {
            System.out.println("purple is not in the list");
        }
    }

    static private void testDeleters() {
        System.out.println("\n>>> testDeleters");
        testDeleteByContent();
        testDeleteByPosition();
    }
    
    static private void testDeleteByContent() {
        System.out.println(">>> test delete by content ...");
        StringLinearList l = new StringLinearList();
        l.addE("red");
        l.addE("yellow");
        l.addE("blue");
        l.addE("green");
        l.addE("orange");
        l.addE("purple");
        System.out.println(">>> l = (red yellow blue green orange purple)");
        System.out.println(">>> display l");
        display(l);
        System.out.println(">>> delete purple and display the list");
        l.delete("purple");
        display(l);
        System.out.println(">>> delete green and display the list");
        l.delete("green");
        display(l);
        System.out.println(">>> delete red and display the list");
        l.delete("red");
        display(l);
    }

    static private void testDeleteByPosition() {
        System.out.println(">>> test delete by position ...");
        StringLinearList l = new StringLinearList();
        l.addE("red");
        l.addE("yellow");
        l.addE("blue");
        l.addE("green");
        l.addE("orange");
        l.addE("purple");
        System.out.println(">>> l = (red yellow blue green orange purple)");
        System.out.println(">>> display l");
        display(l);
        System.out.println(">>> delete element 1 and display the list");
        l.delete(1);
        display(l);
        System.out.println(">>> delete element 3 and display the list");
        l.delete(3);
        display(l);
        System.out.println(">>> delete element 4 and display the list");
        l.delete(4);
        display(l);
    }

    static private void display(StringLinearList sll) {
        System.out.print(sll.toString() + "\n");
    }

}

Questions

  1. How is a string linear list stored in this implementation?
  2. Which operation tends to require more computation, addB or addE ?
  3. Would you say that the nth method is computationally efficient?