コード例 #1
0
        // Public method to sort the droids by type:
        public void SortDroidsByType()
        {
            // Instantiate the GenericStack for each type of droid:
            GenericStack <Droid> protocolStack   = new GenericStack <Droid>();
            GenericStack <Droid> utilityStack    = new GenericStack <Droid>();
            GenericStack <Droid> janitorialStack = new GenericStack <Droid>();
            GenericStack <Droid> astromechStack  = new GenericStack <Droid>();
            // Instantiate the GenericQueue to be used:
            GenericQueue <Droid> droidQueue = new GenericQueue <Droid>();

            // Loop through the droidCollection and sort the droids by type, putting into their stacks:
            for (int i = 0; i < lengthOfCollection; i++)
            {
                // If the droid is of type ProtocolDroid, add to the protocolStack:
                if (droidCollection[i].GetType() == typeof(ProtocolDroid))
                {
                    protocolStack.AddToFront(droidCollection[i]);
                }
                // If the droid is of type UtilityDroid, add to the utilityStack:
                else if (droidCollection[i].GetType() == typeof(UtilityDroid))
                {
                    utilityStack.AddToFront(droidCollection[i]);
                }
                // If the droid is of type JanitorDroid, add to the janitorialStack:
                else if (droidCollection[i].GetType() == typeof(JanitorDroid))
                {
                    janitorialStack.AddToFront(droidCollection[i]);
                }
                // Else, the droid is of type AstromechDroid, so add to the astromechStack:
                else
                {
                    astromechStack.AddToFront(droidCollection[i]);
                }
            }
            // Place the droids in the queue in the desired order (astromech, janitor, utility, protocol)
            // by removing the current droid from the appropriate stack and adding it to the back of the queue
            // until the size of the current stack is 0:
            while (astromechStack.Size > 0)
            {
                droidQueue.AddToBack(astromechStack.RemoveFromFront());
            }
            while (janitorialStack.Size > 0)
            {
                droidQueue.AddToBack(janitorialStack.RemoveFromFront());
            }
            while (utilityStack.Size > 0)
            {
                droidQueue.AddToBack(utilityStack.RemoveFromFront());
            }
            while (protocolStack.Size > 0)
            {
                droidQueue.AddToBack(protocolStack.RemoveFromFront());
            }

            // Replace the droids in droidCollection with the now sorted droid collection from the droidQueue:
            for (int i = 0; i < lengthOfCollection; i++)
            {
                droidCollection[i] = droidQueue.RemoveFromFront();
            }
        }
コード例 #2
0
        public void DroidsByType()
        {
            //Individual stacks for the droids by type
            GenericStack <Droid> Protocols  = new GenericStack <Droid>();
            GenericStack <Droid> Astromechs = new GenericStack <Droid>();
            GenericStack <Droid> Janitors   = new GenericStack <Droid>();
            GenericStack <Droid> Utilities  = new GenericStack <Droid>();

            //One single queue that will hold the sorted droids. They will be inserted by type
            //after that have been soted into there respective stack
            GenericQueue <Droid> DroidQueue = new GenericQueue <Droid>();

            //Bubble sort for the droids by type
            for (int i = 0; i < lengthOfCollection; i++)
            {
                if (droidCollection[i].GetType() == typeof(ProtocolDroid))
                {
                    Protocols.AddToFront(droidCollection[i]);
                }
                if (droidCollection[i].GetType() == typeof(AstromechDroid))
                {
                    Astromechs.AddToFront(droidCollection[i]);
                }
                if (droidCollection[i].GetType() == typeof(JanitorDroid))
                {
                    Janitors.AddToFront(droidCollection[i]);
                }
                if (droidCollection[i].GetType() == typeof(UtilityDroid))
                {
                    Utilities.AddToFront(droidCollection[i]);
                }
            }

            //Add the droids to the queue for further sorting and allowing printing ong
            //only one array - this will empty the current collections and place them in
            //a new one
            while (Astromechs.Size > 0)
            {
                DroidQueue.AddToBack(Astromechs.RemoveFromFront());
            }
            while (Janitors.Size > 0)
            {
                DroidQueue.AddToBack(Janitors.RemoveFromFront());
            }
            while (Utilities.Size > 0)
            {
                DroidQueue.AddToBack(Utilities.RemoveFromFront());
            }
            while (Protocols.Size > 0)
            {
                DroidQueue.AddToBack(Protocols.RemoveFromFront());
            }

            //Reload the droid collection
            for (int r = 0; r < lengthOfCollection; r++)
            {
                droidCollection[r] = DroidQueue.RemoveFromFront();
            }
        }