StevanovicMilan

Zadatak 2

Oct 31st, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 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 StackLNode
  10.     {
  11.         public StackLNode(int content)
  12.         {
  13.             Content = content;
  14.         }
  15.         public int Content { get; set; }
  16.         public StackLNode next { get; set; }
  17.  
  18.     }
  19.     class StackL
  20.     {
  21.         private StackLNode _head;
  22.         private int count = 0;
  23.  
  24.         public bool IsEmpty
  25.         {
  26.             get { return _head == null; }
  27.         }
  28.  
  29.         public int Count { get { return count; } set { count = value; } }
  30.  
  31.         public void Push(int x)
  32.         {
  33.             StackLNode newNode = new StackLNode(x);
  34.             newNode.next = _head;
  35.             _head = newNode;
  36.             count++;
  37.         }
  38.  
  39.         public int Pop()
  40.         {
  41.             if (IsEmpty)
  42.                 throw new InvalidOperationException("The stack is empty.");
  43.             int x = _head.Content;
  44.             _head = _head.next;
  45.             count--;
  46.             return x;
  47.         }
  48.  
  49.         public override string ToString()
  50.         {
  51.             StringBuilder sb = new StringBuilder();
  52.  
  53.             StackLNode node = _head;
  54.             while (node != null)
  55.             {
  56.                 sb.AppendFormat("{0} ", node.Content);
  57.                 node = node.next;
  58.             }
  59.  
  60.             return sb.ToString();
  61.  
  62.  
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment