YCP Logo Assignment 7: Singly-linked list insertion

Due: Tuesday, December 9th by 11:59 PM

Getting Started

Use your solution to Lab 21 as a starting point. Make a copy of that project, renaming it to CS201_Assign7.

Your Task

Add the following method to the LinkedList class:

public void add(int index, E value)

This method inserts a new element value into the list at the specified index. Any existing values at the specified index or to the right of the specified index are shifted one position to the right.

Add the following methods to the LinkedListTest JUnit test class to test your implementation of the add(int, E) method:

public void testInsert() throws Exception {
        names.add(0, "Albert");

        assertEquals(5, names.size());
        assertEquals("Albert", names.get(0));
        assertEquals("Alice", names.get(1));
        assertEquals("Bob", names.get(2));
        assertEquals("Carl", names.get(3));
        assertEquals("Delores", names.get(4));

public void testInsert2() throws Exception {
        names.add(1, "Bjorn");
        names.add(1, "Barbara");

        assertEquals(6, names.size());
        assertEquals("Alice", names.get(0));
        assertEquals("Barbara", names.get(1));
        assertEquals("Bjorn", names.get(2));
        assertEquals("Bob", names.get(3));
        assertEquals("Carl", names.get(4));
        assertEquals("Delores", names.get(5));

public void testInsert3() throws Exception {
        names.add(4, "Egbert");

        assertEquals(5, names.size());
        assertEquals("Alice", names.get(0));
        assertEquals("Bob", names.get(1));
        assertEquals("Carl", names.get(2));
        assertEquals("Delores", names.get(3));
        assertEquals("Egbert", names.get(4));

        assertEquals(6, names.size());
        assertEquals("Egbert", names.get(4));
        assertEquals("Frances", names.get(5));


Insertion into a linked list is similar to removal: you will need a reference to the node which will become the predecessor of the newly-inserted node. Inserting at the beginning and ends of the list are special cases.

Drawing diagrams of the linked-list structure on paper helps greatly in visualizing how linked-list operations should work.


Export your project as a zip file (File->Export...->Archive File) and upload it to the submission server as assign7. The URL of the server is