MorkaLork Development

Interesting stuff I've picked up over the years...

LinkedList

2009-09-28 22:54:05 | 322 views | linkedlist csharp linked list next previous addlast addafter addfirst addbefore

LinkedList<T>


The LinkedList is a generic collection in which every element knows it’s bordering elements. This picture might explain better than words:

A crappy picture I drew depicting a linked list

The blue digits are the collection indexers. The first box links only to its next neighbor since it has no previous (trying to fetch the previous will thus end with a null object). The second box links to both neighbors (with index 0 and 2) and the third only links to it’s previous one.

The LinkedList collection can be useful if you want data where everything is interconnected. In the example further down we will create a small image gallery app where you can choose either the next or previous image. In that case the LinkedList collection is ideal.

Adding data to a LinkedList is a bit different than with a normal array. You start by adding a value, then you add data either in relation to that value or by defining which node it should conjunct with.

The first way:


class Program {

static void Main() {
LinkedList<String> myLinkedList = new LinkedList<string>();

myLinkedList.AddLast("World");
myLinkedList.AddLast("!");
myLinkedList.AddFirst("Hello ");
myLinkedList.AddFirst("Common phrase: ");

foreach (String s in myLinkedList)
Console.Write(s);

//OUTPUT:
//Common phrase: Hello World!

Console.Read();
}
}


The second way:


class Program {

static void Main() {
LinkedList<string> myLinkedList = new LinkedList<string>();

LinkedListNode<string> nodeA = new LinkedListNode<string>("A");
LinkedListNode<string> nodeB = new LinkedListNode<string>("B");
LinkedListNode<string> nodeC = new LinkedListNode<string>("C");

myLinkedList.AddLast(nodeB);

myLinkedList.AddBefore(nodeB, nodeA);

myLinkedList.AddAfter(nodeB, nodeC);

foreach (string s in myLinkedList)
Console.Write("{0} ", s);

//OUTPUT:
//A B C

Console.Read();
}
}




Practical Example, an image gallery




using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace MorkaForm {
public partial class Form1 : Form {

LinkedList<String> linkedList;
LinkedListNode<String> node;

public Form1() {
InitializeComponent();
this.Text = "MorkaLork LinkedList demo";

linkedList = new LinkedList<string>();
linkedList.AddLast(@"C:UsersmaffeluPicturesMorkaPicsSquareMorkaButton.png");
linkedList.AddLast(@"C:UsersmaffeluPicturesMorkaPicsRoundMorkaButton.png");
linkedList.AddLast(@"C:UsersmaffeluPicturesMorkaPicsCrazyMorkaButton.png");

//Get a node
node = linkedList.First;

//Set startup picture
pbMain.Image = Image.FromFile(node.Value);
}

private void btnPrevious_Click(object sender, EventArgs e) {
if (node.Previous != null) {
pbMain.Image = Image.FromFile(node.Previous.Value);
node = node.Previous;
}
}

private void btnNext_Click(object sender, EventArgs e) {
if (node.Next != null) {
pbMain.Image = Image.FromFile(node.Next.Value);
node = node.Next;
}
}

}
}


The application looks like this when it runs:


Opening screen:
Opening screen

Next image:
Next image

Next image:
Next image

Previous image:
Previous image


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.