Thursday, October 3, 2013

P3C3: Linked List

A linked list is a data structure which can link other data. E.g. a list of subjects, people's name etc. Linked list is a list of nodes i.e. data items. Each node consists a data part and a link part. The first node of linked list is pointed by a Header node which is also called an external node. Similarly, the last node's link part consists a NULL.

C Sharp can implement the linked list easily by using System.Collections.Generic class.

Source code

using System;
//used for linked list feature
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        LinkedList<string> linked = new LinkedList<string>();

        linked.AddFirst("Maths");
        linked.AddLast("Politics");
        linked.AddLast("Biology");
        linked.AddLast("Physics");
        linked.AddLast("Computing");

        foreach (var item in linked)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
}

OUTPUT

Maths
Politics
Biology
Physics
Computing

No comments:

Post a Comment