首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

交流-分享:用 C# 实现的一个队列源代码解决办法

2012-02-01 
交流-分享:用 C# 实现的一个队列源代码usingSystemusingSystem.Collections.GenericusingSystem.Textna

交流-分享:用 C# 实现的一个队列源代码
using   System;
using   System.Collections.Generic;
using   System.Text;

namespace   Link
{
        //结构:
        public   struct   structData
        {
                public   string   sStr;
                public   int   iInt;
        }

        //实现链表:
        public   class   myLink
        {
                //Begin:内部类:定义链表:
                public   class   Link
                {
                        private   structData   sd;
                        private   Link   next;

                        public   Link()
                        {
                        }

                        public   Link(structData   sd0)
                        {
                                next   =   null;
                                sd   =   sd0;
                        }

                        public   structData   Sd
                        {
                                get
                                {
                                        return   sd;
                                }
                                set
                                {
                                        sd   =   value;
                                }
                        }

                        public   Link   Next
                        {


                                get
                                {
                                        return   next;
                                }
                                set
                                {
                                        next   =   value;
                                }
                        }
                }
                //End:内部类:定义链表

                private   Link   link;

                public   myLink()
                {
                        link   =   null;
                }

                public   Link   LINK  
                {
                        get   {   return   link;   }
                        set   {   link   =   value;   }
                }

                //添加一个结点:
                public   void   Add(structData   sd)  
                {
                        Link   l   =   new   Link();
                        l.Sd   =   sd;
                        l.Next   =   link;     //这2句是链表的核心代码
                        link   =   l;
                }

                //输出一个结点(先进先出):
                public   structData   Peek()
                {
                        Link   Currlink   =   link;
                        while   (Currlink.Next   !=   null)
                        {


                                Currlink   =   Currlink.Next;
                        }
                        structData   sd   =   Currlink.Sd;
                        return   sd;
                }

                //删除首结点:
                public   void   DeleteFirst()
                {
                        Link   Prevlink   =   link;
                        Link   Currlink   =   link;
                        Currlink   =   Prevlink.Next;
                        while   (Currlink.Next   !=   null)
                        {
                                Prevlink   =   Currlink;
                                Currlink   =   Prevlink.Next;
                        }
                        Prevlink.Next   =   null;
                }

                //迭代器:
                public   System.Collections.IEnumerator   GetEnumerator()
                {
                        Link   lk   =   link;
                        while   (lk   !=   null)
                        {
                                yield   return   lk;
                                lk   =   lk.Next;
                        }
                }
        }

        class   Program
        {
                static   void   Main(string[]   args)
                {
                        myLink   lnk   =   new   myLink();
                        structData   sd1;
                        sd1.sStr   =   "s1 ";


                        sd1.iInt   =   1;
                        lnk.Add(sd1);

                        structData   sd2;
                        sd2.sStr   =   "s2 ";
                        sd2.iInt   =   2;
                        lnk.Add(sd2);

                        structData   sd3;
                        sd3.sStr   =   "s3 ";
                        sd3.iInt   =   3;
                        lnk.Add(sd3);

                        structData   sd4;
                        sd4.sStr   =   "s4 ";
                        sd4.iInt   =   4;
                        lnk.Add(sd4);

                        structData   sd5;
                        sd5.sStr   =   "s5 ";
                        sd5.iInt   =   5;
                        lnk.Add(sd5);

                        Console.WriteLine( "所有数据: ");
                        foreach   (myLink.Link   lk   in   lnk)
                        {
                                Console.Write(lk.Sd.sStr   +   ",   ");
                                Console.WriteLine(lk.Sd.iInt.ToString());
                        }

                        Console.WriteLine( "显示队列首数据: ");
                        structData   sd01   =   lnk.Peek();
                        Console.WriteLine(sd01.sStr);
                        Console.WriteLine(sd01.iInt.ToString());

                        Console.WriteLine( "删除队列首数据后,显示队列新的首数据: ");
                        lnk.DeleteFirst();


                        structData   sd02   =   lnk.Peek();
                        Console.Write(sd02.sStr   +   ",   ");
                        Console.WriteLine(sd02.iInt.ToString());

                        Console.WriteLine( "再次删除队列首数据后,显示队列新的首数据: ");
                        lnk.DeleteFirst();
                        structData   sd03   =   lnk.Peek();
                        Console.Write(sd03.sStr   +   ",   ");
                        Console.WriteLine(sd03.iInt.ToString());
                        Console.WriteLine( "这时队列的所有数据: ");
                        foreach   (myLink.Link   lk   in   lnk)
                        {
                                Console.Write(lk.Sd.sStr   +   ",   ");
                                Console.WriteLine(lk.Sd.iInt.ToString());
                        }

                        Console.ReadLine();
                }
        }
}
电脑学习网     http://www.why100000.com    
张庆   2007.5.29

[解决办法]
沙发
[解决办法]
ding
[解决办法]
again ding
[解决办法]
队列,一般用Queue来实现比较符合要去.
[解决办法]
练习不错。

热点排行