Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 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 GDD2200_DoublyLinkedList
  8. {
  9.     /// <summary>
  10.     /// DoublyLinkedList
  11.     /// </summary>
  12.     public class DoublyLinkedList<U>
  13.     {
  14.         /// <summary>
  15.         /// DoublyLinkedListNode
  16.         /// Private class that is only known to the DoublyLinkedList class
  17.         /// and cannot be accessed by any "outside" code
  18.         /// </summary>
  19.        
  20.         private DoublyLinkedListNode<U> head;
  21.         private DoublyLinkedListNode<U> tail;
  22.         private int count;
  23.          
  24.         private class DoublyLinkedListNode<T>
  25.         {
  26.  
  27.             #region Properties_DLLN
  28.             /// <summary>
  29.             /// Reference to the next node in the list
  30.             /// </summary>
  31.            
  32.             public DoublyLinkedListNode Next { get; set; }
  33.  
  34.             /// <summary>
  35.             /// Reference to the previous node in the list
  36.             /// </summary>
  37.             public DoublyLinkedListNode Prev { get; set; }
  38.  
  39.             /// <summary>
  40.             /// Item stored in the node
  41.             /// </summary>
  42.             public int Item { get; set; }
  43.             #endregion  
  44.            
  45.             /// <summary>
  46.             /// Node constructor
  47.             /// </summary>
  48.             /// <param name="item">item to store in the node</param>
  49.             /// <param name="next">next node in the list</param>
  50.             /// <param name="prev">previous node in the list</param>
  51.             public DoublyLinkedListNode(T item, DoublyLinkedListNode<T> next = null, DoublyLinkedListNode<T> prev = null)
  52.             {
  53.                 Item = item;
  54.                 Next = next;
  55.                 Prev = prev;
  56.             }
  57.         }
  58.  
  59.         #region Properties_DDL
  60.         /// <summary>
  61.         /// Reference to the "front" of the list
  62.         /// </summary>
  63.         private DoublyLinkedListNode Head { get; set; }
  64.  
  65.         /// <summary>
  66.         /// Reference to the "end" of the list
  67.         /// </summary>
  68.         private DoublyLinkedListNode Tail { get; set; }
  69.  
  70.         /// <summary>
  71.         /// Number of items in the list
  72.         /// </summary>
  73.         public int Count { get; private set; }
  74.         #endregion
  75.  
  76.         /// <summary>
  77.         /// Add the item to the front of the list
  78.         /// </summary>
  79.         /// <param name="item">item to add</param>
  80.         public void AddFront(int item)
  81.         {
  82.             //Adding to Empty List in Front
  83.             if (this.head == null)
  84.             {
  85.                 this.head = new DoublyLinkedListNode<U>(item);
  86.                 this.tail = this.head;
  87.             }
  88.             //Adding Item to Current List Front
  89.             else
  90.             {
  91.                 head.Prev = new DoublyLinkedListNode<U>(item);
  92.                 head.Prev.Next = head;
  93.                 head = head.Prev;
  94.             }
  95.             count++;
  96.  
  97.             //Throws an exception if the list is at 0
  98.             if (item <= 0)
  99.                 throw new ArgumentOutOfRangeException("Item(s) are at negative: " + item);
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Add the item to the back of the list
  104.         /// </summary>
  105.         /// <param name="item">item to add</param>
  106.         public void AddBack(int item)
  107.         {
  108.             tail.Next = new DoublyLinkedListNode(item);
  109.             tail.Next.Prev = tail;
  110.             tail = tail.Next;
  111.  
  112.             //Throws an exception if the list is at 0
  113.             if (item <= 0)
  114.                 throw new ArgumentOutOfRangeException("Item(s) are at Zero or Negative: " + item);
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Remove the item from the front of the list
  119.         /// </summary>
  120.         /// <returns>item removed</returns>
  121.         public int RemoveFront()
  122.         {
  123.                 //Remove First Node
  124.                 DoublyLinkedListNode<U> frontCurrent = head.Next;
  125.                 if (frontCurrent != null)
  126.                 {
  127.                     frontCurrent.Prev = null;
  128.                 }
  129.                 head = head.Next;
  130.                 count--;
  131.            
  132.             // Throws an exception if the list is empty
  133.             if (count <= 0)
  134.                 throw new ArgumentOutOfRangeException("Item(s) are at Zero or Negative:");
  135.             return 0;
  136.         }
  137.  
  138.         /// <summary>
  139.         /// Remove item from the back of the list
  140.         /// </summary>
  141.         /// <returns>item removed</returns>
  142.         public int RemoveBack()
  143.         {
  144.                 //Remove Last Node
  145.                 DoublyLinkedListNode<U> backCurrent = tail.Prev;
  146.                 if (backCurrent != null)
  147.                 {
  148.                     backCurrent.Next = null;
  149.                 }
  150.                 tail = tail.Prev;
  151.                 count--;
  152.            
  153.             // Throws an exception if the list is empty
  154.             if (count <= 0)
  155.                 throw new ArgumentOutOfRangeException("Item(s) are at Zero or Negative:");
  156.  
  157.             return 0;
  158.         }
  159.  
  160.         /// <summary>
  161.         /// Remove the item provided from the list
  162.         /// </summary>
  163.         /// <param name="item">item to remove</param>
  164.         /// <returns>true if found and removed, false otherwise</returns>
  165.         public bool RemoveItem(int item)
  166.         {
  167.             //If First Node Removed
  168.             if (head.Item == item)
  169.             {
  170.                 // Check Remove Single Node
  171.                 if (head == tail)
  172.                 {
  173.                     tail = null;
  174.                 }
  175.  
  176.                 // Remove First and Decrement
  177.                 DoublyLinkedListNode<U> next = head.Next;
  178.                 if (next != null)
  179.                 {
  180.                     next.Prev = null;
  181.                 }
  182.                 head = head.Next;
  183.                 count--;
  184.  
  185.                 return true;
  186.             }
  187.  
  188.             else
  189.             {
  190.                 DoublyLinkedListNode<U> current = head;
  191.                 while (current != null)
  192.                 {
  193.                     if (current.Item == item)
  194.                     {
  195.                         //Remove Current
  196.                         DoublyLinkedListNode<U> next = current.Next;
  197.  
  198.                         if (next != null)
  199.                         {
  200.                             next.Prev = current.Prev;
  201.                         }
  202.  
  203.                         current.Prev.Next = next;
  204.                         count--;
  205.  
  206.                         //If Remove Last Node
  207.                         if (current == tail)
  208.                         {
  209.                             tail = current.Prev;
  210.                         }
  211.  
  212.                         //Null References
  213.                         current.Prev = null;
  214.                         current.Next = null;
  215.  
  216.                         return true;
  217.                     }
  218.                     // Run Along Remainder of List
  219.                     current = current.Next;
  220.                 }
  221.             }
  222.             return false;
  223.         }
  224.  
  225.         /// <summary>
  226.         /// Search for the item in the list
  227.         /// </summary>
  228.         /// <param name="item">true if found, false if otherwise</param>
  229.         /// <returns></returns>
  230.         public bool Search(int item)
  231.         {
  232.             //IndexOf Search
  233.             DoublyLinkedListNode<U> search = this.head;
  234.             for (int i = 0; i < this.count; i++)
  235.             {
  236.                 if (search.Item.Equals(item))
  237.                 {
  238.                     return true;
  239.                 }
  240.                 search = search.Next;
  241.             }
  242.             return false;
  243.         }
  244.  
  245.         /// <summary>
  246.         /// Convert the list to a string
  247.         /// </summary>
  248.         /// <returns>The list as a string</returns>
  249.         /// DO NOT EDIT THIS METHOD OR ALL OF YOUR TESTS WILL FAIL
  250.         public override string ToString()
  251.         {
  252.             DoublyLinkedListNode<U> curr = Head;
  253.             StringBuilder output = new StringBuilder("HEAD->");
  254.             while (curr != null)
  255.             {
  256.                 output.Append(curr.Item);
  257.                 output.Append("->");
  258.                 curr = curr.Next;
  259.             }
  260.             output.Append("NULL\n");
  261.  
  262.             output.Append("TAIL->");
  263.             curr = Tail;
  264.             while (curr != null)
  265.             {
  266.                 output.Append(curr.Item);
  267.                 output.Append("->");
  268.                 curr = curr.Prev;
  269.             }
  270.             output.Append("NULL");
  271.  
  272.             return output.ToString();
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement