This article illustrates the method of removing and adding elements in C # collection over time. Share to everyone for your reference, specific as follows:
Most of the time, there is no need to add or delete elements to the element, but sometimes it is necessary, for example, if the elements in the collection are all people in society, then the element is deleted when someone dies, and some people are born with an increase in the collection element. In this case, the traversal cannot be done the way it was, and the collection in C # does not support the Foreach loop for this type of traversal that has an addition or deletion action.
There are three ways to solve this problem.
The first method: Use C # 's linkedlist<> double linked list. I originally imagined that the elements that needed to be deleted were removed directly from the original list, those newly added elements, first loaded into a temporary list, and so on, and then add to the temporary linked list of the first node to the end of the list, so the complexity of the algorithm is also low, but, unexpectedly, C # double linked list , you cannot add a node belonging to another linked list to this list, and its next property is read-only. Helpless, only one side of the cycle, the end of the original linked table to add nodes, so you need to mark the end of the loop position, that is, the original unchanged linked list of the end of the cycle, rather than in the modified chain of the tail end of the list. This requires that a reference to the trailing node be obtained before the loop begins. The program is as follows (the list has 0-29 of the integer value node, traversal encountered 3 integer times, on the end of the list to add a 0-valued node, encountered 2 integer times to delete the node)
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Namespace collection time Delete or add element {public partial class Form1:form {public Form1 () {InitializeComponent ();
Private linkedlist<int> list = new linkedlist<int> ();
Initialize, add 0-29 integers into the list private void Button1_Click (object sender, EventArgs e) {for (int i = 0; i < i++)
{This.list.AddLast (i); }//Traverse the list, do the following://encounter can be divisible by 3, just add a 0 element after the list, encounter can be divisible by 2 to delete the element private void button2_click (object sender, Eventa RGS e) {linkedlistnode<int> Nodenow = this.list.first;//list first element linkedlistnode<int> NodeLast
= this.list.last;//The last element of the original list, the end of the loop tag linkedlistnode<int> nodetmp;//temporary node//loop End condition is that the current node is the last node of the original list while (Nodenow!= nodelast) {//If divisible by 3, add a 0 if to the list (nodEnow.value% 3 = 0) {this.list.AddLast (0);
//If divisible by 2, delete the element if (nodenow.value% 2 = 0) {//If Nodenow is deleted, then you must not use next to get the next element to be judged Because it has automatically moved down one, it is necessary to delete the Nodenow before,//get its next, assign to Nodetmp, after Nodenow deleted, and then assign nodetmp memory to Nodenow No
Detmp = Nodenow.next;
This.list.Remove (Nodenow);
Nodenow = nodetmp;
else {//If not divisible by 2, leave the element in the list and get the next and Judge Nodenow = Nodenow.next;
}//finally do not forget to Nodelast (the last element of the original list) itself, the while loop above does not include this nodelast if (nodenow.value% 3 = 0) {
This.list.AddLast (0);
} if (nodenow.value% 2 = 0) {this.list.Remove (Nodenow);
}//test result private void Button3_Click (object sender, EventArgs e) {foreach (int i in this.list)
{Console.WriteLine (i);
}
}
}
}
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Namespace collection time Delete or add element {public partial class Form1:form {public Form1 () {InitializeComponent ();
Private linkedlist<int> list = new linkedlist<int> ();
Initialize, add 0-29 integers into the list private void Button1_Click (object sender, EventArgs e) {for (int i = 0; i < i++)
{This.list.AddLast (i); }//Traverse the list, do the following://encounter can be divisible by 3, just add a 0 element after the list, encounter can be divisible by 2 to delete the element private void button2_click (object sender, Eventa RGS e) {linkedlistnode<int> Nodenow = this.list.first;//list first element linkedlistnode<int> NodeLast
= this.list.last;//The last element of the original list, the end of the loop tag linkedlistnode<int> nodetmp;//temporary node//loop End condition is that the current node is the last node of the original list while (Nodenow!= nodelast) {//If divisible by 3, add a 0 if to the list (nodEnow.value% 3 = 0) {this.list.AddLast (0);
//If divisible by 2, delete the element if (nodenow.value% 2 = 0) {//If Nodenow is deleted, then you must not use next to get the next element to be judged Because it has automatically moved down one, it is necessary to delete the Nodenow before,//get its next, assign to Nodetmp, after Nodenow deleted, and then assign nodetmp memory to Nodenow No
Detmp = Nodenow.next;
This.list.Remove (Nodenow);
Nodenow = nodetmp;
else {//If not divisible by 2, leave the element in the list and get the next and Judge Nodenow = Nodenow.next;
}//finally do not forget to Nodelast (the last element of the original list) itself, the while loop above does not include this nodelast if (nodenow.value% 3 = 0) {
This.list.AddLast (0);
} if (nodenow.value% 2 = 0) {this.list.Remove (Nodenow);
}//test result private void Button3_Click (object sender, EventArgs e) {foreach (int i in this.list)
{Console.WriteLine (i);
}
}
}
}
The second method: using C # list<>,list<> is based on the array of sequential table, increase, delete action time complexity is higher, than the efficiency of the linked list. It is basically the same as the first method, but also need to use an int-type variable to mark the end of the original order table, when you delete an element, this variable needs to be reduced. Code slightly.
The third method, custom single linked list generic class (linked list class see http://www.jb51.net/article/87610.htm). The advantage of the first method is that it is possible to combine the two linked lists flexibly by simply setting the first node of the second linked list to the next node (or directly add) of the first linked list. In fact, for C # Double linked list, I am not very clear, why AddLast () method, can not add a list of elements to another list, and can only add a node does not belong to any linked list (some people say that the first method, in fact, you can use node clone, But this is only to increase the algorithm's space and time complexity, contrary to the use of the list of the original intention. The reason why C # does not support this practice may be that MS is concerned that the node you are joining is on a circular list, which can cause the last attribute of the original list, the Count property, and so on to be counted (to form a dead loop). The test code is as follows:
Read more about C # Interested readers can view the site topics: "C # traversal algorithm and Skills summary", "C # Design Thread usage Tips", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", " WinForm Control Usage Summary, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming
I hope this article will help you with C # programming.