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);
        }
Example #3
0
 public NodePriority(int priority, TData data)
 {
     Priority = priority;
     Data     = data;
     Link     = null;
 }
Example #4
0
 //Compare the NodePriority for sorting
 public int CompareTo(Node nodeToCompare)
 {
     return(NodePriority.CompareTo(nodeToCompare.NodePriority));
 }
Example #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;
        }
Example #7
0
 public Node(Func <Node, object> evaluateNode, NodeType type, NodePriority priority)
 {
     _evaluate = () => evaluateNode(this);
     Type      = type;
     Priority  = priority;
 }