Advertisement
StevanovicMilan

Zadatak 4 - Generic

Oct 31st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.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 Vezba_04_2017
  8. {
  9.     class GenericQueueNode<T>
  10.     {
  11.         public GenericQueueNode(T content)
  12.         {
  13.             Content = content;
  14.         }
  15.  
  16.         public T Content { get; set; }
  17.         public GenericQueueNode<T> next { get; set; }
  18.     }
  19.  
  20.     class GenericQueue<T>
  21.     {
  22.         private GenericQueueNode<T> _head;
  23.         private GenericQueueNode<T> _tail;
  24.  
  25.         public bool IsEmpty
  26.         {
  27.             get { return _head == null; }
  28.         }
  29.         public void Put(T x)
  30.         {
  31.             GenericQueueNode<T> newNode = new GenericQueueNode<T>(x);
  32.  
  33.             if (IsEmpty)
  34.                 {
  35.                 _head = newNode;
  36.                 _tail = newNode;
  37.             }
  38.             else
  39.             {
  40.                 _tail.next = newNode;
  41.                 _tail = newNode;
  42.             }
  43.         }
  44.  
  45.         public T Get()
  46.         {
  47.             if (IsEmpty)
  48.                 throw new InvalidOperationException("D kju is empti");
  49.  
  50.             T x = _head.Content;
  51.             if(_head == _tail)
  52.             {
  53.                 _head = null;
  54.                 _tail = null;
  55.             }
  56.             else
  57.             {
  58.                 _head = _head.next;
  59.             }
  60.             return x;
  61.         }
  62.  
  63.         public override string ToString()
  64.         {
  65.             StringBuilder sb = new StringBuilder();
  66.  
  67.             GenericQueueNode<T> node = _head;
  68.             while(node != null)
  69.             {
  70.                 sb.AppendFormat("{0} ", node.Content);
  71.                 node = node.next;
  72.             }
  73.  
  74.             return sb.ToString();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement