Shane Shane - 1 year ago 75
C# Question

Creating a very simple linked list

I am trying to create a linked list just to see if I can, and I am having trouble getting my head around it. Does anyone have an example of a very simple implementation of Linked list using C#? All the examples I have found so far are quite overdone.

Answer Source

A Linked List, at its core is a bunch of Nodes linked together.

So, you need to start with a simple Node class:

public class Node {
    public Node next;
    public Object data;

Then your linked list will have as a member one node representing the head (start) of the list:

public class LinkedList {
    private Node head;

Then you need to add functionality to the list by adding methods. They usually involve some sort of traversal along all of the nodes.

public void printAllNodes() {
    Node cur = head;
    while ( != null) 
        cur =;

Also, inserting new data is another common operation:

public void Add(Object data) {
    Node toAdd = new Node(); = data;
    Node current = head;
    // traverse all nodes (see the print all nodes method for an example)
    current.Next = toAdd;

This should provide a good starting point.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download