コード例 #1
0
        /// <summary>
        /// Removes specific node from linked list.
        /// </summary>
        /// <param name="list">The list is an instance of the Linked List.</param>
        /// <param name="data">The data to be removed from linked list.</param>
        /// <returns>Sorted Linked List</returns>
        public SortedLinkedList RemoveAtPositionFromLinkedList(SortedLinkedList list, int data)
        {
            NewNode <int> currentNode = list.Head;

            ////Traverse till end
            while (currentNode != null)
            {
                //// check if required node is the Head node
                if (currentNode == list.Head && list.Head.NodeData.Equals(data))
                {
                    ////if found then delete the Head
                    list.Head = currentNode.Next;
                    GC.Collect();
                    return(list);
                }
                else if (currentNode.Next.NodeData.Equals(data))
                {
                    currentNode.Next = currentNode.Next.Next;
                    return(list);
                }
                else
                {
                    //// break the links
                    currentNode = currentNode.Next;
                }
            }

            return(list);
        }
コード例 #2
0
        /// <summary>
        /// Prints the linked list.
        /// </summary>
        /// <param name="list">The list is an instance of linked list.</param>
        /// <returns>sorted list</returns>
        public SortedLinkedList PrintLinkedList(SortedLinkedList list)
        {
            //// The current node is temporary node for traversing into Linked List
            NewNode <int> currentNode = list.Head;

            if (list.Head == null)
            {
                Console.WriteLine("\nEmpty Linked List...");
            }
            else
            {
                Console.WriteLine("Linked List :\n");
                Console.Write("Head");

                // Traverse till the end of the list....
                while (currentNode != null)
                {
                    ////print the data of each node
                    Console.WriteLine(" -> " + currentNode.NodeData);
                    currentNode = currentNode.Next;
                }
            }

            return(list);
        }
コード例 #3
0
        /// <summary>
        /// Removes the last node from linked list.
        /// </summary>
        /// <param name="list">The list is an instance of the Linked List.</param>
        /// <returns>Sorted Linked List</returns>
        public SortedLinkedList RemoveLastFromLinkedList(SortedLinkedList list)
        {
            ////check if Linked List is present or not
            if (list.Head == null)
            {
                Console.WriteLine("\nEmpty Linked List...");
            }
            else
            {
                //// The current node is temporary node for traversing into Linked List
                NewNode <int> currentNode = list.Head;

                // Traverse till the end of the list....
                while (currentNode.Next.Next != null)
                {
                    currentNode = currentNode.Next;
                }

                // Add new node as the Next node to the last node.
                currentNode.Next = null;

                //// Free the memory space aquired by node
                GC.Collect();
            }

            return(list);
        }
コード例 #4
0
        /// <summary>
        /// Adds the into linked list.
        /// </summary>
        /// <param name="list">The list is an instance of Linked List.</param>
        /// <param name="data">The data is to be added into the Linked List.</param>
        /// <returns>updated list</returns>
        public SortedLinkedList AddLastIntoLinkedList(SortedLinkedList list, int data)
        {
            NewNode <int> newNode = new NewNode <int>();

            newNode.NodeData = data;
            newNode.Next     = null;
            if (list.Head == null)
            {
                list.Head = newNode;
            }
            else
            {
                //// The current node is temporary node for traversing into Linked List
                NewNode <int> currentNode = list.Head;

                // Traverse till the end of the list....
                while (currentNode.Next != null)
                {
                    currentNode = currentNode.Next;
                }

                // Add new node as the Next node to the last node.
                currentNode.Next = newNode;
            }

            return(list);
        }
コード例 #5
0
        /// <summary>
        /// Removes the first node from linked list.
        /// </summary>
        /// <param name="list">The list is as instance of the Linked List.</param>
        /// <returns>Sorted  Linked List</returns>
        public SortedLinkedList RemoveFirstFromLinkedList(SortedLinkedList list)
        {
            NewNode <int> currentNode = list.Head;

            //// check if required node is the Head node
            if (currentNode == list.Head)
            {
                ////delete the Head
                list.Head = currentNode.Next;
                GC.Collect();
            }

            return(list);
        }
コード例 #6
0
        /// <summary>
        /// Searches the node into the linked list.
        /// </summary>
        /// <param name="list">The list is an instance of the Linked List.</param>
        /// <param name="data">The data to be searched.</param>
        /// <returns>Sorted list</returns>
        public bool SearchIntoLinkedList(SortedLinkedList list, int data)
        {
            //// The current node is temporary node for traversing into Linked List
            NewNode <int> currentNode = list.Head;

            while (currentNode != null)
            {
                if (object.Equals(currentNode.NodeData, data))
                {
                    return(true);
                }

                currentNode = currentNode.Next;
            }

            return(false);
        }
コード例 #7
0
        /// <summary>
        /// Adds the node into sorted linked list.
        /// </summary>
        /// <param name="list">The list is an instance of the Linked List.</param>
        /// <param name="data">The data is to be get added into list.</param>
        /// <returns>sorted linked list</returns>
        public SortedLinkedList AddIntoSortedLinkedList(SortedLinkedList list, int data)
        {
            NewNode <int> newNode = new NewNode <int>();

            newNode.NodeData = data;
            newNode.Next     = null;
            NewNode <int> currentNode = list.Head;

            //// check if required node is the Head node
            if (list.Head == null)
            {
                ////
                newNode.Next   = list.Head.Next;
                list.Head.Next = newNode;
                return(list);
            }

            ////Traverse till end
            while (currentNode != null)
            {
                if (list.Head.NodeData > data)
                {
                    newNode.Next = list.Head;
                    list.Head    = newNode;
                }

                ////Compare the position of the new node to be added into the list
                if (currentNode.NodeData < data && currentNode.Next.NodeData > data)
                {
                    newNode.Next     = currentNode.Next;
                    currentNode.Next = newNode;
                }

                currentNode = currentNode.Next;
            }

            return(list);
        }
コード例 #8
0
        /// <summary>
        /// Sorts the linked list.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns>sorted linked list</returns>
        public SortedLinkedList SortLinkedList(SortedLinkedList list)
        {
            NewNode <int> currentNode = list.Head;

            NewNode <int> tempNode = new NewNode <int>();

            ////Traverse till end
            while (currentNode != null)
            {
                NewNode <int> currentNode1 = currentNode.Next;

                while (currentNode1 != null)
                {
                    //// check if required node is the Head node
                    if (list.Head == null)
                    {
                        Console.WriteLine("\nEmpty List...");
                    }

                    ////if first is greater then swap
                    if (currentNode.NodeData > currentNode1.NodeData)
                    {
                        ////Swap the elements
                        tempNode.NodeData     = currentNode.NodeData;
                        currentNode.NodeData  = currentNode1.NodeData;
                        currentNode1.NodeData = tempNode.NodeData;
                    }

                    //// break the links
                    currentNode1 = currentNode1.Next;
                }

                currentNode = currentNode.Next;
            }

            return(list);
        }
コード例 #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadIntegerFromFile{T}"/> class.
        /// </summary>
        public void OrderedListOperation()
        {
            try
            {
                char ans;
                do
                {
                    Console.WriteLine("1.Read From File To Linked List");
                    Console.WriteLine("2.Print From Linked List");
                    Console.WriteLine("3.Insert node into Linked List");
                    Console.WriteLine("4.Remove first node from Linked List");
                    Console.WriteLine("5.Remove last node from Linked List");
                    Console.WriteLine("6.Remove specific node from Linked List");
                    Console.WriteLine("7.Search string into Linked List");
                    int ch = Convert.ToInt32(Console.ReadLine());
                    switch (ch)
                    {
                    case 1:
                        this.text = File.ReadAllText(Utility <int> .GetIntegerInputPath());
                        string[] number  = this.text.Split('\n');
                        int[]    numbers = new int[number.Length];
                        for (int n = 0; n < number.Length; n++)
                        {
                            numbers[n] = int.Parse(number[n]);
                        }

                        ////Add numbers read from file into linked list
                        foreach (int value in numbers)
                        {
                            this.SortList = this.SortList.AddLastIntoLinkedList(this.SortList, value);
                        }

                        break;

                    case 2:

                        ////print the linked list
                        Console.Write("\nnumbers read from Linked List are :\n");
                        this.SortList = this.SortList.SortLinkedList(this.SortList);
                        this.SortList = this.SortList.PrintLinkedList(this.SortList);
                        break;

                    case 3:

                        ////Insert after given node
                        Console.WriteLine("Enter number to add : ");
                        int input1 = Convert.ToInt32(Console.ReadLine());
                        this.SortList = this.SortList.AddIntoSortedLinkedList(this.SortList, input1);
                        Console.WriteLine("\nAfter Insering : ");
                        this.SortList = this.SortList.PrintLinkedList(this.SortList);
                        break;

                    case 4:

                        ////Remove first element
                        this.SortList = this.SortList.RemoveFirstFromLinkedList(this.SortList);
                        Console.Write("\nNode deleted from LinkedList : ");
                        break;

                    case 5:

                        ////Remove last node from linked list
                        this.SortList = this.SortList.RemoveLastFromLinkedList(this.SortList);
                        Console.Write("\nNode deleted from LinkedList : ");
                        break;

                    case 6:

                        ////Remove specific node
                        Console.Write("\nEnter the number you want to delete from LinkedList : ");
                        int data = Convert.ToInt32(Console.ReadLine());
                        this.SortList = this.SortList.RemoveAtPositionFromLinkedList(this.SortList, data);
                        Console.Write("\nNode deleted from LinkedList : ");
                        break;

                    case 7:

                        ////Search into linked list
                        Console.Write("\nEnter the word to search from LinkedList : ");
                        int  input2 = Convert.ToInt32(Console.ReadLine());
                        bool flag   = this.SortList.SearchIntoLinkedList(this.SortList, input2);

                        ////if found then remove else add into linked list
                        if (flag == true)
                        {
                            this.SortList = this.SortList.RemoveAtPositionFromLinkedList(this.SortList, input2);
                            Console.WriteLine("\nNumber Found... Removing that number from Linked List...");
                        }
                        else
                        {
                            this.SortList = this.SortList.AddLastIntoLinkedList(this.SortList, input2);
                            Console.WriteLine("\nNumber not Found... Adding that number into Linked List...");
                        }

                        break;

                    default:
                        break;
                    }

                    Console.WriteLine("Do you want to operate more on Linked List (y/n) : ");
                    ans = Convert.ToChar(Console.ReadLine());
                }while (ans == 'Y' || ans == 'y');
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return;
        }