Delete and add elements in the set time duration

Source: Internet
Author: User

Most of the time, when traversing a set element, you do not need to add or delete the element, but sometimes you need it. For example, if the element in the set is all people in the society, if a person dies, the element is deleted. If a person is born, the element is added to the set. In this case, traversal cannot be performed in the original way, And the set in C # does not support the foreach loop for this type of traversal with addition and deletion actions.

There are three ways to solve this problem.

Method 1: Use the double-stranded list of c. I originally imagined that the elements to be deleted from the original linked list should be removed directly. The newly added elements should be first loaded into a temporary linked list and the loop ends, add the head node of the temporary linked list to the end of the original linked list.AlgorithmThe complexity is low, but, unexpectedly, the double-chain table of C # cannot add nodes belonging to another linked list to this linked list, and its next attribute is also read-only. Instead, you can only add nodes to the end of the original linked list while repeating them. In this way, you need to mark the end position of the loop, that is, you need to end the loop at the end of the original unaltered linked list, instead of ending at the tail node of the modified linked list. In this way, the reference of the tail node is obtained before the loop starts.ProgramAs shown below (the linked list has an integer node between 0 and 29. When the time interval reaches an integer multiple of 3, a zero-value node is added to the end of the linked list and the node is deleted when an integer multiple of 2 is reached)

View plaincopy to clipboardprint?
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;
Delete or add elements to the namespace set
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private partition list <int> List = new partition list <int> ();
// Initialize and add an integer ranging from 0 to 29 to the linked list
Private void button#click (Object sender, eventargs E)
{
For (INT I = 0; I <30; I ++)
{
This. List. addlast (I );
}
}
// Perform the following operations on a recurring table:
// If three elements can be divisible, add a zero element to the linked list. If two elements can be divisible, delete the element.
Private void button2_click (Object sender, eventargs E)
{
Optional listnode <int> nodenow = This. List. First; // the first element of the linked list
Optional listnode <int> nodelast = This. List. Last; // the last element of the original linked list, marking the end of the loop
Inclulistnode <int> nodetmp; // temporary Node
// The condition for loop termination is that the current node is the last node of the original linked list.
While (nodenow! = Nodelast)
{
// If it can be divisible by 3, add 0 after the linked list
If (nodenow. Value % 3 = 0)
{
This. List. addlast (0 );
}
// Delete this element if it can be divisible by 2
If (nodenow. Value % 2 = 0)
{
// If nodenow is deleted, you cannot use next to obtain the next element to be judged.
// Because it has been automatically moved to the next place, this is before deleting nodenow,
// Obtain its next and assign it to nodetmp. After nodenow is deleted, the nodetmp memory is assigned to nodenow.
Nodetmp = nodenow. Next;
This. List. Remove (nodenow );
Nodenow = nodetmp;
}
Else
{
// If the element cannot be divisible by 2, the element is retained in the linked list, and the next one is obtained for judgment.
Nodenow = nodenow. Next;
}

}
// Do not forget to process nodelast (the last element of the original linked list) itself. The above while loop 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;
Delete or add elements to the namespace set
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private partition list <int> List = new partition list <int> ();
// Initialize and add an integer ranging from 0 to 29 to the linked list
Private void button#click (Object sender, eventargs E)
{
For (INT I = 0; I <30; I ++)
{
This. List. addlast (I );
}
}
// Perform the following operations on a recurring table:
// If three elements can be divisible, add a zero element to the linked list. If two elements can be divisible, delete the element.
Private void button2_click (Object sender, eventargs E)
{
Optional listnode <int> nodenow = This. List. First; // the first element of the linked list
Optional listnode <int> nodelast = This. List. Last; // the last element of the original linked list, marking the end of the loop
Inclulistnode <int> nodetmp; // temporary Node
// The condition for loop termination is that the current node is the last node of the original linked list.
While (nodenow! = Nodelast)
{
// If it can be divisible by 3, add 0 after the linked list
If (nodenow. Value % 3 = 0)
{
This. List. addlast (0 );
}
// Delete this element if it can be divisible by 2
If (nodenow. Value % 2 = 0)
{
// If nodenow is deleted, you cannot use next to obtain the next element to be judged.
// Because it has been automatically moved to the next place, this is before deleting nodenow,
// Obtain its next and assign it to nodetmp. After nodenow is deleted, the nodetmp memory is assigned to nodenow.
Nodetmp = nodenow. Next;
This. List. Remove (nodenow );
Nodenow = nodetmp;
}
Else
{
// If the element cannot be divisible by 2, the element is retained in the linked list, and the next one is obtained for judgment.
Nodenow = nodenow. Next;
}

}
// Do not forget to process nodelast (the last element of the original linked list) itself. The above while loop 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 );
}
}
}
}

 

Method 2: Use List <> and list <> of C # As an array-based ordered table. The time for adding or deleting an action is more complex than that for a linked list. It is basically the same as the first method. You also need to use an int type variable to mark the tail element of the original sequence table. When deleting an element, this variable needs to be automatically subtracted.Code.

 

The third method, custom single-chain table generic class (Linked List class see http://blog.csdn.net/suinon/archive/2010/03/02/5340376.aspx ). Compared with the first method, it is able to flexibly merge two linked lists, you only need to set the header node of the second linked list to the next node (or directly add) of the End Node of the first linked list. In fact, I don't know why the addlast () method cannot add the elements of a linked list to another linked list, instead, you can only add a node that does not belong to any linked list (some people say that the first method can actually use node clone, but it does not increase the space and time complexity of the algorithm, it violates the intention of using the linked list ). C # The reason why this approach is not supported may be that Ms is worried that the node you join is located on a ring linked list, in this way, the last attribute and count attribute of the original linked list cannot be calculated (an endless loop is formed ). The test code is as follows:

View plaincopy to clipboardprint?
// Merge two linked lists
Vertex list <int> List = new vertex list <int> ();
For (INT I = 0; I <10; I ++)
{
List. Add (I );
}
Listlist <int> list2 = new listlist <int> ();
For (INT I = 10; I <20; I ++)
{
List2.add (I );
}
List. Add (list2.head );

Node <int> N = List. head;
While (n! = NULL)
{
Console. writeline (N. data );
N = n. Next;
}
Console. Readline ();
Console. writeline (list. getlength ());
Console. Readline ();
// Merge two linked lists
Vertex list <int> List = new vertex list <int> ();
For (INT I = 0; I <10; I ++)
{
List. Add (I );
}
Listlist <int> list2 = new listlist <int> ();
For (INT I = 10; I <20; I ++)
{
List2.add (I );
}
List. Add (list2.head );

Node <int> N = List. head;
While (n! = NULL)
{
Console. writeline (N. data );
N = n. Next;
}
Console. Readline ();
Console. writeline (list. getlength ());
Console. Readline ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.