C# OOP: doubly linked list

Delete item of doubly linked list

Step 4: Delete an element of the doubly linked list

To delete an element of the doubly linked list, you need to consider the followings:

1. If the element to be deleted is the first element of the list and the list contains only one element, you only need to assign null to the pfirst and plast. If the element to be deleted is the first element of the list and the list contain more than one element, you need a temporary link to point to the pfirst then move the pfirst to point to its next element and set the temporary pointer and the previous link of the pfirst to null.

C#(Csharp) Delete the first item of the double linkedlist  

2. If the element to be deleted is in the middle of the list, you need a traversing link(temp) to point to the element to be deleted. Then exchange the links as you see in the picture below. To handle situation where the element to be deleted is the last element of the list, you need to test whether the target item is equal to the plast. If it is really equal, you need to update the plast to point to the item staying before the target item to be deleted. Finally set the temporary pointer to null.

C#(Csharp) Delete middle item of the double linkedlist  

//C#(Csharp) code to  delete an item of the doubly linked list

public void delete(int pos)

            {

                int deleted = 1;

                int i;

                if (pfirst != null)

                { //make sure the list is not empty.

                    ListNode<T> tr, temp;

                    if (pos == 1)

                    {//delete the first item

                        if (countitem() == 1)

                        { //The list contains only one item

                            pfirst = null;

                            plast = null;

 

                        }

                        else

                        { //The list contains more than one item

                            tr = pfirst;

                            pfirst = pfirst.next;

                            pfirst.prev = null;

                            tr = null;

                        }

                      

                    }

 

                    else if (pos > 1 && pos <= countitem())

                    {//delete middle item

                        temp = pfirst;

                        for (i = 1; i < pos; i = i + 1) { temp = temp.next; }

                        if (temp.next == null) { plast = temp.prev; plast.next = null; }

                        else

                        {

                            temp.prev.next = temp.next;

                            temp.next.prev = temp.prev;

 

                        }

                        temp = null;                       

                    }

                    else { deleted = 0; Console.WriteLine("Invalid position!"); }

                    if (deleted != 0) Console.WriteLine("Deleted");

                }

                else Console.WriteLine("No item found");

            }




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.