Home > Backend Development > C#.Net Tutorial > How to implement traversal of a one-way linked list using C#?

How to implement traversal of a one-way linked list using C#?

PHPz
Release: 2023-08-31 09:41:09
forward
993 people have browsed it

How to implement traversal of a one-way linked list using C#?

Set up the LinkedList collection -

var list = new LinkedList<string>();
Copy after login

Now, add elements-

list.AddLast("One");
list.AddLast("Two");
list.AddLast("Four");
Copy after login

Now, let us add new elements to the already created LinkedList-

LinkedListNode<String> node = list.Find("Four");
list.AddBefore(node, "Three");
list.AddAfter(node, "Five");
Copy after login

Now let us see how to traverse the nodes in a singly linked list -

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      var list = new LinkedList < string > ();
      list.AddLast("One");
      list.AddLast("Two");
      list.AddLast("Four");

      Console.WriteLine("Travering...");
      foreach(var res in list) {
         Console.WriteLine(res);
      }

      LinkedListNode < String > node = list.Find("Four");
      list.AddBefore(node, "Three");
      list.AddAfter(node, "Five");

      Console.WriteLine("Travering after adding new elements...");

      foreach(var res in list) {
         Console.WriteLine(res);
      }
   }
}
Copy after login

The above is the detailed content of How to implement traversal of a one-way linked list using C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template