예제 #1
0
        // GetLastNode. Returns the last node of the queue
        private NodeInt GetLastNode()
        {
            NodeInt temp = head;

            while (temp.Next != null)
            {
                temp = temp.Next;
            }
            return(temp);
        }
예제 #2
0
 // Print. Prints the objects from the begining to the end
 public void Print()
 {
     if (IsEmpty)
     {
         Console.WriteLine("Empty queue. (Print)");
     }
     else
     {
         NodeInt temp = head;
         while (temp.Next != null)
         {
             Console.Write(temp.Data + ",");
             temp = temp.Next;
         }
         Console.WriteLine(temp.Data);
     }
 }
예제 #3
0
        // EnqueueAscending. Adds an object (int) to the list sorted in an ascending way
        public void EnqueueAscending(int newData)
        {
            NodeInt newNode      = new NodeInt(newData);
            NodeInt actualNode   = head;
            NodeInt previousNode = head;

            // If the queue is empty
            if (IsEmpty)
            {
                head = newNode;
            }
            else
            {
                NodeInt lastNode = GetLastNode();

                // If the newData is lower than the first node's data
                if (newData <= head.Data)
                {
                    newNode.Next = head;
                    head         = newNode;
                }
                else
                {
                    // if the newData is greater than the last node's data
                    if (newData > lastNode.Data)
                    {
                        lastNode.Next = newNode;
                    }
                    else
                    {
                        // otherwise
                        while (newData > actualNode.Data && actualNode.Next != null)
                        {
                            previousNode = actualNode;
                            actualNode   = actualNode.Next;
                        }
                        previousNode.Next = newNode;
                        newNode.Next      = actualNode;
                    }
                }
            }
            Count++;
        }
예제 #4
0
        // PrintEven.Returns a string containing the odd numbers separated by comas
        public string PrintOdd()
        {
            string  oddNumbers = string.Empty;;
            NodeInt temp       = head;
            NodeInt lastNode   = GetLastNode();

            if (IsEmpty == false)
            {
                while (temp.Next != null)
                {
                    if (temp.Data % 2 != 0)
                    {
                        oddNumbers += temp.Data + ",";
                    }
                    temp = temp.Next;
                }
                if (lastNode.Data % 2 != 0)
                {
                    oddNumbers += lastNode.Data + ",";
                }
            }
            return(oddNumbers[0..^ 1]);
예제 #5
0
        // PrintEven.Returns a string containing the even numbers separated by comas
        public string PrintEven()
        {
            string  evenNumbers = string.Empty;;
            NodeInt temp        = head;
            NodeInt lastNode    = GetLastNode();

            if (IsEmpty == false)
            {
                while (temp.Next != null)
                {
                    if (temp.Data % 2 == 0)
                    {
                        evenNumbers += temp.Data + ",";
                    }
                    temp = temp.Next;
                }
                if (lastNode.Data % 2 == 0)
                {
                    evenNumbers += lastNode.Data + ",";
                }
            }
            return(evenNumbers.Substring(0, evenNumbers.Length - 1));
        }
예제 #6
0
 // Constructor
 public PriorityQueue()
 {
     head  = null;
     Count = 0;
 }