Hi, I have a home assignment in C#, I need to make a simple one-way linked list and I'm having trouble. I can make all kinds of linked lists in C++ with no problem at all but I can't do it in C# and that's because I can't access memory directly so no pointers for me.

Now this is where the fun begins. Where the hell do I start? I don't want to use templates, I need to make it by myself. I just don't know what should I do to replace * and -> in C#?

This is what I've got so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        //CLASS NODE
        public class Node
        {
            int userinput;                                    
            Node p_next;                           

            void Add();
            void Print();
        }

        //CLASS LIST
        public class List
        {
            Node p_start;                            

            void PrintAll();
            void AddToStart();
            void AddToEnd();
        }

        //MAIN
        public static void Main(string[] args)
        {
            List MyList = new List();
            MyList.p_start = null; <- !?!?
           
        }
    }
}

So how do I go about creating a new List in Main() and creating new pointers?

Dani AI

Generated

A few focused points that address the two problems seen in this thread: implementing a simple linked list in C# (no raw pointers needed) and why the WinForms Print button might not be invoking your handler.

C# classes are reference types, so a Node that holds a reference to the next Node replaces C-style pointers. Keep Node fields accessible (public or via properties) only if you need external access; prefer properties for encapsulation. Avoid naming your class List because that collides with System.Collections.Generic.List<T>—use LinkedList, MyList or similar. For language details, see the C# classes overview: Classes and structs (C#).

If a button click does not run the expected method, the most common cause is the event not being wired to the correct handler (or wired to the wrong one). Use the form designer Properties -> Events (lightning bolt) to check which method is assigned, or inspect FormName.Designer.cs for the this.button3.Click += ... line. It is also possible both buttons point to the same handler by mistake. See the WinForms Click event docs: Control.Click Event.

Do not return from inside a traversal loop (that will give you only the first element). Either build and return a single string of all values, or make the method void and have it append directly to the textbox. Example ToString-style traversal (adjust names to your classes):

public string ToDelimitedString(string sep = ", ")
{
    var sb = new System.Text.StringBuilder();
    var node = head; // your start node
    while (node != null)
    {
        if (sb.Length > 0) sb.Append(sep);
        sb.Append(node.Value);
        node = node.Next;
    }
    return sb.ToString();
}

Practical troubleshooting: put a breakpoint or a simple Debug.WriteLine/MessageBox.Show at the start of the button handler to confirm it fires. If sees Add working but Print jumps back to Add, inspect the event wiring in Designer and confirm PrintList is public and returns the expected format. Thanks to for nudging the linked-list concept — in C# the reference model handles the pointer role.

Recommended Answers

All 3 Replies

C# allows pointers as you are used to in C++.
No need for it:

might help

Thanks, I somehow managed to work it out but I have another problem.

I'm making a GUI linked list in C# and I'm having trouble returning a value.

On one side I have:

private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = Convert.ToString(MyList.PrintList());
        }

And on the other I have:

public int PrintList()
        {
           p_temp = p_start;                             
            while (p_temp != null)                        
            {
                int i = p_temp.element;
                p_temp = p_temp.p_next;          
                return i;
            }
            return 0;
        }

Now these two functions are in two separate files and when I click the button nothing happens. Now I don't know what I'm doing wrong.

I add to the list like this:

private void button1_Click(object sender, EventArgs e)
        {
        MyList.AddToStart(Convert.ToInt32(textBox1.Text));
        }

and this:

public void AddToStart(int element)
        {
            Node bla = new Node();                  
            bla.element = element;                             
            bla.p_next = p_start;                       
            p_start = bla;                                    
        }

Now the code works when compiling a console program but it doesn't work with GUI, where I have to return values between two c# files.

OK I started debugging and looks like the button3_click() even doesn't run and I don't know why.

Adding to the list works:

private void button1_Click(object sender, EventArgs e)
{
          MyList.AddToStart(Convert.ToInt32(textBox1.Text));
}

But when I click the Print button:

private void button3_Click(object sender, EventArgs e)
{
            textBox2.Text = Convert.ToString(MyList.PrintList());
}

It goes back to Add button and does that all over again and skips this one BUT if I copy this line to the button1_click and have both events in the same button event, it works. Porke?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.