כל מבני הנתונים של משרד החינוך (node , binnode , queue , stack) בC#

לחזרה לדף הסיכומים ועבודות - לחצו כאן

המחלקה Node<T> - חוליה
    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 bool HasNext()
        {
            return this.GetNext() != null;
        }
        public void SetValue(T value)
        {
            this.value = value;
        }

        public void SetNext(Node<T> next)
        {
            this.next = next;
        }

        public override string ToString()
        {
            Node<T> pos=this;
            string str="[";
            while (pos.HasNext())
            {
                str += pos.value + ",";
                pos = pos.next;
            }
            str += pos.value + "]";
            return str;
         }
      }
  המחלקה Stack<T> - מחסנית
   class Stack<T>
    {
        private Node<T> first;

        public Stack()
        {
            this.first = null;
        }

        public bool IsEmpty()
        {
            return this.first == null;
        }

        public void Push(T x)
        {
            this.first = new Node<T>(x, this.first);
        }

        public T Top()
        {
            return this.first.GetValue();
        }

        public T Pop()
        {
            T x = this.first.GetValue();
            this.first = this.first.GetNext();
            return x;
        }

        public override string ToString()
        {
            string str = "[";
            Node<T> pos = this.first;
            while (pos != null)
            {
                str = str + pos.GetValue().ToString();
                if (pos.HasNext())
                    str = str + ",";
                pos = pos.GetNext();
            }
            str = str + "]";
            return str;
        }
    }


המחלקה Queue<T>  - תור
    class Queue<T>
    {
        private Node<T> first;
        private Node<T> last;


        public Queue()
        {
            this.first = null;
            this.last = null;
        }
        public void Insert(T value)
        {
            Node<T> newNode = new Node<T>(value);
            if (this.first == null)
                   this.first = newNode;
             else
                this.last.SetNext(newNode);
            this.last = newNode;
        }
        public T Remove()
        {
            T value = this.first.GetValue();
            this.first = this.first.GetNext();
            if (this.first == null)
                this.last = null;
            return value;
        }
        public T Head()
        {
            return this.first.GetValue();
        }

        public bool IsEmpty()
        {
            return this.first == null;
        }

        public override string ToString()
        {
            return "" + this.first;
        }
    }


המחלקה BinNode<int> - צומת

    class BinNode<T>
    {
        private BinNode<T> left;
        private T value;
        private BinNode<T> right;

        public BinNode(T value)
        {
            this.left = null;
            this.value = value;
            this.right = null;
        }
        public BinNode(BinNode<T> left, T value, BinNode<T> right)
        {
            this.left = left;
            this.value = value;
            this.right = right;
        }
        public T GetValue()                   
        {       return this.value;    }
        public BinNode<T> GetLeft()  
        {       return this.left;        }
        public BinNode<T> GetRight()  
        {       return this.right;     }
        public void Setvalue(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 this.left != null;
        }
        public bool HasRight()
        {
            return this.right != null;
        }
        public override string ToString()
        {
            return this.value.ToString();
        }
 }


3 תגובות: