Moses Yap
:-) A happy IT Guy

C#: Creating a Singly Linked List using Generics

May 4, 2008 14:03 by mosesyap

This is more for exercise more than anything else.  By the way, in .NET 3.5, the System.Collection.Generics has a LinkedList<T> class that represents a doubly linked list. 

I wanted to test out some stuff in this practice.  Using an indexer and using an enumerator.  Here's the code I did.  I have not refactored the code so it might be a bit messy.  I am not going to change it as I don't have a use for it right now.

linklistdiagram

 

The code (Node Class):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkListConsole
{
    public class Node<T>
    {
        private T _Value;
        public T Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
            }
        }

        private Node<T> _Next;
        public Node<T> NextNode
        {
            get { return _Next; }
            set
            {
                _Next = value;
            }
        }

        public Node(T itemval)
        {
            _Value = itemval;
        }

        public Node(T itemval, Node<T> next)
        {
            _Value = itemval;
            _Next = next;
        }
    }
}

The code (LinkList Class):

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkListConsole
{
    public class LinkList<T>
    {
        private int _Count;
        public int Count
        {
            get { return _Count; }
            set
            {
                _Count = value;
            }
        }

        private Node<T> _Head;
        private Node<T> _Current;

        public T Value
        {
            get 
            {
                if (_Current != null)
                {
                    return _Current.Value;
                }
                else
                {
                    throw new Exception("There are no items in the list");
                }
            }
            set 
            { 
                if (_Current != null)
                {
                    _Current.Value = value; 
                }
                else
                {
                    throw new Exception("There are no items in the list");
                }
            }
        }

        public void MoveFirst()
        {
            if (_Head != null)
            {
                _Current = _Head;
            }
            else
            {
                throw new Exception("No record on first");
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            Node<T> temp = _Head;
            for (int i = 0; i < this._Count; i++)
            {
                yield return temp.Value;
                temp = temp.NextNode;
            }
        }

        public T this[int i]
        {
            get
            {
                if (i >= 0 && i < this._Count)
                {
                    Node<T> temp = _Head;
                    for (int j = 0; j < i; j++)
                    {
                        temp = temp.NextNode;
                    }
                    return temp.Value;
                }
                else
                {
                    throw new IndexOutOfRangeException("Index out of range");
                }
            }
            set
            {
                if (i >= 0 && i < this._Count)
                {
                    Node<T> temp = _Head;
                    for (int j = 0; j < i; j++)
                    {
                        temp = temp.NextNode;
                    }
                    temp.Value = value;
                }
                else
                {
                    throw new IndexOutOfRangeException("Index out of range");
                }
            }
        }


        public LinkList()
        {
            _Head = _Current = null;
            _Count = 0;
        }

        public LinkList(T head)
        {
            _Head = _Current = new Node<T>(head);
            _Count = 1;
        }

        public void Insert(T item)
        {
            if (_Current == null)
            {
                _Head = _Current = new Node<T>(item, null);
            }
            else
            {
                _Current = _Current.NextNode = new Node<T>(item, _Current.NextNode);
            }

            _Count++;
        }


        public T Next()
        {
            if (_Current.NextNode != null)
            {
                _Current = _Current.NextNode;
                return _Current.Value;
            }
            else
            {
                throw new Exception("No more nodes");
            }
        }

        public void Transverse()
        {
            if(_Head != null)
            {
                Node<T> temp = _Head;
                do
                {
                    Console.WriteLine(temp.Value);
                    temp = temp.NextNode;
                }
                while (temp != null);
            }
        }
    }
}

 

 

The Code (Console Program to test this out):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkListConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkList<string> stringlist = new LinkList<string>();
            stringlist.Insert("Test 1");
            stringlist.Insert("Test 2");
            stringlist.Insert("Test 3");

            stringlist.Transverse();


            Console.WriteLine("Assignment and reading using indexer");

            Console.WriteLine(stringlist[2]);
            Console.WriteLine(stringlist[1]);
            stringlist[0] = "This is my new value";

            stringlist.Transverse();

            Console.WriteLine("Using Enumerator");

            foreach (string value in stringlist)
            {
                Console.WriteLine(value);
            }

            Console.WriteLine("Move First");
            stringlist.MoveFirst();
            Console.WriteLine(stringlist.Value);
            //Move 2 times
            stringlist.Next();
            stringlist.Next();
            Console.WriteLine(stringlist.Value);

            Console.ReadLine();

        }
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .NET | Algorithms | C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 5. 2009 16:11