public void Enqueue(int elementPriority, TData data)
        {
            var newNode = new NodePriority <TData>(elementPriority, data);

            // Insert In Beginning
            if (IsEmpty() || elementPriority.CompareTo(_frontNode.Priority) == -1)
            {
                newNode.Link = _frontNode;
                _frontNode   = newNode;
                return;
            }

            var currentNode = _frontNode;

            while (currentNode.Link != null)
            {
                if (currentNode.Link.Priority.CompareTo(elementPriority) > -1)
                {
                    break;
                }

                currentNode = currentNode.Link;
            }

            newNode.Link     = currentNode.Link;
            currentNode.Link = newNode;
        }
        public TData Dequeue()
        {
            if (IsEmpty())
            {
                throw new InvalidOperationException("Queue underflow!");
            }

            var dequeuedElement = _frontNode.Data;

            _frontNode = _frontNode.Link;

            return(dequeuedElement);
        }
Esempio n. 3
0
 public NodePriority(int priority, TData data)
 {
     Priority = priority;
     Data     = data;
     Link     = null;
 }
Esempio n. 4
0
 //Compare the NodePriority for sorting
 public int CompareTo(Node nodeToCompare)
 {
     return(NodePriority.CompareTo(nodeToCompare.NodePriority));
 }
Esempio n. 5
0
 //Compare the NodePriority for sorting (make new custom comparer)
 public int CompareTo(Node nodeToCompare)
 {
     //Compare the current node priority to the nodeToCompare's node priority
     return(NodePriority.CompareTo(nodeToCompare.NodePriority));
 }
        // No rear part is required as we will enqueue data based on their priority in Sorted Linked List

        public PriorityQueueLinkedList()
        {
            _frontNode = null;
        }
Esempio n. 7
0
 public Node(Func <Node, object> evaluateNode, NodeType type, NodePriority priority)
 {
     _evaluate = () => evaluateNode(this);
     Type      = type;
     Priority  = priority;
 }