Advertisement
StevanovicMilan

Zadatak 3

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