Advertisement
StevanovicMilan

Zadatak 3 - Generic

Nov 1st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 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 ConsoleApplication7
  8. {
  9.  
  10.    class GenericQueueNode<T>
  11.     {
  12.         public GenericQueueNode(T content)
  13.         {
  14.             Content = content;
  15.             next = null;
  16.         }
  17.  
  18.         public T Content { get; set; }
  19.         public GenericQueueNode<T> next { get; set; }
  20.     }
  21.  
  22.     class GenericQueue<T>
  23.     {
  24.         private GenericQueueNode<T> _head;
  25.         private int count = 0;
  26.  
  27.         public bool IsEmpty
  28.         {
  29.             get { return _head == null; }
  30.         }
  31.  
  32.         public int Count { get { return count; } set { count = value; } }
  33.  
  34.         private GenericQueueNode<T> getLastNode()
  35.         {
  36.             GenericQueueNode<T> node = _head;
  37.  
  38.             while (node.next != null)
  39.                 node = node.next;
  40.  
  41.             return node;
  42.         }
  43.  
  44.         public void Put(T x)
  45.         {
  46.             GenericQueueNode<T> newNode = new GenericQueueNode<T>(x);
  47.  
  48.             if(IsEmpty)
  49.             {
  50.                 GenericQueueNode<T> node = new GenericQueueNode<T>(x);
  51.                 _head = newNode;
  52.             }
  53.             else
  54.             {
  55.                 GenericQueueNode<T> node = new GenericQueueNode<T>(x);
  56.                 GenericQueueNode<T> lastNode = getLastNode();
  57.                 lastNode.next = node;
  58.                 lastNode = node;
  59.             }
  60.  
  61.             count++;
  62.         }
  63.  
  64.         public T Get()
  65.         {      
  66.  
  67.             if (IsEmpty)
  68.                 throw new InvalidOperationException("D kju iz empti");
  69.  
  70.             T x = _head.Content;
  71.             _head = _head.next;
  72.  
  73.             count--;
  74.             return x;
  75.         }
  76.  
  77.         public override string ToString()
  78.         {
  79.             StringBuilder sb = new StringBuilder();
  80.  
  81.             GenericQueueNode<T> node = _head;
  82.             while (node != null)
  83.             {
  84.                 sb.AppendFormat("{0} ", node.Content);
  85.                 node = node.next;
  86.             }
  87.  
  88.             return sb.ToString();
  89.         }
  90.     }
  91.  
  92.     class Program
  93.     {
  94.         static void Main(string[] args)
  95.         {
  96.             GenericQueue<int> queue = new GenericQueue<int>();
  97.             queue.Put(1);
  98.             queue.Put(2);
  99.             queue.Put(3);
  100.             Console.WriteLine(queue.ToString());
  101.             Console.WriteLine("Skidam {0} ", queue.Get());
  102.             Console.WriteLine(queue.ToString());
  103.             queue.Put(4);
  104.             queue.Put(5);
  105.             Console.WriteLine(queue.ToString());
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement