Advertisement
TodorMitev

DoubleLinkedList<T>

Jun 21st, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DoubleLinkedListExample
  7. {
  8.     public class Node<T>
  9.     {
  10.         public T item;
  11.         public Node<T> next;
  12.         public Node<T> prev;
  13.  
  14.         public Node(T data)
  15.         {
  16.             this.item = data;
  17.             this.next = null;
  18.             this.prev = null;
  19.         }
  20.     }
  21.     public class DoubleLinkedList<T>
  22.     {
  23.         public int Size { get; set; }
  24.  
  25.         private Node<T> head;
  26.         private Node<T> current;
  27.  
  28.         public DoubleLinkedList()
  29.         {
  30.             this.Size = 0;
  31.             this.head = null;
  32.             this.current = null;
  33.         }
  34.  
  35.         public void Add(Node<T> element)
  36.         {
  37.             if (this.head == null)
  38.             {
  39.                 this.head = element;
  40.                 this.head.prev = null;
  41.                 this.current = this.head;
  42.                 this.head.next = this.current;
  43.                 this.Size++;
  44.             }
  45.             else
  46.             {
  47.                 this.current.prev = this.current;
  48.                 this.current.next = element;
  49.                 this.current = this.current.next;
  50.                 this.Size++;
  51.             }
  52.         }
  53.  
  54.         public void AddToHead(Node<T> element)
  55.         {
  56.             if (this.head == null)
  57.             {
  58.                 this.head = element;
  59.                 this.current = this.head;
  60.                 this.head.next = this.current;
  61.                 this.Size++;
  62.             }
  63.             else
  64.             {
  65.                 this.head.prev = element;
  66.                 this.head.prev.next = this.head;
  67.                 this.head = this.head.prev;
  68.                 this.head.prev = null;
  69.                 this.Size++;
  70.             }
  71.         }
  72.  
  73.         public T this[int index]
  74.         {
  75.             get
  76.             {
  77.                 if(index >= this.Size || index<0)
  78.                 {
  79.                     throw new IndexOutOfRangeException(
  80.                     String.Format("The index is out range! (index: {0}, size: {1})", index, this.Size));
  81.                 }
  82.  
  83.                 Node<T> result = this.head;
  84.                 result = GetElementOfPosition(index, result);
  85.                 return result.item;
  86.             }
  87.         }
  88.  
  89.         public void InsertAt(Node<T> element, int position)
  90.         {
  91.             if (position > this.Size || position < 0)
  92.             {
  93.                 throw new IndexOutOfRangeException(
  94.                     String.Format("The position is out range! (pos: {0}, size: {1})", position, this.Size));
  95.             }
  96.             if (position == this.Size)
  97.             {
  98.                 Add(element);
  99.             }
  100.             else
  101.             {
  102.                 Node<T> currentAtPosition = this.head;
  103.                 currentAtPosition = GetElementOfPosition(position - 1, currentAtPosition);
  104.                 Node<T> nextToCurrentAtPosition = currentAtPosition.next;
  105.                 currentAtPosition.next = element;
  106.                 element.next = nextToCurrentAtPosition;
  107.                 this.Size++;
  108.             }
  109.         }
  110.  
  111.         private Node<T> GetElementOfPosition(int index, Node<T> result)
  112.         {
  113.             for (int i = 0; i < index; i++)
  114.             {
  115.                 result = GetNext(result);
  116.             }
  117.             return result;
  118.         }
  119.  
  120.         private Node<T> GetNext(Node<T> element)
  121.         {
  122.             Node<T> result = element.next;
  123.             return result;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement