예제 #1
0
        public bool Delete(int position)
        {
            current = Head;

            if (position == 1)
            {
                Head = current.Next;
                current.Next = null;
                current = null;
                return true;
            }

            if (position > 1)
            {
                Node tempNode = Head;
                Node lastNode = null;
                int count = 0;

                while (tempNode != null)
                {
                    if (count == position - 1)
                    {
                        lastNode.Next = tempNode.Next;
                        return true;
                    }
                    count++;

                    lastNode = tempNode;
                    tempNode = tempNode.Next;
                }
            }
            return false;
        }
예제 #2
0
        /* REMOVE A NODE FROM THE LIST */
        // This is base 1, not base 0.
        public bool Delete(int position)
        {
            current = Head;                 // Set the current position we're working on to the first one in the list.

            /* Delete the first node. */
            if (position == 1)              // If the position is the very first node in the list (we want to delete the first one in the list)
            {
                Head = current.Next;            // Set the head to the next position (position 2) in the list
                current = null;                 // Set the current position to nothing
                return true;                    // Successfully deleted
            }

            /* Delete a node other than the first. */
            if (position > 1)
            {
                Node temporaryNode = Head;      // Make temporary node reference Head (first) node in the series
                Node lastNode = null;           // This node will be used for the delete
                int count = 0;                  // Counter to know if we have reached the position given as an argument (position) or not

                /* If the temporary node is NOT null we can continue to walk through this linked list */
                /* If it IS null however, we have reached the end of the list. */
                while (temporaryNode != null) {

                    // If the count is equal to the position entered (minus 1) then we have found the node we want to delete.
                    if (count == position - 1)
                    {
                        // Set the last node's Next node to the TempNode's Next node.
                        lastNode.Next = temporaryNode.Next;

                        // The temporary node's Next node now references nothing.
                        temporaryNode.Next = null;

                        // We found the node.
                        return true;
                    }

                    // Increment counter since we are moving foward in the list
                    count++;

                    // Set the last node equal to the tempnode. Now both variables are pointing to the same node.
                    lastNode = temporaryNode;

                    // Now set the tempnode to tempnode's Next node. This will move tempnode one more location forward in the list.
                    temporaryNode = temporaryNode.Next;
                }
            }

            // temporaryNode hit null, so we didn't delete anything (we couldn't find the node with that position)
            return false;
        }
예제 #3
0
        /* ADD A NODE TO THE LIST */
        public void Add(string content)
        {
            Node newNode = new Node();      // Create a new node to add to the list
            newNode.Data = content;         // Set the data to the passed given content string

            if (Head == null)               // Is the list empty?
            {
                Head = newNode;                 // Set the new node as the first node in the list
            }
            else
            {                               // Is the list not empty?
                current.Next = newNode;         // Make the current node that we are on (the last in the list)'s Next node reference this node.
            }

            current = newNode;              // Finally, set the last node in this Linked List to the just-added node.
        }
예제 #4
0
        public void Add(string content)
        {
            Node node = new Node();
            node.Data = content;

            if(Head == null)
            {
                Head = node;
            }
            else
            {
                current.Next = node;
            }

            current = node;
        }
예제 #5
0
        public bool Delete(int position)
        {
            // Sets current to start of linkedlist.
            current = Head;
            // If position to delete is on the very first node.
            if (position == 1)
            {
                // Set head to next node in list and remove this node's pointer so it will be put in garbage collection.
                Head = current.Next;
                // The next two statements remove all pointers to and from node so that it gauranteed will be picked up by garbage collection.
                current.Next = null;
                current = null;
                return true;
            }

            // Checks to make sure positive number was entered.
            // Should also check to make sure the position is less than the size of linkedlist.
            if (position > 1)
            {
                // Make a temp node that starts at the head.
                Node tempCurrentNode = Head;
                Node tempPreviousNode = null;
                // Starts a counter to  know if we reached the position
                int index = 0;

                // While current is not null, walk through list. If it is null, we reached the end.
                while (tempCurrentNode != null)
                {
                    // If index equals position - 1, then we have found the one to delete.
                    if (index == position - 1)
                    {
                        // Remove all pointers to and from found node to remove.
                        tempPreviousNode.Next = tempCurrentNode.Next;
                        tempCurrentNode.Next = null;
                        return true;
                    }
                    index++;

                    // Set both tempNodes to point to the same node. Then point tempNode to the next node.
                    tempPreviousNode = tempCurrentNode;
                    tempCurrentNode = tempCurrentNode.Next;
                }
            }
            return false;
        }
예제 #6
0
        public void Add(string content)
        {
            //Make a new node to add to the linked list
            Node node = new Node();
            //Set the data to the passed in content
            node.Data = content;

            //This will add the first element to our list
            if(Head == null)
            {
                Head = node;
            }
            //Not the first node, so set the new node to the current node's next variable
            else
            {
                last.Next = node;
            }
            //Move down the list. Set current to the new node we added.
            last = node;
        }
예제 #7
0
        /// <summary>
        /// Adds a new node to the linked list. Then specifies current node as newly created node.
        /// </summary>
        /// <param name="content">Content to add to new node.</param>
        public void Add(string content)
        {
            // Make a new node for linked list. Current node stays the same until later specified.
            Node node = new Node();
            // Adds passed in content to data field of node.
            node.Data = content;

            // Adds the first element to our list.
            if (Head == null)
            {
                Head = node;
            }
            // Not the first node so set the new node to current node's "next" variable.
            else
            {
                current.Next = node;
            }

            // Move down on list. Set current to node just added.
            current = node;
        }
예제 #8
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 second 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 successful so, return true
                return true;
            }

            //check to make sure that at least a positive number was entered
            //should also chack to make sure that the position is less than the
            //size of the array so that we areen'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
                Node tempNode = Head;
                //Set a last node to null. It will be used for the delete
                Node 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 tempNode's next property
                        //Jumping over tempNone. 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 successful
                        return true;
                    }
                    //increment the counter since we are going to move forward in the list
                    count++;
                    //Set the lastNode equal to the tmpNode. Now both variables are pointing to
                    //the exact same node.
                    previousTempNode = tempNode;
                    //Now set the tempNode to tempNode's 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;
        }