Beispiel #1
0
		public void SortByType()
		{

            //Bucket Sort
            /*
            A arry to be sorted
            n is length of A
            for i = 1 to n
            insert A[i] into B bucket (queue)
            for i = 0 to n-1
            sort B
            */

            //Generic Stack for each droid type
            Stack<Droid> protocolStack = new Stack<Droid>();
            Stack<Droid> utilityStack = new Stack<Droid>();
            Stack<Droid> janitorStack = new Stack<Droid>();
            Stack<Droid> astromechStack = new Stack<Droid>();

            //Create a Queue for Droids
            Queue<Droid> droidQueue = new Queue<Droid>();
            //Create a linked list for the droids
            LinkedList<Droid> droidLinkedList = new LinkedList<Droid>();

            int n = droidCollection.Length;

            //Move droid type into the bucket
            foreach (int i in droidCollection)
            {
                //check if value is not null
                if(droidCollection[i] != null)
                {
                    //Add a droid to the linked list
                    droidLinkedList.AddLast(droidCollection);
                }
                //Add a new node for the data in the linked list
                GenericNode<Droid> node = new GenericNode<Droid>();
                node.Next;
                droidQueue.enqueue(droidLinkedList);

            }

            //Push the droids into the queue(enqueue) and sort by pushing into their respected stacks

            foreach (int i in droidQueue)
            {
                switch (droidQueue.Equals)
                {
                    case "Protocol":
                        //take out of queue
                        //Add to the respected stack
                        protocolStack.push(droidQueue.dequeue());
                        break;
                    case "Utility":
                        utilityStack.push(droidQueue.dequeue());
                        break;
                    case "Janitor":
                        droidQueue.dequeue();
                        janitorStack.push(droidQueue.dequeue());
                        break;
                    case "Astromech":
                        droidQueue.dequeue();
                        astromechStack.push(droidQueue.dequeue());
                        break;
                }
            }
            }
Beispiel #2
0
 //Constructor. It will set the Head to null because there is nothing in the list yet.
 public LinkedList()
 {
     Head = null;
 }
Beispiel #3
0
        public bool Delete(int Position)
        {
            //Set current to Head. Need to walk through it from the beginning
            current = Head;

            //If the position is the very first node in the list
            if (Position == 1)
            {
                //Set the Head to the next node in the list. This will be the 2nd one.
                Head = current.Next;
                //Delete the current.Next pointer so there is no reference from current to
                //another node
                current.Next = null;
                //current = null because we want the garbage collector to come pick it up.
                current = null;
                //it was successfull so, return true;
                return(true);
            }
            //Check to make sure that at least a positive number was entered.
            //Should also check to make sure that the position is less than the
            //size of the array so that we aren't looking for something that doesn't
            //exist. Adding a size property will be more work.
            //TODO: Add a size property
            if (Position > 1)
            {
                //Make a temp node that starts at the Head. This way we don't need to actually
                //move the Head pointer. We can just use the temp node
                GenericNode <T> tempNode = Head;
                //Set a previous node to null. It will be used for the delete
                GenericNode <T> previousTempNode = null;
                //Start a counter to know if we have reached the position yet or not.
                int count = 0;

                //while the tempNode is NOT null, we can continue to walk through the
                //linked list. If it is null, then we have reached the end.
                while (tempNode != null)
                {
                    //If the count is the same as the position entered - 1, then we have found
                    //the one we would like to delete.
                    if (count == Position - 1)
                    {
                        //Set the last node's Next property to the TempNodes Next Property
                        //Jumping over the tempNode. The previous node's next will now point
                        //to the node AFTER the tempNode
                        previousTempNode.Next = tempNode.Next;

                        if (tempNode.Next == null)
                        {
                            last = previousTempNode;
                        }
                        //Remove the next pointer of the tempnode
                        tempNode.Next = null;
                        //Return True because it was successfull
                        return(true);
                    }
                    //Increment the counter since we are going to move forward in the list
                    count++;
                    //Set the lastNode equal to the tempNode. Now both variables are pointing to
                    //the exact same node.
                    previousTempNode = tempNode;
                    //Now set the tempNode to tempNodes Next node. This will move tempNode
                    //one more location forward in the list.
                    tempNode = tempNode.Next;
                }
            }
            //tempNode became null, so apparently we did not find it. Return false.
            return(false);
        }
Beispiel #4
0
 public GenericQueue()
 {
     Head = null;
 }