Elliott C. Back: Technology FTW!

SLL: Single Linked Lists

Posted in Code, Education by Elliott Back on September 1st, 2005.

An SLL (Singly Linked List) is a simple datastructure with the following format:

class SLL {
  public Object data;
  public SLL next;
}

This is sufficient to create a single list list. Imagine writing:

SLL a = new SLL();
SLL b = new SLL();
SLL c = new SLL();

a.next = b;
b.next = c;
c.next = null;

Then you’ll have a structure like:

  a       b       c
-----   -----   -----
|   | ->|   | ->|   |  -> null
-----   -----   ----- 

With a little code to assign data, you’ll have:

a.data = new Integer(1);
b.data = new Integer(2);
c.data = new Integer(3);

And the diagram becomes:

  a       b       c
-----   -----   -----
| 1 | ->| 2 | ->| 3 |  -> null
-----   -----   ----- 

So, a single linked list is a collection of containers that are linked in series. Each container contains a reference to the next container, and a reference to some data in memory somewhere. When “next” is null, then you are at the end of the list. Note that there is no way to traverse backwards, or find the head of the list, if you don’t already know it, so single linked lists (SLLs) usually are stored by just a reference to the first cell (head). From there, you can traverse and find any element.

This entry was posted on Thursday, September 1st, 2005 at 9:17 pm and is tagged with datastructure, linked lists, c data, containers, element, memory. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus