Advertisement
lidorcohen

C# Classes

Feb 1st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. ----------------------------------NODE----------------------------------
  2.  
  3. class Node<T>
  4. {
  5.     private T value;
  6.     private Node<T> next;
  7.  
  8.     public Node(T value)
  9.     {
  10.         this.value = value;
  11.         this.next = null;
  12.     }
  13.  
  14.     public Node(T value, Node<T> next)
  15.     {
  16.         this.value = value;
  17.         this.next = next;
  18.     }
  19.  
  20.     public T GetValue()
  21.     {
  22.         return this.value;
  23.     }
  24.  
  25.     public Node<T> GetNext()
  26.     {
  27.         return this.next;
  28.     }
  29.  
  30.     public void SetValue(T x)
  31.     {
  32.         this.value = x;
  33.     }
  34.  
  35.     public void SetNext(Node<T> x)
  36.     {
  37.         this.next = x;
  38.     }
  39.  
  40.     public override string ToString()
  41.     {
  42.         return this.value + " --> " + this.next;
  43.     }
  44. }
  45.  
  46. ----------------------------------STACK----------------------------------
  47.  
  48. class Stack<T>
  49. {
  50.     private Node<T> head;
  51.  
  52.     public Stack()
  53.     {
  54.         this.head = null;
  55.     }
  56.  
  57.     public void Push(T x)
  58.     {
  59.         Node<T> temp = new Node<T>(x);
  60.         temp.SetNext(head);
  61.         head = temp;
  62.     }
  63.  
  64.     public T Pop()
  65.     {
  66.         T x = head.GetValue();
  67.         head = head.GetNext();
  68.         return x;
  69.     }
  70.  
  71.     public T Top()
  72.     {
  73.         return head.GetValue();
  74.     }
  75.  
  76.     public bool IsEmpty()
  77.     {
  78.         return head == null;
  79.     }
  80.  
  81.     public override string ToString()
  82.     {
  83.         if (this.IsEmpty())
  84.             return "[]";
  85.         Stack<T> temp = new Stack<T>();
  86.         while (!this.IsEmpty())
  87.             temp.Push(this.Pop());
  88.         string s = "[";
  89.         while (!temp.IsEmpty())
  90.         {
  91.             s = s + temp.Top() + ',';
  92.             this.Push(temp.Pop());
  93.         }
  94.         s = s.Substring(0, s.Length - 1) + "]";
  95.         return s;
  96.     }
  97. }
  98.  
  99. ----------------------------------QUEUE----------------------------------
  100.  
  101. class Queue<T>
  102. {
  103.     private Node<T> first;
  104.     private Node<T> last;
  105.  
  106.     private Queue()
  107.     {
  108.         this.first = null;
  109.         this.last = null;
  110.     }
  111.  
  112.     public void Insert(T x)
  113.     {
  114.         Node<T> temp = new Node<T>(x);
  115.         if (first == null)
  116.             first = temp;
  117.         else
  118.             last.SetNext(temp);
  119.         last = temp;
  120.     }
  121.  
  122.     public T Remove()
  123.     {
  124.         T x = first.GetValue();
  125.         first = first.GetNext();
  126.         if (first == null)
  127.             last = null;
  128.         return x;
  129.     }
  130.  
  131.     public T head()
  132.     {
  133.         return first.GetValue();
  134.     }
  135.  
  136.     public bool IsEmpty()
  137.     {
  138.         return first == null;
  139.     }
  140.  
  141.     public override string ToString()
  142.     {
  143.         if (this.IsEmpty())
  144.             return "[]";
  145.         Node<T> temp0 = null;
  146.         this.Insert(temp0.GetValue());
  147.         string s = "[";
  148.         T temp = this.Remove();
  149.         while (temp != null)
  150.         {
  151.             s = s + temp + ",";
  152.             this.Insert(temp);
  153.             temp = this.Remove();
  154.         }
  155.         s = s.Substring(0, s.Length - 1) + "]";
  156.         return s;
  157.     }
  158. }
  159.  
  160. ----------------------------------BIN NODE----------------------------------
  161.  
  162. public class BinNode<T>
  163. {
  164.     private T value;
  165.     private BinNode<T> left;
  166.     private BinNode<T> right;
  167.  
  168.     public BinNode(T value)
  169.     {
  170.         this.value = value;
  171.         this.left = null;
  172.         this.right = null;
  173.     }
  174.  
  175.     public BinNode(BinNode<T> left, T value, BinNode<T> right)
  176.     {
  177.         this.value = value;
  178.         this.left = left;
  179.         this.right = right;
  180.     }
  181.  
  182.     public T GetValue()
  183.     {
  184.         return value;
  185.     }
  186.  
  187.     public BinNode<T> GetLeft()
  188.     {
  189.         return left;
  190.     }
  191.  
  192.     public BinNode<T> GetRight()
  193.     {
  194.         return right;
  195.     }
  196.  
  197.     public void SetInfo(T value)
  198.     {
  199.         this.value = value;
  200.     }
  201.  
  202.     public void SetLeft(BinNode<T> left)
  203.     {
  204.         this.left = left;
  205.     }
  206.  
  207.     public void SetRight(BinNode<T> right)
  208.     {
  209.         this.right = right;
  210.     }
  211.  
  212.     public bool HasLeft()
  213.     {
  214.         return (left != null);
  215.     }
  216.  
  217.     public bool HasRight()
  218.     {
  219.         return (right != null);
  220.     }
  221.  
  222.     public override string ToString()
  223.     {
  224.         return left + "->" + value + "  " + right;
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement