Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------NODE----------------------------------
- class Node<T>
- {
- private T value;
- private Node<T> next;
- public Node(T value)
- {
- this.value = value;
- this.next = null;
- }
- public Node(T value, Node<T> next)
- {
- this.value = value;
- this.next = next;
- }
- public T GetValue()
- {
- return this.value;
- }
- public Node<T> GetNext()
- {
- return this.next;
- }
- public void SetValue(T x)
- {
- this.value = x;
- }
- public void SetNext(Node<T> x)
- {
- this.next = x;
- }
- public override string ToString()
- {
- return this.value + " --> " + this.next;
- }
- }
- ----------------------------------STACK----------------------------------
- class Stack<T>
- {
- private Node<T> head;
- public Stack()
- {
- this.head = null;
- }
- public void Push(T x)
- {
- Node<T> temp = new Node<T>(x);
- temp.SetNext(head);
- head = temp;
- }
- public T Pop()
- {
- T x = head.GetValue();
- head = head.GetNext();
- return x;
- }
- public T Top()
- {
- return head.GetValue();
- }
- public bool IsEmpty()
- {
- return head == null;
- }
- public override string ToString()
- {
- if (this.IsEmpty())
- return "[]";
- Stack<T> temp = new Stack<T>();
- while (!this.IsEmpty())
- temp.Push(this.Pop());
- string s = "[";
- while (!temp.IsEmpty())
- {
- s = s + temp.Top() + ',';
- this.Push(temp.Pop());
- }
- s = s.Substring(0, s.Length - 1) + "]";
- return s;
- }
- }
- ----------------------------------QUEUE----------------------------------
- class Queue<T>
- {
- private Node<T> first;
- private Node<T> last;
- private Queue()
- {
- this.first = null;
- this.last = null;
- }
- public void Insert(T x)
- {
- Node<T> temp = new Node<T>(x);
- if (first == null)
- first = temp;
- else
- last.SetNext(temp);
- last = temp;
- }
- public T Remove()
- {
- T x = first.GetValue();
- first = first.GetNext();
- if (first == null)
- last = null;
- return x;
- }
- public T head()
- {
- return first.GetValue();
- }
- public bool IsEmpty()
- {
- return first == null;
- }
- public override string ToString()
- {
- if (this.IsEmpty())
- return "[]";
- Node<T> temp0 = null;
- this.Insert(temp0.GetValue());
- string s = "[";
- T temp = this.Remove();
- while (temp != null)
- {
- s = s + temp + ",";
- this.Insert(temp);
- temp = this.Remove();
- }
- s = s.Substring(0, s.Length - 1) + "]";
- return s;
- }
- }
- ----------------------------------BIN NODE----------------------------------
- public class BinNode<T>
- {
- private T value;
- private BinNode<T> left;
- private BinNode<T> right;
- public BinNode(T value)
- {
- this.value = value;
- this.left = null;
- this.right = null;
- }
- public BinNode(BinNode<T> left, T value, BinNode<T> right)
- {
- this.value = value;
- this.left = left;
- this.right = right;
- }
- public T GetValue()
- {
- return value;
- }
- public BinNode<T> GetLeft()
- {
- return left;
- }
- public BinNode<T> GetRight()
- {
- return right;
- }
- public void SetInfo(T value)
- {
- this.value = value;
- }
- public void SetLeft(BinNode<T> left)
- {
- this.left = left;
- }
- public void SetRight(BinNode<T> right)
- {
- this.right = right;
- }
- public bool HasLeft()
- {
- return (left != null);
- }
- public bool HasRight()
- {
- return (right != null);
- }
- public override string ToString()
- {
- return left + "->" + value + " " + right;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement