当前位置:编程学习 > C#/ASP.NET >>

c#链表问题

大家能帮忙看看我这程序的问题吗?编译能通过,但就是输出的时候有问题
using System;
using System.Collections.Generic;
using System.Text;
//程序主要功能是用随机数生成两个链表,并输出
namespace test2
{
    class count
    {
        public int number;
        public count next;
    }
    class Program
    {
        static void Main(string[] args)
        {
            czMath cz = new czMath();
            count head1 = new count();
            count head2 = new count();
            cz.add(ref head1);
            count current1 = head1;
            while (current1 != null)
            {
                Console.Write("\0\0{0}", current1.number);
                current1 = current1.next;
            }
            Console.WriteLine();
            cz.add(ref head2);
            count current2 = head2;
            while (current2 != null)
            {
                Console.Write("\0\0{0}", current2.number);
                current2 = current2.next;
            }
            Console.WriteLine();
        }
    }
    class czMath
    {
        public void add(ref count head)
        {
            count current;
            count temp;
            Random ran = new Random();
            current = head;
            current.number = ran.Next(100);
            current.next = null;
            for (int i = 0; i <= 5; i++)
            {
                temp = new count();
                temp.number = ran.Next(100);
                temp.next = null;
                current.next = temp;
                current = current.next;
            }
        }
    }
}

程序输出的时候两个链表中的数据是一样的,但我按F11逐语句调试时,在局部变量窗口看到两个链表中的数据是不一样,但输出的时候就一样了,很郁闷,不知道是怎么回事,大家能帮我看看吗? --------------------编程问答-------------------- 链表不是这么写的。。。

http://www.shrinkrays.net/code-snippets/csharp/linked-list-and-double-linked-lists-in-csharp.aspx --------------------编程问答-------------------- caozhy你的网址很有用!多谢,多谢! --------------------编程问答-------------------- 没指针,感觉在c#用线性链表 有点无法实现 --------------------编程问答-------------------- 这样就不会重复了,在你的基础上稍做了一些修改。

using System;
using System.Collections.Generic;
using System.Text;
//程序主要功能是用随机数生成两个链表,并输出 
namespace test2
{
class count
{
public int number;
public count next;
}
class Program
{
static void Main(string[] args)
{
czMath cz = new czMath();
count head1 = new count();
count head2 = new count();
cz.add(ref head1);
count current1 = head1;
while (current1 != null)
{
Console.Write("\0\0{0}", current1.number);
current1 = current1.next;
}
Console.WriteLine();
cz.add(ref head2);
count current2 = head2;
while (current2 != null)
{
Console.Write("\0\0{0}", current2.number);
current2 = current2.next;
}
Console.WriteLine();
Console.ReadLine();
}
}

class czMath
{
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}

public void add(ref count head)
{
count current;
count temp;
Random ran = new Random(GetRandomSeed());
current = head;
current.number = ran.Next(100);
current.next = null;
for (int i = 0; i <= 5; i++)
{
temp = new count();
temp.number = ran.Next(100);
temp.next = null;
current.next = temp;
current = current.next;
}
}
}
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,