Advertisement
Guest User

DoublyLinkedList

a guest
Nov 27th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication7
  8. {
  9.     class Node
  10.     {
  11.         object _data;
  12.         public object Data {
  13.             get { return _data; }
  14.             set { _data = value; }
  15.         }
  16.  
  17.         Node _next;
  18.         public Node Next
  19.         {
  20.             get { return _next; }
  21.             set { _next = value; }
  22.         }
  23.  
  24.         Node _prev;
  25.         public Node Prev
  26.         {
  27.             get { return _prev; }
  28.             set { _prev = value; }
  29.         }
  30.  
  31.         public Node(object d, Node n, Node p)
  32.         {
  33.             Data = d;
  34.             Next = n;
  35.             Prev = p;
  36.         }
  37.     }
  38.  
  39.     class DoublyList
  40.     {
  41.         Node _head;
  42.         public Node head
  43.         {
  44.             get { return _head; }
  45.             set { _head = value; }
  46.         }
  47.  
  48.         Node _tail;
  49.         public Node tail
  50.         {
  51.             get { return _tail; }
  52.             set { _tail = value; }
  53.         }
  54.  
  55.         int _count;
  56.         public int count
  57.         {
  58.             get { return _count; }
  59.             set { }
  60.         }
  61.  
  62.         public DoublyList()
  63.         {
  64.             head = null;
  65.             tail = null;
  66.             count = 0;
  67.         }
  68.  
  69.         public object Add(object o)
  70.         {
  71.             return Add(count, o);
  72.         }
  73.  
  74.         public object Add(int index, object o)
  75.         {
  76.             if (index < 0 || index > count)
  77.             {
  78.                 throw new IndexOutOfRangeException("Az index nem lehet negatív!");
  79.             }
  80.             if (Empty() || index == 0)
  81.             {
  82.                 head = new Node(o, null, null);
  83.                 tail = head;
  84.             }
  85.             else
  86.             {
  87.                 if (count / 2 > index)
  88.                 {
  89.                     Node current = head;
  90.                     for (int i = 0; i < index - 1; i++)
  91.                     {
  92.                         current = current.Next;
  93.                     }
  94.                     Node next = current.Next;
  95.                     current.Next = new Node(o, current.Next, current);
  96.                     if (next != null)
  97.                     {
  98.                         next.Prev = current.Next;                        
  99.                     }
  100.                     else
  101.                     {
  102.                         tail = current.Next;
  103.                     }
  104.                 }
  105.                 else
  106.                 {
  107.                     Node current = tail;
  108.                     for (int i = 0; i < index - 1; i++)
  109.                     {
  110.                         current = current.Prev;
  111.                     }
  112.                     Node prev = current.Next;
  113.                     current.Prev = new Node(o, current.Prev, current);
  114.                     if (prev != null)
  115.                     {
  116.                         prev.Next = current.Prev;
  117.                     }
  118.                     else
  119.                     {
  120.                         head = current.Prev;
  121.                     }
  122.                 }
  123.             }
  124.             _count++;
  125.             return o;
  126.         }
  127.  
  128.         public object Remove(int index)
  129.         {
  130.             object x;
  131.             if (index < 0 || index > count)
  132.             {
  133.                 throw new IndexOutOfRangeException("Az index nem lehet negatív!");
  134.             }
  135.             else if (Empty() || index == 0)
  136.             {
  137.                 x = head.Next.Data;
  138.                 head.Next = head.Next.Next;
  139.             }
  140.             else
  141.             {
  142.                 Node current = head;
  143.                 for (int i = 0; i < index; i++)
  144.                 {
  145.                     current = current.Next;
  146.                 }
  147.                 x = current.Data;
  148.                 current = current.Next;
  149.             }
  150.             _count--;
  151.             return x;
  152.         }
  153.  
  154.         public object Get(int index)
  155.         {
  156.             Node current = head;
  157.             for (int i = 0; i < index - 1; i++)
  158.             {
  159.                 current = current.Next;
  160.             }
  161.             return current.Next.Data;
  162.         }
  163.  
  164.         public void Clear()
  165.         {
  166.             head = null;
  167.         }
  168.  
  169.         public bool Empty()
  170.         {
  171.             return count == 0;
  172.         }
  173.  
  174.         public int IndexOf(object o)
  175.         {
  176.             Node current = head;
  177.             if (Empty() || o == null)
  178.             {
  179.                 throw new Exception("");
  180.             }
  181.             else
  182.             {
  183.                 for (int i = 0; i < count; i++)
  184.                 {
  185.                     if (current.Data.Equals(o))
  186.                     {
  187.                         return i;
  188.                     }
  189.                     current = current.Next;
  190.                 }
  191.                 return -1;
  192.             }
  193.         }
  194.  
  195.         public bool Contains(object o)
  196.         {
  197.             return !(IndexOf(o) == -1);
  198.         }
  199.     }
  200.  
  201.     class Program
  202.     {
  203.         static void Main(string[] args)
  204.         {
  205.  
  206.             DoublyList a = new DoublyList();
  207.  
  208.             a.Add(10);
  209.             a.Add(10);
  210.             a.Add(10);
  211.             a.Add(10);
  212.             a.Add(10);
  213.  
  214.             Console.WriteLine(a.Get(4));
  215.  
  216.             Console.ReadKey();
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement