Exemplo n.º 1
0
        /// <summary>
        ///     Insertion after a node.
        /// </summary>
        /// <param name="searchNodeValue">Node containing value.</param>
        /// <param name="newNodeValue">New node value.</param>
        public void InsertionAfterANode(T searchNodeValue, T newNodeValue)
        {
            //start node check
            if (StartNode == null)
            {
                return;
            }

            //Get the reference of the node contain value.
            var referenceNode = GetNode(searchNodeValue);

            //if the node with data found .
            if (referenceNode == null)
            {
                return;
            }

            //create the node .
            //Connect new node with the next node of the reference node.
            var newNode = new SingleLinkListNode <T>(newNodeValue)
            {
                Link = referenceNode.Link
            };

            //reset the link between reference node and new node .
            referenceNode.Link = newNode;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Delete the last node .
        /// </summary>
        public void DeleteLastNode()
        {
            if (StartNode == null)
            {
                return;
            }

            //Go to the one node before the last node .
            var predecessorNodeOfLastNode = GetReferenceToThePredecessorOfLastNode();

            //Only one node exist .
            if (StartNode.Link == null)
            {
                //Remove that node .
                StartNode = null;
                return;
            }

            //Find predecessor of last node.
            if (predecessorNodeOfLastNode == null)
            {
                return;
            }
            //Remove last node .
            predecessorNodeOfLastNode.Link = null;
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Delete node with value .
        /// </summary>
        /// <param name="searchNodeValue"></param>
        public void DeleteNodeWithValue(T searchNodeValue)
        {
            //Check node .
            var desiredNode = GetNode(searchNodeValue);

            //null check.
            if (desiredNode == null)
            {
                return;
            }
            //check start node.
            if (desiredNode == StartNode)
            {
                StartNode = StartNode.Link;
            }

            //Get predecessor of last node .
            desiredNode = GetPredecessorNode(searchNodeValue);
            //null check.
            if (desiredNode == null)
            {
                return;
            }
            //Remove that node .
            desiredNode.Link = desiredNode.Link.Link;
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Insert a new node at position .
        /// </summary>
        /// <param name="position">Position in the list.</param>
        /// <param name="withInfo">Info part of the node.</param>
        public void InsertANewNodeAtPosition(int position, T withInfo)
        {
            //Get the reference of the predecessor node of the position .
            var referenceNode = GetPredecessorNodeAtPosition(position);

            //if the node with data found .
            if (referenceNode == null)
            {
                return;
            }
            //When reference node is not the start node .
            if (referenceNode != StartNode)
            {
                //create the node .
                //Connect new node with the next node of the reference node.
                var newNode = new SingleLinkListNode <T>(withInfo)
                {
                    Link = referenceNode.Link
                };
                //reset the link between reference node and new node .
                referenceNode.Link = newNode;
            }
            else
            {
                //Insert node at the beginning of the node .
                InsertionAtTheBeginning(withInfo);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Insert a node in an empty list .
        /// </summary>
        /// <param name="data"></param>
        public void InsertionInAnEmptyList(T data)
        {
            //create a new node .
            var node = new SingleLinkListNode <T>(data)
            {
                Link = null
            };

            //it should be pointed by start node .
            StartNode = node;
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Insertion before a node.
        /// </summary>
        /// <param name="searchNodeValue">Node containing value.</param>
        /// <param name="newNodeValue">New node value.</param>
        public void InsertionBeforeANode(T searchNodeValue, T newNodeValue)
        {
            //start node check
            if (StartNode == null)
            {
                return;
            }

            //Get the reference of the predecessor node that contain value.
            var referenceNode = GetPredecessorNode(searchNodeValue);

            //if the node with data found .
            if (referenceNode == null)
            {
                return;
            }

            //Reference node is not a start node .
            if (referenceNode != StartNode)
            {
                //create the node .
                //Connect new node with the next node of the reference node.
                var newNode = new SingleLinkListNode <T>(newNodeValue)
                {
                    Link = referenceNode.Link
                };
                //reset the link between reference node and new node .
                referenceNode.Link = newNode;
            }
            //A reference node can be a start node in both condition .
            //Either searched value present in start node or.
            //second node contain the searched value .
            else
            {
                {
                    // Reference node is a start node nad it contains the searched value.
                    if (referenceNode.Info.Equals(searchNodeValue))
                    {
                        InsertionAtTheBeginning(newNodeValue);
                    }
                    else
                    {
                        //create the node .
                        //Connect new node with the next node of the reference node.
                        var newNode = new SingleLinkListNode <T>(newNodeValue)
                        {
                            Link = referenceNode.Link
                        };
                        //reset the link between reference node and new node .
                        referenceNode.Link = newNode;
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Delete first node .
        /// </summary>
        public void DeleteFirstNode()
        {
            //Check start node.
            if (StartNode == null)
            {
                return;
            }
            //2nd node .
            var secondNode = StartNode.Link;

            //let start node point it .
            StartNode = secondNode;
        }
Exemplo n.º 8
0
 /// <summary>
 ///     Insert a node at the beginning .
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="data"></param>
 public void InsertionAtTheBeginning(T data)
 {
     //Check start.
     if (StartNode == null)
     {
         InsertionInAnEmptyList(data);
     }
     else
     {
         //create node .
         //link new node with the first node of the list .
         //first node is pointed by the start .
         var node = new SingleLinkListNode <T>(data)
         {
             Link = StartNode
         };
         //Now start node should point to the new node .
         StartNode = node;
     }
 }
Exemplo n.º 9
0
        private void singlelinklist_Click(object sender, EventArgs e)
        {
            SingleLinkList list = new SingleLinkList();

            list.InsertNodeAtBegining(1);
            list.InsertNodeAfter(3, 1);
            list.InsertNodeBefore(2, 3);
            list.InsertNodeAtEnd(4);
            list.InsertNodeAtEnd(5);
            list.InsertNodeAfter(6, 5);
            list.DeleteNode(3);

            list.Traverse();
            list.TraverseBackward();

            //Make list circular
            SingleLinkListNode second = list.Find(2);
            SingleLinkListNode node   = list.Find(6);

            node.link = second;

            list.IsListCircular();
        }
Exemplo n.º 10
0
 /// <summary>
 ///     Single linked list Constructor .
 /// </summary>
 public SingleLinkedList()
 {
     //Set up start node to null .
     StartNode = null;
 }