How to replace an element in LinkedList?

How to replace an item in a linked list with C #? Although in java you can either use the set or add method to replace a specific item in the list ... I cannot find how to replace it with C #.

+4
source share
3 answers

Replace the value 2 with 3:

LinkedList<int> ll; ll.Find(2).Value = 3; 
+10
source

There is no Replace method for a linked node list. To replace node, you must:

  • Find the node you want to replace.
  • Insert a new node before this node ( AddBefore )
  • Delete the source node.

You can also use AddAfter . The result will be the same.

+7
source

With LinkedList<T> you work with LinkedListNode<T> elements. LinkedListNode<T> has a Value property that you can use to get or set a specific item.

Unlike C #, Java has no concept of properties integrated into the language. In Java, you create properties by adding the set_Property and get_Property methods manually. C # properties can be accessed as fields.

Java:

 obj.set_MyProperty(123); x = obj.get_MyProperty(); 

WITH#:

 obj.MyProperty = 123; x = obj.MyProperty; 

Inside, however, C # calls the getter and setter methods. You would declare a property like this in C #:

 private int _myProperty; public int MyProperty { get { return _myProperty; } set { _myProperty = value; } } 

In this special case, when no other logic is involved, you can use the automatically implemented properties:

 public int MyProperty { get; set; } 

In your linked list, you change the frst element from the list as follows:

 myLinkedList.Fist.Value = your new value; 
+2
source

Source: https://habr.com/ru/post/1385977/


All Articles