Beispiel #1
0
        public static void SwapNodes(DoubleLinkedList selectedList, Node nodeOne, Node nodeTwo) //swaps two nodes on the list based on the reference of the node.
        {
            if (nodeOne.next.next == nodeTwo.prev || nodeTwo.next.next == nodeOne.prev)         //Checks if the nodes are next to eachother.
            {
                if (nodeTwo.next == nodeOne.prev)                                               //Checks if they are in the correct order being passed in.
                {
                    SwapNodes(selectedList, nodeTwo, nodeOne);                                  //Re-runs SwapNodes changing the order of nodes passed in.
                }
                else //Runs if the pairs are in the right order.
                {
                    if (nodeOne.prev.prev == null && nodeTwo.next.next != null) //Checks if the pair is at the start of the list.
                    {
                        Node tempNode = new Node();                             //Copy of nodeOne
                        tempNode.next = nodeOne.next.next;
                        tempNode.prev = nodeOne.prev.prev;

                        Node nodeRightOfTwo = new Node(); //Copy of nodeTwo
                        nodeRightOfTwo.next = nodeTwo.next.next;
                        nodeRightOfTwo.prev = nodeTwo.next.prev;
                        nodeTwo.prev.prev   = null;

                        nodeTwo.next.next      = nodeOne.prev;
                        nodeOne.prev.prev      = nodeTwo.next;
                        nodeOne.next.next      = nodeRightOfTwo.next;
                        nodeOne.next.next.prev = nodeOne.next;
                        selectedList.head      = nodeTwo.prev;
                    }
                    else if (nodeTwo.next.next == null && nodeOne.prev.prev != null) //Checks if the pair is at the end of the list.
                    {
                        Node nodeLeftOfOne = new Node();
                        nodeLeftOfOne.prev = nodeOne.prev.prev;
                        nodeLeftOfOne.data = nodeOne.prev.data;
                        nodeLeftOfOne.next = nodeOne.prev.next;

                        nodeOne.next.next      = null;
                        nodeOne.prev.prev      = nodeTwo.next;
                        nodeTwo.next.next      = nodeOne.prev;
                        nodeTwo.prev.prev      = nodeLeftOfOne.prev;
                        nodeTwo.prev.prev.next = nodeTwo.prev;
                    }
                    else if (nodeTwo.next.next == null && nodeOne.prev.prev == null) //Checks if the pair is in a list of 2 length.
                    {
                        nodeTwo.next.next = nodeOne.prev;
                        nodeOne.prev.prev = nodeTwo.next;
                        nodeOne.next.next = null;
                        nodeTwo.prev.prev = null;
                        selectedList.head = nodeTwo.prev;
                    }
                    else //Checks if the pair is in a list greater than 3 but on neither end.
                    {
                        Node tempNodeLeftOne = new Node();
                        tempNodeLeftOne.prev = nodeOne.prev.prev;
                        tempNodeLeftOne.data = nodeOne.prev.data;
                        tempNodeLeftOne.next = nodeOne.prev.next;

                        nodeOne.prev.prev      = nodeTwo.next;
                        nodeOne.next.next      = nodeTwo.next.next;
                        nodeTwo.prev.prev      = tempNodeLeftOne.prev;
                        nodeTwo.next.next      = nodeOne.prev;
                        nodeTwo.prev.prev.next = nodeTwo.prev;
                        nodeOne.next.next.prev = nodeOne.next;
                    }
                }
            }
            else //Runs if the pairs are far apart.
            {
                if (nodeOne.prev.prev == null && nodeTwo.next.next != null) //Checks if the pair is at the start.
                {
                    Node tempNodeTwoFarLeft = new Node();
                    tempNodeTwoFarLeft.prev = nodeTwo.prev.prev;
                    tempNodeTwoFarLeft.data = nodeTwo.prev.data;
                    tempNodeTwoFarLeft.next = nodeTwo.prev.next;

                    Node tempNodeTwoFarRight = new Node();
                    tempNodeTwoFarRight.prev = nodeTwo.next.prev;
                    tempNodeTwoFarRight.data = nodeTwo.next.data;
                    tempNodeTwoFarRight.next = nodeTwo.next.next;

                    nodeTwo.prev.prev      = null;
                    nodeTwo.next.next      = nodeOne.next.next;
                    nodeTwo.next.next.prev = nodeTwo.next;
                    nodeOne.next.next      = tempNodeTwoFarRight.next;
                    nodeOne.prev.prev      = tempNodeTwoFarLeft.prev;
                    nodeOne.prev.prev.next = nodeOne.prev;
                    nodeOne.next.next.prev = nodeOne.next;
                    selectedList.head      = nodeTwo.prev;
                }
                else if (nodeTwo.next.next == null && nodeOne.prev.prev != null) //Checks if the pair is at the end of the list.
                {
                    Node tempNodeOneFarLeft = new Node();
                    tempNodeOneFarLeft.data = nodeOne.prev.data;
                    tempNodeOneFarLeft.prev = nodeOne.prev.prev;
                    tempNodeOneFarLeft.next = nodeOne.prev.next;

                    Node tempNodeOneFarRight = new Node();
                    tempNodeOneFarRight.data = nodeOne.next.data;
                    tempNodeOneFarRight.prev = nodeOne.next.prev;
                    tempNodeOneFarRight.next = nodeOne.next.next;

                    nodeOne.next.next      = null;
                    nodeOne.prev.prev      = nodeTwo.prev.prev;
                    nodeOne.prev.prev.next = nodeOne.prev;
                    nodeTwo.prev.prev      = tempNodeOneFarLeft.prev;
                    nodeTwo.next.next      = tempNodeOneFarRight.next;
                    nodeTwo.next.next.prev = nodeTwo.next;
                    nodeTwo.prev.prev.next = nodeTwo.prev;
                }
                else if (nodeTwo.next.next == null && nodeOne.prev.prev == null) //Checks if the pair are on opposite ends.
                {
                    Node tempNodeOneOppositeRight = new Node();
                    tempNodeOneOppositeRight.data = nodeOne.next.data;
                    tempNodeOneOppositeRight.prev = nodeOne.next.prev;
                    tempNodeOneOppositeRight.next = nodeOne.next.next;

                    nodeOne.next.next      = null;
                    nodeOne.prev.prev      = nodeTwo.prev.prev;
                    nodeOne.prev.prev.next = nodeOne.prev;
                    nodeTwo.prev.prev      = null;
                    nodeTwo.next.next      = tempNodeOneOppositeRight.next;
                    nodeTwo.next.next.prev = nodeTwo.next;
                    selectedList.head      = nodeTwo.prev;
                }
                else //Runs if the pair are in the centre.
                {
                    Node tempNodeOneCentreLeft = new Node();
                    tempNodeOneCentreLeft.data = nodeOne.prev.data;
                    tempNodeOneCentreLeft.prev = nodeOne.prev.prev;
                    tempNodeOneCentreLeft.next = nodeOne.prev.next;

                    Node tempNodeOneCentreRight = new Node();
                    tempNodeOneCentreRight.data = nodeOne.next.data;
                    tempNodeOneCentreRight.prev = nodeOne.next.prev;
                    tempNodeOneCentreRight.next = nodeOne.next.next;

                    nodeOne.prev.prev      = nodeTwo.prev.prev;
                    nodeOne.next.next      = nodeTwo.next.next;
                    nodeOne.next.next.prev = nodeOne.next;
                    nodeOne.prev.prev.next = nodeOne.prev;
                    nodeTwo.prev.prev      = tempNodeOneCentreLeft.prev;
                    nodeTwo.next.next      = tempNodeOneCentreRight.next;
                    nodeTwo.prev.prev.next = nodeTwo.prev;
                    nodeTwo.next.next.prev = nodeTwo.next;
                }
            }
        }
Beispiel #2
0
        public static void MainProgram()
        {
            DoubleLinkedList primaryList = new DoubleLinkedList(); //Creates the main list for the program.

            string fileName = "test.txt";                          //String to hold the location of the file, located in /bin/debug

            int commaCounter = 0;                                  //Counter for looping through the first and second name.

            StreamReader fileReader = new StreamReader(fileName);  //Creates a StreamReader that will read the file in /bin/debug.

            while (!fileReader.EndOfStream)                        //Executes until the end of the file
            {
                while (commaCounter != 2)                          //Loop for the first name and the second name.
                {
                    int arrayPos = 1;                              //Used to keep track of the position in the array.

                    char[] charArray = new char[1];                //A char array to hold the data that will be inserted into the node.

                    char testchar = Convert.ToChar(fileReader.Read());

                    while (testchar != ',')                      //Reads the file until a comma is found.
                    {
                        if ((int)Convert.ToChar(testchar) == 32) //Checks if the character is a space.
                        {
                            //Do Nothing.
                        }
                        else //Adds to the char array if it's an alphabetical character.
                        {
                            charArray = ResizeArray(charArray, arrayPos); //Resizes the array to accommodate for the next character.

                            charArray[arrayPos - 1] = testchar; //Adds the character into the array.

                            arrayPos++;                         //Increases the position in the char array.
                        }

                        testchar = Convert.ToChar(fileReader.Read()); //Updates the character to the next character in the file.
                    }

                    InsertLast(primaryList, charArray); //Inserts the new char array into a node, inserts the node into the list.

                    foreach (char x in charArray)       //Goes through all the elements in the char array
                    {
                        Console.Write(x);               //Writes the characters to the console.
                    }

                    Console.WriteLine(); //Adds a line space.

                    commaCounter++;
                }

                fileReader.Read();                                       //Reads the space after the last name.

                int newchar = fileReader.Read();                         //A new char used to hold the current char in the file.

                char[] numArray = new char[1];                           //A new char array to hold the numbers.

                int arrayNumPos = 1;                                     //Holds the current position in the array.

                while (newchar >= 48 && newchar <= 57)                   //Loop to make sure you only read if the value is a number.
                {
                    numArray = ResizeArray(numArray, arrayNumPos);       //Resizes the array to accommodate for the new character.

                    numArray[arrayNumPos - 1] = Convert.ToChar(newchar); //Adds the new character into the array.

                    newchar = fileReader.Read();                         //Updates the character.

                    arrayNumPos++;

                    if (newchar < 48 || newchar > 57) //Catch incase newchar is changed into something that is not a number.
                    {
                        break;                        //Exits the while loop.
                    }
                }

                //Prints all of the numbers.
                foreach (char num in numArray)
                {
                    Console.Write(num);
                }

                Console.WriteLine();               //Adds space between every 3 nodes (every person).

                fileReader.Read();                 //removes the \n that adds a new line.

                InsertLast(primaryList, numArray); //Inserts to the end of the array.

                commaCounter = 0;                  //Resets the comma counter.
            }

            //PrintSurname(primaryList); //prints all of the surnames in the list
            //PrintFirstname(primaryList); //prints all of the first names
            //InsertionSortByLastname(primaryList); //runs insertion sort on the last name
            //InsertionSortByFirstname(primaryList); //runs the insertion sort on the first name
            //InsertionBySortID(primaryList); runs the insertion sort
        }