What is a SinglyLinkedList?
A SinglyLinkedList is a List. It has any methods that you expect from a list such as add, clear, contains, get, ,indexOf, isEmpty, remove, set, size, toString, etc.
What is a major difference between the SinglyLinkedList and the ArrayList?
The ArrayList is implemented using array. The SinglyLinkedList is implemented using many smaller units called node.
What is the Node class?
The Node class is a class with two fields and the usual getter and setter methods.
E data stores a single element of the list
Node<E> next point to the immediate next Node
What is the SinglyLinkedList class?
The SinglyLinkedList class is a class with two fields and the usual constructors and methods.
Node<E> head point to the very first Node of the SinglyLinkedList. If the SinglyLinkedList is empty, then the head is null.
int size the size variable keep track of the number of elements in the SinglyLinkedList
Day 1
Your job today is to implement the Node class.
The following components of the SinglyLinkedList class:
Default constructor
Fields
void add(E value)
void clear()
boolean isEmpty()
int size()
Test Code
public class SinglyLinkedListTest{
public static void main(String[] args)
{
SinglyLinkedList<String> l = new SinglyLinkedList<String>();
System.out.println(l.size()); //should print 0
l.add("A");
System.out.println(l.size()); //should print 1
l.add("B");
System.out.println(l.size()); //should print 2
l.clear();
System.out.println(l.size()); //should print 0
}
}
How to throw an exception?
import java.lang.IndexOutOfBoundsException;
throw new IndexOutOfBoundsException("optional message");
Day 2
Your job today is to implement the following methods of the SinglyLinkedList class:
E get(int index)
int indexOf(E value)
E set(int index, E value)
String toString()
Test Code
public class SinglyLinkedListTest{
public static void main(String[] args)
{
SinglyLinkedList<String> l = new SinglyLinkedList<String>();
l.add("A");
l.add("B");
l.add("C");
System.out.println(l); //should print [A,B,C]
System.out.println(l.get(1)); //should print B
System.out.println(l.set(1,"D")); //should print B
System.out.println(l.get(1)); //should print D
System.out.println(l.indexOf("D")); //should print 1
System.out.println(l.indexOf("C")); //should print 2
System.out.println(l.indexOf("E")); //should print -1
System.out.println(l); //should print [A,D,C]
}
}
Day 3
Your job today is to implement the following methods of the SinglyLinkedList class:
void add(int index, E value)
E remove(int index)
Test Code
public class SinglyLinkedListTest{
public static void main(String[] args)
{
SinglyLinkedList<String> l = new SinglyLinkedList<String>();
l.add("A");
l.add("B");
l.add("C");
System.out.println(l); //should print [A,B,C]
System.out.println(l.remove(1)); //should print B
System.out.println(l); //should print [A,C]
System.out.println(l.remove(0)); //should print A
System.out.println(l.remove(0)); //should print C
l.add("A");
l.add(0,"B");
l.add(1,"C");
System.out.println(l); //should print [B,C,A]
}
}
If you are done with the above, please sign up for LeetCode.com and work on problems on https://leetcode.com/explore/learn/card/linked-list/209/singly-linked-list/
Challenge
Implement an enhanced for loop for your SinglyLinkedList.
Test Code
public class SinglyLinkedListTest{
public static void main(String[] args)
{
SinglyLinkedList<String> l = new SinglyLinkedList<String>();
l.add("A");
l.add("B");
l.add("C");
for(String elem : l)
{
System.out.print(elem + " "); //should print A B C
}
}
}