Exemple #1
0
 //Constructor. It will set the Head to null because there is nothing in the list yet.
 public GenericLinkedList()
 {
     Head = null;
 }
Exemple #2
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);
        }
        static void Main(string[] args)
        {
            //Define a new linked list to use
            MyLinkedList myLinkedList = new MyLinkedList();

            //Add a bunch of stuff to it
            myLinkedList.Add("first");
            myLinkedList.Add("second");
            myLinkedList.Add("third");
            myLinkedList.Add("fourth");

            //Loop through with this differently looking for loop to output
            //In here, the first part is initalization: Setting x to the Head
            //the second part is the test: If x != null, keep going
            //The last part is: Set the current x to x's next porinter. (The next in the list)
            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Print out the 2nd one
            Node nodeINeed = myLinkedList.Retrive(2);

            Console.WriteLine(nodeINeed.Data);

            //Print out the 2nd one again in one statement
            Console.WriteLine(myLinkedList.Retrive(2).Data);

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Delete the 2nd element in the list
            myLinkedList.Delete(2);
            //Delete the new 2nd element in the list. Was 3rd before previous delete
            myLinkedList.Delete(2);

            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Add two new ones to the list
            myLinkedList.Add("fifth");
            myLinkedList.Add("sixth");

            //Print the list one last time
            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }


            Console.WriteLine("***************************************");
            Console.WriteLine("***************************************");

            //A generic linked list that sends in the type that we would like to use
            //This one will behave exactly like the one used above since it is taking a
            //string.
            GenericLinkedList <string> myGenericLinkedList = new GenericLinkedList <string>();

            //Some other linked lists that can use the generic one. One of them is of type
            //integer, and the other is of type Object
            GenericLinkedList <int>    myOtherGenericLinkedLIst  = new GenericLinkedList <int>();
            GenericLinkedList <Object> myObjectGenericLinkedList = new GenericLinkedList <object>();

            //Use the generic string one to do the same work as above
            //Add a bunch of stuff to it
            myGenericLinkedList.Add("first");
            myGenericLinkedList.Add("second");
            myGenericLinkedList.Add("third");
            myGenericLinkedList.Add("fourth");

            //Loop through with this differently looking for loop to output
            //In here, the first part is initalization: Setting x to the Head
            //the second part is the test: If x != null, keep going
            //The last part is: Set the current x to x's next porinter. (The next in the list)
            for (GenericNode <string> x = myGenericLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }
        }