StevanovicMilan

Zadatak 2 - Generic

Oct 31st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 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 Vezba_04_2017
  8. {
  9.     class GenericStack<T>
  10.     {
  11.        class GenericStackNode<T>
  12.         {
  13.             public GenericStackNode(T t)
  14.             {
  15.                 Content = t;
  16.                 Next = null;
  17.             }
  18.  
  19.             public T Content { get; set; }
  20.             public GenericStackNode<T> Next { get; set; }
  21.         }
  22.  
  23.  
  24.  
  25.         private GenericStackNode<T> _head;
  26.  
  27.         public int Lenght
  28.         {
  29.             get
  30.             {
  31.                 int lenght = 0;
  32.  
  33.                 GenericStackNode<T> current = _head;
  34.                 while(current != null)
  35.                 {
  36.                     current = current.Next;
  37.                     lenght++;
  38.                 }
  39.                 return lenght;
  40.             }
  41.         }
  42.  
  43.         public bool IsEmpty
  44.         {
  45.             get { return _head == null; }
  46.         }
  47.  
  48.         public T this[int index]
  49.         {
  50.             get
  51.             {
  52.                 return GetNode(index).Content;
  53.             }
  54.             set
  55.             {
  56.                 GetNode(index).Content = value;
  57.             }
  58.         }
  59.  
  60.         private GenericStackNode<T> GetNode(int index)
  61.         {
  62.             if (index < 0 || index >= Lenght)
  63.                 throw new IndexOutOfRangeException();
  64.  
  65.             GenericStackNode<T> current = _head;
  66.             for (int i = 0; i < index; i++)
  67.                 current = current.Next;
  68.  
  69.             return current;
  70.         }
  71.  
  72.         public void AddFirst(T content)
  73.         {
  74.             GenericStackNode<T> newNode = new GenericStackNode<T>(content);
  75.             newNode.Next = _head;
  76.             _head = newNode;
  77.         }
  78.  
  79.         public void AddLast(T content)
  80.         {
  81.             if (IsEmpty)
  82.                 AddFirst(content);
  83.             else
  84.             {
  85.                 GenericStackNode<T> newNode = new GenericStackNode<T>(content);
  86.                 GenericStackNode<T> currentNode = _head;
  87.                 while (currentNode.Next != null)
  88.                 {
  89.                     currentNode = currentNode.Next;
  90.                 }
  91.                 currentNode.Next = newNode;
  92.             }
  93.         }
  94.  
  95.         public void RemoveFirst()
  96.         {
  97.             if (!IsEmpty)
  98.                 _head = _head.Next;
  99.             else
  100.                 throw new InvalidOperationException();
  101.         }
  102.        
  103.         public void RemoveLast()
  104.         {
  105.             if(!IsEmpty)
  106.             {
  107.                 if (_head.Next == null)
  108.                     _head = null;
  109.                 else
  110.                 {
  111.                     GenericStackNode<T> node = _head;
  112.                     while (node.Next.Next != null)
  113.                         node = node.Next;
  114.                     node.Next = null;
  115.                 }
  116.             }
  117.         }
  118.         public void InsertAt(T content, int index)
  119.         {
  120.             if (index < 0 || index >= Lenght)
  121.                 throw new IndexOutOfRangeException();
  122.  
  123.             if (index == 0)
  124.                 AddFirst(content);
  125.             else if (index == Lenght)
  126.                 AddLast(content);
  127.             else
  128.             {
  129.                 GenericStackNode<T> newNode = new GenericStackNode<T>(content);
  130.                 GenericStackNode<T> previousNode = GetNode(index - 1);
  131.                 newNode.Next = previousNode.Next;
  132.                 previousNode.Next = newNode;
  133.             }
  134.  
  135.  
  136.         }
  137.  
  138.         public void Clear()
  139.         {
  140.             _head = null;
  141.         }
  142.  
  143.         public int IndexOf(T content)
  144.         {
  145.             GenericStackNode<T> newNode = new GenericStackNode<T>(content);
  146.  
  147.             for (int i = 0; i < Lenght; i++)
  148.             {
  149.                 if (_head == newNode)
  150.                     return i;
  151.                 _head = _head.Next;
  152.             }
  153.             return -1;
  154.         }
  155.         public override string ToString()
  156.         {
  157.             StringBuilder sb = new StringBuilder();
  158.  
  159.             GenericStackNode<T> node = _head;
  160.             while (node != null)
  161.             {
  162.                 sb.Append(node.Content);
  163.                 sb.Append(" -> ");
  164.                 node = node.Next;
  165.             }
  166.             sb.Append("*");
  167.  
  168.             return sb.ToString();
  169.         }
  170.     }
  171. }
Add Comment
Please, Sign In to add comment