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?

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.