Advertisement
StevanovicMilan

Zadatak 4

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