simonradev

Deck

Dec 21st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 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 DeckTest
  8. {
  9.     class DeckTest
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Deck<string> testDeck = new Deck<string>();
  14.  
  15.             testDeck.PushLeft("one");
  16.             testDeck.PushLeft("two");
  17.             testDeck.PushLeft("three");
  18.             testDeck.PushRight("four");
  19.             testDeck.PushRight("five");
  20.             testDeck.PushRight("six");
  21.             testDeck.PushRight("seven");
  22.  
  23.            string checkLett = testDeck.PeekLeft();
  24.             string checkRight = testDeck.PeekRight();
  25.            
  26.  
  27.             int count = testDeck.Count;
  28.         }
  29.     }
  30.  
  31.     /// <summary>
  32.     /// This is a linear, generic data type that works like a STACK but has two sides /Right and Left/.
  33.     /// You can choose into which side you want to insert an element.
  34.     /// You can retrieve the elements only from the side you inserted it into.
  35.     /// </summary>
  36.     /// <typeparam name="TType"></typeparam>
  37.     class Deck<TType>
  38.     {
  39.         private TType[] leftElements;
  40.         private TType[] rightElements;
  41.         private int leftCount;
  42.         private int rightCount;
  43.         private int leftElementsSize = 6;
  44.         private int rightElementsSize = 6;
  45.         private bool rightIsForResizing;
  46.  
  47.         public Deck()
  48.         {
  49.             leftElements = new TType[leftElementsSize];
  50.             rightElements = new TType[rightElementsSize];
  51.         }
  52.  
  53.         public int Count { get { return (this.leftCount + this.rightCount); } }
  54.  
  55.         /// <summary>
  56.         /// Insert the element on the right of the deck.
  57.         /// </summary>
  58.         /// <param name="rightElement"></param>
  59.         public void PushRight(TType rightElement)
  60.         {
  61.             if (rightCount == rightElements.Length)
  62.             {
  63.                 rightIsForResizing = true;
  64.                 DoubleUpTheSize();
  65.             }
  66.  
  67.             rightElements[rightCount] = rightElement;
  68.  
  69.             rightCount++;
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Insert the element on the left of the deck.
  74.         /// </summary>
  75.         /// <param name="leftElement"></param>
  76.         public void PushLeft(TType leftElement)
  77.         {
  78.             if (leftCount == leftElements.Length)
  79.             {
  80.                 rightIsForResizing = false;
  81.                 DoubleUpTheSize();
  82.             }
  83.  
  84.             leftElements[leftCount] = leftElement;
  85.  
  86.             leftCount++;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Returns and removes the last element from the right side of the deck.
  91.         /// </summary>
  92.         /// <returns></returns>
  93.         public TType PopRight()
  94.         {
  95.             if (rightCount == 0)
  96.             {
  97.                 throw new InvalidOperationException
  98.                     ("The right deck is empty. No item to pop. Try inserting something first.");
  99.             }
  100.  
  101.             int indexToRemoveFromRight = rightCount - 1;
  102.  
  103.             TType elementToReturn = rightElements[indexToRemoveFromRight];
  104.             rightElements[indexToRemoveFromRight] = default(TType);
  105.  
  106.             rightCount--;
  107.  
  108.             return elementToReturn;
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Returns and removes the last element from the left side of the deck.
  113.         /// </summary>
  114.         /// <returns></returns>
  115.         public TType PopLeft()
  116.         {
  117.             if (leftCount == 0)
  118.             {
  119.                 throw new InvalidOperationException
  120.                     ("The left deck is empty. No item to pop. Try inserting something first.");
  121.             }
  122.  
  123.             int indexToRemoveFromLeft = leftCount - 1;
  124.  
  125.             TType elementToReturn = leftElements[indexToRemoveFromLeft];
  126.             leftElements[indexToRemoveFromLeft] = default(TType);
  127.  
  128.             leftCount--;
  129.  
  130.             return elementToReturn;
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Only returns without removing the last element from the right side of the deck.
  135.         /// </summary>
  136.         /// <returns></returns>
  137.         public TType PeekRight()
  138.         {
  139.             if (rightCount == 0)
  140.             {
  141.                 throw new InvalidOperationException
  142.                     ("The right deck is empty. No item to peek. Try inserting something first.");
  143.             }
  144.  
  145.             int itemToPeek = rightCount - 1;
  146.  
  147.             return rightElements[itemToPeek];
  148.         }
  149.  
  150.         /// <summary>
  151.         /// Only returns without removing the last element from the left side of the deck.
  152.         /// </summary>
  153.         /// <returns></returns>
  154.         public TType PeekLeft()
  155.         {
  156.             if (leftCount == 0)
  157.             {
  158.                 throw new InvalidOperationException
  159.                     ("The left deck is empty. No item to peek. Try inserting something first.");
  160.             }
  161.  
  162.             int itemToPeek = leftCount - 1;
  163.  
  164.             return leftElements[itemToPeek];
  165.         }
  166.  
  167.         private void DoubleUpTheSize()
  168.         {
  169.             if (rightIsForResizing)
  170.             {
  171.                 rightElementsSize *= 2;
  172.                 TType[] rightArrHolder = new TType[rightElements.Length];
  173.                 Array.Copy(rightElements, rightArrHolder, rightElements.Length);
  174.                 rightElements = new TType[rightElementsSize];
  175.                 Array.Copy(rightArrHolder, rightElements, rightArrHolder.Length);
  176.                 rightArrHolder = default(TType[]);
  177.             }
  178.             else
  179.             {
  180.                 leftElementsSize *= 2;
  181.                 TType[] leftArrHolder = new TType[leftElements.Length];
  182.                 Array.Copy(leftElements, leftArrHolder, leftArrHolder.Length);
  183.                 leftElements = new TType[leftElementsSize];
  184.                 Array.Copy(leftArrHolder, leftElements, leftArrHolder.Length);
  185.                 leftArrHolder = default(TType[]);
  186.             }
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment