Advertisement
jhei13

LinkedLeast001

Aug 27th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1.  public class LinkedList : IList
  2.     {
  3.         private Node head, tail, current;
  4.         private int count;
  5.  
  6.         public int Count
  7.         {
  8.             get { return count; }
  9.             set { count = value; }
  10.         }
  11.         public object First
  12.         {
  13.             get
  14.             {
  15.                 try
  16.                 {
  17.                     return head.Data;
  18.                 }
  19.                 catch (NullReferenceException)
  20.                 {
  21.                     Console.Write("List is Empty");
  22.                     return (-32767);
  23.                 }
  24.             }
  25.         }
  26.  
  27.         public object Last
  28.         {
  29.             get
  30.             {
  31.                 try
  32.                 {
  33.                     return tail.Data;
  34.  
  35.                 }
  36.                 catch
  37.                 {
  38.                     Console.Write("List is Empty");
  39.                     return (-32767);
  40.                 }
  41.             }
  42.         }
  43.         public bool IsEmpty()
  44.         {
  45.             return (head == null && tail == null);
  46.         }
  47.         public void Clear()
  48.         {
  49.             this.head = null;
  50.             tail = null;
  51.             current = null;
  52.             this.count = 0;
  53.         }
  54.  
  55.         public void Insert(object item)
  56.         {
  57.             Node NeoNude = new Node(item, null);
  58.             count++;
  59.             if (IsEmpty())
  60.             {
  61.                 head = NeoNude;
  62.             }
  63.             else
  64.             {
  65.                 tail.Next = NeoNude;
  66.             }
  67.             tail = NeoNude;
  68.         }
  69.         public void InsertAt(object item, int pos)
  70.         {
  71.                 if (!IsEmpty())
  72.                 {
  73.                     Node NeoNude = new Node(item, null);
  74.      
  75.                     if (pos == 0) // if index is head
  76.                     {
  77.                         NeoNude.Next = head;
  78.                         head = NeoNude;
  79.                     }
  80.                     else if (pos <= count - 1) // if index is in middle
  81.                     {
  82.                         current = head;
  83.                         Node temp = null;
  84.      
  85.                         for (int i = 0; i < pos; i++)
  86.                         {
  87.                             temp = current;
  88.                             current = current.Next;
  89.                         }
  90.      
  91.                         NeoNude.Next = current;
  92.                         temp.Next = NeoNude;
  93.                     }
  94.                     else if (pos == count - 1)// if index is last
  95.                         Insert(item);
  96.                     else
  97.                     {
  98.                         Console.WriteLine("FAILED");
  99.                         return;
  100.                     }
  101.                     Count++;
  102.                 }
  103.             }
  104.  
  105.  
  106.        
  107.         public void DisplayList()
  108.         {
  109.             string list = "";
  110.             current = head;
  111.             while (current != null)
  112.             {
  113.                 list = list + current.Data + " ";
  114.                 current = current.Next;
  115.             }
  116.             Console.Write(list);
  117.         }
  118.         public bool Find(object item)
  119.         {
  120.             current = head;
  121.             //bool flag = false;
  122.             if (!IsEmpty())
  123.             {
  124.                
  125.                 while (current != null)
  126.                 {
  127.                     if (item.Equals(current.Data))
  128.                     {
  129.                         return true;
  130.                     }
  131.                     else
  132.                     {
  133.                         current = current.Next;
  134.                        
  135.                     }
  136.                 }
  137.                
  138.             }
  139.             return false;
  140.         }
  141.  
  142.         public void Remove(object item)
  143.         {
  144.              if (!IsEmpty())
  145.             {
  146.                 current = head;
  147.                 while(current != null)
  148.                 {
  149.                     if (head.Data.Equals(item))
  150.                     {
  151.                         head = current.Next;
  152.                         current.Next = null;
  153.                         break;
  154.                     }
  155.                     Node temp = current.Next;
  156.                     if (temp.Data.Equals(item))
  157.                     {
  158.                         if (temp.Next == null)
  159.                         {
  160.                             tail = current;
  161.                             current.Next = temp.Next;
  162.                         }
  163.                         else if (temp.Next != null)
  164.                         {
  165.                             current.Next = temp.Next;
  166.                         }
  167.                         break;
  168.                     }
  169.                     current = current.Next;
  170.                 }
  171.                 count--;
  172.             }
  173.         }
  174.        
  175.  
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement