Pages

Friday, April 6, 2012

Data Structure: How to Create a One Way Linear Linked List in C# .Net

In the world of software engineering data structures like linked lists are often needed. Here is a way to create a one way linear linked list using C# .Net. But any other object oriented programming language can be used in the same way. Some common linked list functionality are given. Hope it will help you.


   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace Datastructure
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              OneWayLinearLinkedList linkedList = new OneWayLinearLinkedList();
  13:   
  14:              Console.WriteLine("Insert:");
  15:              for(int i = 0; i < 6; i++)
  16:              {
  17:                  linkedList.Insert(new Node(i));
  18:              }
  19:              linkedList.Print();
  20:   
  21:              Console.WriteLine("Insert At:");
  22:              linkedList.InsertAt(new Node(11), 3);
  23:              linkedList.Print();
  24:   
  25:              Console.WriteLine("Delete:");
  26:              linkedList.Delete(linkedList.NodeAt(2));
  27:              linkedList.Print();
  28:   
  29:              Console.WriteLine("Delete At:");
  30:              linkedList.DeleteAt(2);
  31:              linkedList.Print();
  32:   
  33:              Console.WriteLine("Clear:");
  34:              linkedList.Clear();
  35:              linkedList.Print();
  36:   
  37:              Console.ReadKey();
  38:          }
  39:      }
  40:   
  41:      /// <summary>
  42:      /// One way linear linked list
  43:      /// </summary>
  44:      public class OneWayLinearLinkedList
  45:      {
  46:          private Node head;
  47:   
  48:          public OneWayLinearLinkedList()
  49:          {
  50:              head = null;
  51:          }
  52:   
  53:          /// <summary>
  54:          /// Gets the node at a specific postion
  55:          /// </summary>        
  56:          public Node NodeAt(int index)
  57:          {
  58:              if (head == null)
  59:                  return null;
  60:              else if (index == 0)
  61:              {
  62:                  return head;
  63:              }
  64:              else
  65:              {
  66:                  Node currentNode = head.NextNode;
  67:                  Node currentNodeNext = currentNode.NextNode;
  68:   
  69:                  for (int i = 1; i < index; i++)
  70:                  {
  71:                      currentNode = currentNode.NextNode;
  72:                      currentNodeNext = currentNode.NextNode;
  73:                  }
  74:   
  75:                  return currentNode;
  76:              }
  77:          }
  78:          
  79:          /// <summary>
  80:          /// Inserts a node at the begining of the linked list
  81:          /// </summary>
  82:          public void Insert(Node newNode)
  83:          {            
  84:              if (head == null)
  85:              {                
  86:                  head = newNode;
  87:                  newNode.NextNode = null;
  88:              }
  89:              else
  90:              {
  91:                  newNode.NextNode = head;
  92:                  head = newNode;
  93:              }
  94:          }
  95:   
  96:          /// <summary>
  97:          /// Inserts a node at a specific position
  98:          /// </summary>
  99:          public void InsertAt(Node newNode, int index)
 100:          {
 101:              if (index == 0)
 102:              {
 103:                  newNode.NextNode = head;
 104:                  head = newNode;
 105:              }
 106:              else
 107:              {
 108:                  Node currentNode = head;
 109:                  Node currentNodeNext = head.NextNode;
 110:                  for (int i = 1; i < index; i++)
 111:                  {
 112:                      currentNode = currentNode.NextNode;
 113:                      currentNodeNext = currentNode.NextNode;
 114:                  }
 115:                  if (currentNode != null)
 116:                  {
 117:                      currentNode.NextNode = newNode;
 118:                      newNode.NextNode = currentNodeNext;
 119:                  }
 120:   
 121:              }
 122:          }
 123:          
 124:          /// <summary>
 125:          /// Deletes a node
 126:          /// </summary>
 127:          public void Delete(Node deleteNode)
 128:          {
 129:              bool isSuccess = false;
 130:   
 131:              if (head == null) return;
 132:   
 133:              else if (head.Equals(deleteNode))
 134:              {
 135:                  head = null;
 136:              }
 137:              else
 138:              {
 139:                  Node currentNode = head;
 140:                  Node currentNodeNext = head.NextNode;
 141:                  while (currentNodeNext != null)
 142:                  {
 143:                      if (currentNodeNext.Equals(deleteNode))
 144:                      {
 145:                          currentNode.NextNode = currentNodeNext.NextNode;
 146:                          currentNodeNext = null;
 147:                          break;
 148:                      }
 149:                      else
 150:                      {
 151:                          currentNode = currentNodeNext;
 152:                          currentNodeNext = currentNodeNext.NextNode;
 153:                      }
 154:                  }
 155:              }
 156:          }
 157:   
 158:          /// <summary>
 159:          /// Delets a node at a specific position
 160:          /// </summary>
 161:          public void DeleteAt(int index)
 162:          {
 163:              if (head == null) return;
 164:   
 165:              if (index == 0)
 166:              {
 167:                  head = null;
 168:              }
 169:              else
 170:              {
 171:                  Node currentNode = head;
 172:                  Node currentNodeNext = head.NextNode;
 173:                  for (int j = 1; j < index; j++)
 174:                  {
 175:                      currentNode = currentNode.NextNode;
 176:                      currentNodeNext = currentNode.NextNode;
 177:                  }
 178:                  if (currentNode != null)
 179:                  {
 180:                      currentNode.NextNode = currentNodeNext.NextNode;
 181:                  }
 182:              }
 183:          }
 184:   
 185:          /// <summary>
 186:          /// Prints the linear linked list
 187:          /// </summary>
 188:          public void Print()
 189:          {
 190:              Node firstNode = head;
 191:              while (firstNode != null)
 192:              {
 193:                  Console.Write(firstNode.Data + " ");
 194:                  firstNode = firstNode.NextNode;
 195:              }
 196:   
 197:              Console.Write("\n");
 198:          }
 199:   
 200:          /// <summary>
 201:          /// Clears the linked list
 202:          /// </summary>
 203:          public void Clear()
 204:          {
 205:              Node currentNode = head;
 206:              Node currentNodeNext;
 207:   
 208:              while (currentNode != null)
 209:              {
 210:                  currentNodeNext = currentNode.NextNode;
 211:                  currentNode = null;
 212:                  currentNode = currentNodeNext;
 213:              }
 214:   
 215:              head = null;
 216:          }
 217:   
 218:   
 219:   
 220:      }
 221:   
 222:      /// <summary>
 223:      /// Node for the one way linear linked list
 224:      /// </summary>
 225:      public class Node
 226:      {
 227:          public int Data { get; set; }
 228:          public Node NextNode { get; set; }
 229:   
 230:          public Node(int data)
 231:          {
 232:              this.Data = data;
 233:          }  
 234:      }
 235:  }

Wednesday, March 28, 2012

Design Pattern Tutorial Part 2: The Decorator Pattern (Structural Pattern) : Example in C#.net

Part 1

Here is an example of the implementation of the Decorator Pattern with C#.net.


   1:      class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              IComponent component = new Component();
   6:              Output("Original component: ", component);
   7:              Output("Decorated by A : ", new DecoratorA(component));
   8:              Output("Decorated by B: ", new DecoratorB(component));
   9:              Output("Decorated by A then by B: ", new DecoratorB(new DecoratorA(component)));
  10:              
  11:              DecoratorB b = new DecoratorB(new Component());
  12:              Output("Decorated by B then by A: ", new DecoratorA(b));
  13:              
  14:              // Extended behavior
  15:              Console.WriteLine("\t\t\t\t\t\t" + b.ExtendedBehaviour());
  16:   
  17:              Console.ReadKey();
  18:          }
  19:   
  20:          static void Output(string s, IComponent component)
  21:          {
  22:              Console.WriteLine(s + component.Operation());
  23:          }
  24:      }
  25:   
  26:      public interface IComponent
  27:      {
  28:          string Operation();
  29:      }
  30:   
  31:      public class Component : IComponent
  32:      {
  33:          private string firstName = "Debashish";
  34:          private string lastName = "Shiman";
  35:   
  36:          public string Operation()
  37:          {
  38:              return firstName + " " + lastName;
  39:          }
  40:      }
  41:   
  42:      public class DecoratorA : IComponent
  43:      {
  44:          IComponent component;
  45:   
  46:          public DecoratorA(IComponent c)
  47:          {
  48:              component = c;
  49:          }
  50:   
  51:          public string Operation()
  52:          {
  53:              string s = component.Operation();
  54:              s = "Mr " + s;
  55:              return s;
  56:          }
  57:      }
  58:   
  59:      public class DecoratorB : IComponent
  60:      {
  61:          IComponent component;
  62:   
  63:          public DecoratorB(IComponent c)
  64:          {
  65:              component = c;
  66:          }
  67:   
  68:          public string Operation()
  69:          {
  70:              string s = component.Operation();
  71:              s = s + ", Software Engineer";
  72:              return s;
  73:          }
  74:   
  75:          public string ExtendedBehaviour()
  76:          {
  77:              return " and Web Developer";
  78:          }
  79:      }

Design Pattern Tutorial Part 1: The Decorator Pattern (Structural Pattern)

The Decorator Pattern is used to extend the functionality of a particular object in runtime without changing the object hence without affecting the other instance of the same class. The object is said to be “decorated” with new extended functionality. This is achieved by creating a new wrapper or “decorator” class which wraps the original class. 

 

Features

One of the key features of the Decorator Pattern, from the point of implementation, is that a decorator class both inherits the original class and contains an instance of it. The other features are:
  • The original object remains unaffected by the decoration and so does the other instances.
  • No one class becomes burdened with lots of features making the objects light-weight.
  • The decorations are independent from each other, so that hundreds of decorators can be there to provide thousands of decoration combinations.

 

Uses

The Decorator pattern may be useful in following situations:
  • For evolving systems where there are frequent changes in functionality.
  • To attach new functionality or behaviour to an object.
  • To Change the behaviour of an object without affecting other instances.
  • Avoid subclassing as too many classes could result.
  • Where there is a component class unavailable for subclassing.

 

Design

 

  • IComponent: The interface that identifies the classes of objects that can be decorated.
  • Decorator: The class that both implements the “IComponent” interface and has an instance of it. It is possible to have more than one classes of this kind.
  • Component: The original class which can be decorated by extended behaviours.
  • DecoratorA & DecoratorB: Extended classes of “Decorator” for further extension.