Doubly-linked list/Definition: Difference between revisions

→‎{{header|C sharp|C#}}: Added usage notes about framework class; adjusted style
(→‎{{header|C sharp|C#}}: Added usage notes about framework class; adjusted style)
Line 356:
 
=={{header|C sharp}}==
 
<lang C sharp>
The .NET framework provides the <code>LinkedListNode<T></code> class, which represents an individual node of a linked list, and the <code>LinkedList<T></code> class, which provides abstractions to read and modify a list as if it were a single object. The <code>LinkedListNode<T>.Next</code> and <code>LinkedListNode<T>.Previous</code> properties is read-only, ensuring that all lists must be created using <code>LinkedList<T></code> and that each list can only be mutated using the methods of its <code>LinkedList<T></code> instance (the appropriate .NET accessibility modifiers are used to hide these implementation details).
using System.Collections.Generic;
 
namespace Doubly_Linked_List
One node instance is forbidden to be in multiple lists; this is enforced using the <code>LinkedListNode<T>.List</code> property, which is set accordingly when a node is added to a <code>LinkedList<T></code> and set to <code>null</code> when it is removed. This also has the effect of preventing cycles.
 
Though mutating the ''structure'' of a list can only be done through the <code>LinkedList<T></code> class, mutating the values contained by the nodes of a list is done through its individual <code>LinkedListNode<T></code> instances, as the <code>LinkedListNode<T>.Next</code>.Value property is settable.
 
<lang C sharp>using System.Collections.Generic;
 
class Program
{
static void Main(string[] args)
class Program
{
LinkedList<string> list = new LinkedList<string>();
static void Main(string[] args)
list.AddFirst(".AddFirst() adds at the head.");
{
LinkedListNode<string> tail = list.FindAddLast(".AddLast() adds at the tail.");
LinkedList<string> list = new LinkedList<string>();
LinkedListNode<string> head = list.AddFirstFind(".AddFirst() adds at the head.");
list.AddLastAddAfter(head, ".AddLastAddAfter() adds atafter a thespecified tailnode.");
LinkedListNode<string> headtail = list.Find(".AddFirstAddLast() adds at the headtail.");
list.AddAfterAddBefore(headtail, ".AddAfter()Betcha addscan't afterguess awhat specified.AddBefore() nodedoes.");
LinkedListNode<string> tail = list.Find(".AddLast() adds at the tail.");
list.AddBefore(tail, "Betcha can't guess what .AddBefore() does.");
 
System.Console.WriteLine("Forward:");
foreach (string nodeValue in list) { System.Console.WriteLine(nodeValue); }
 
System.Console.WriteLine("\nBackward:");
LinkedListNode<string> current = tail;
while (current != null)
{
System.Console.WriteLine(current.Value);
current = current.Previous;
}
}
}
}</lang>
}
 
{{out}}
/* Output:
<pre>Forward:
.AddFirst() adds at the head.
.AddAfter() adds after a specified node.
Betcha can't guess what .AddBefore() does.
.AddLast() adds at the tail.
 
Backward:
.AddLast() adds at the tail.
Betcha can't guess what .AddBefore() does.
.AddAfter() adds after a specified node.
.AddFirst() adds at the head.</pre>
*/
</lang>
 
=={{header|Clojure}}==
Anonymous user