• 可自动增长队列长度的泛型循环队列


    最近不忙,随便写写!

    队列代码:

     /// <summary>
        
    /// 一个可以自动增长长度的队列,并且可以重复循环队列
        
    /// </summary>
        
    /// <typeparam name="T">自定义的泛型</typeparam>
        class Queue  <T>
        {   
    //队列出队index     
            int front;
            
    //队列入队Index
            int back;
            
    //队列中元素数量
            int size;
            
    //队列的初始长度设置为3
            T[] data=new T [3] ;
            
    //获取队列中的元素数量
            public int QueueSize
            {
                
    get { return size; }
            }
            
    /// <summary>
            
    /// 元素出队
            
    /// </summary>
            
    /// <returns></returns>
            public T DeQueue()
            {
                T t 
    =default(T );
                
    if (size != 0)
                {
                    SetFrontToZero();               
                    t 
    = data[front];
                    data[front] 
    = default(T);
                    front
    ++;
                    size
    --;
                 

                }
                
    return t;
            }
            
    /// <summary>
            
    /// 元素入队
            
    /// </summary>
            
    /// <param name="t"></param>
            public void EnQueue(T t)
            {

                AddQueueSize();
                SetBackToZero();
               
                data[back] 
    = t;
                back
    ++;      
                size
    ++;

            }
            
    //队列自增长
            private  void AddQueueSize()
            {
                
    if (size >= data.Length)
                {
                    back 
    = size;
                    T[] newData 
    = new T[data.Length * 2];

                    
    for (int i = 0; i < data.Length; i++)
                    {
                        newData[i] 
    = DeQueue();

                    }
                    data 
    = newData;

                    size 
    = back;
                    front 
    = 0;
                }
            }
             
    /// <summary>
             
    /// 设置出队index
             
    /// </summary>
            private void SetBackToZero()
            {
                
    if (back == data.Length&&size <data .Length )
                {
                    back 
    = 0;
                }
            }
            
    /// <summary>
            
    /// 设这出队Index
            
    /// </summary>
            private void SetFrontToZero()
            {
                
    if (front  == data.Length && size !=0)
                {
                    front 
    = 0;
                }
            }

        }
     
    测试用到的泛型数据:
    View Code
    /// <summary>
        
    /// 个人成绩结构体
        
    /// </summary>
        class SocreMode : IComparable,ICloneable 
        {
            
    public string mFileName = "";
            
    public string mMail = "";
            
    public string mName = "";
            
    public int mSocre;
            
    public string FileName
            {
                
    get
                {
                    
    return mFileName;
                }
                
    set
                {
                    mFileName 
    = value;
                }
            }
            
    public string Mail
            {
                
    get
                {
                    
    return mMail;
                }
                
    set { mMail = value; }

            }
            
    public string Name
            {
                
    get { return mName; }
                
    set { mName = value; }

            }
            
    public int  Socre
            {
                
    get { return mSocre; }
                
    set { mSocre = value; }
            }
            
    /// <summary>
            
    /// 实现IComparable接口
            
    /// </summary>
            
    /// <param name="obj">SocreMode对象</param>
            
    /// <returns></returns>
            int IComparable.CompareTo(object obj)
            {
                SocreMode temp 
    = (SocreMode)obj;
                
    if (temp.mName == mName)
                {
                    
    return this.mMail.CompareTo(temp.mMail);

                }
                
    else
                {
                    
    return this.mName.CompareTo(temp.Name);
                }
            }
            
    /// <summary>
            
    ///  重载ToString 方法
            
    /// </summary>
            
    /// <returns></returns>
            public override string ToString()
            {
                
    return string.Format(mMail.PadRight(30+ "\t" + mName.PadRight(15 - (System.Text.Encoding.Default.GetByteCount(mName) - mName.Length)) + "\t" + mSocre.ToString());
            }
            
    public object Clone()
            {
                SocreMode temp 
    = new SocreMode();
                temp.mSocre 
    = this.mSocre;
                
    return temp;

            }
        }

    测试代码:

      class Program
        {
            
    static void Main(string[] args)
            {
                
    int len;
                
    int lens = 0;
                    
    int lCount=30;
                    Queue
    <SocreMode> lQueue = new Queue<SocreMode>();
                    SocreMode lSocreMode 
    = new SocreMode();
                    Console.WriteLine(
    " 一次入队3个元素,出队三个元素  ");
                    
    for (int i = 0; i < lCount; i++)
                    {
                        len 
    = 3;
                        SocreMode tmpSocreMode 
    =(SocreMode) lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        
    int one = lQueue.DeQueue().Socre;
                        
    int two = lQueue.DeQueue().Socre;
                        
    int three = lQueue.DeQueue().Socre;
                        Console.Write( one.ToString () 
    +two.ToString () +three .ToString ());
                       
                    }
                    Console.WriteLine(
    "\n////////////////////////////////////");
                    Console.WriteLine(
    " 一次入队2个元素,出队1个元素  ");
                    len 
    = 0;
                    lens 
    = 2;
                    
    for (int i = 0; i < lCount; i++)
                    {
                        len
    ++;
                        SocreMode tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        
    int one = lQueue.DeQueue().Socre;
                      
                        Console.Write(one.ToString() );
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }

                    }
                    
    while (lQueue.QueueSize != 0)
                    {
                        len
    ++;
                        
    //lQueue.EnQueue(tmpSocreMode);
                        int one = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }
                    }
                    Console.WriteLine(
    "\n////////////////////////////////////");
                    Console.WriteLine(
    " 一次入队4个元素,出队2个元素  ");
                    len 
    = 0;
                    lens 
    = 4;
                    
    for (int i = 0; i < lCount; i++)
                    {
                        len
    ++;
                        SocreMode tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        
    int one = lQueue.DeQueue().Socre;
                        
    int two = lQueue.DeQueue().Socre;
                        
    //int three = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString() + two.ToString() );
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }

                    }
                    
    while (lQueue.QueueSize != 0)
                    {
                        len
    ++;
                        
    //lQueue.EnQueue(tmpSocreMode);
                        int one = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }
                    }
                    Console.WriteLine(
    "\n////////////////////////////////////");
                    Console.WriteLine(
    " 一次入队4个元素,出队2个元素  ");
                    len 
    = 0;
                    lens 
    = 5;
                    
    for (int i = 0; i < lCount; i++)
                    {
                        len
    ++;
                        SocreMode tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        
    int one = lQueue.DeQueue().Socre;
                        
    int two = lQueue.DeQueue().Socre;
                        
    //int three = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString() + two.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }

                    }
                    
    while (lQueue.QueueSize != 0)
                    {
                        len
    ++;
                        
    //lQueue.EnQueue(tmpSocreMode);
                        int one = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }
                    }
                    Console.WriteLine(
    "\n////////////////////////////////////");
                    Console.WriteLine(
    " 一次入队7个元素,出队2个元素  ");
                    len 
    = 0;
                    lens 
    = 7;
                    
    for (int i = 0; i < lCount; i++)
                    {
                        len
    ++;
                        SocreMode tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);
                        tmpSocreMode 
    = (SocreMode)lSocreMode.Clone();
                        tmpSocreMode.Socre 
    = tmpSocreMode.Socre + i;
                        lQueue.EnQueue(tmpSocreMode);                    
                        
    int one = lQueue.DeQueue().Socre;
                        
    int two = lQueue.DeQueue().Socre;
                        
    //int three = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString() + two.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }

                    }
                    
    while (lQueue.QueueSize != 0)
                    {
                        len
    ++;
                        
    //lQueue.EnQueue(tmpSocreMode);
                        int one = lQueue.DeQueue().Socre;
                        Console.Write(one.ToString());
                        
    if (len == lens)
                        {
                            Console.Write(
    "\n");
                            len 
    = 0;
                        }
                    }
                    Console.WriteLine(
    "\n////////////////////////////////////");
                    Console.ReadKey();
            }
        }
  • 相关阅读:
    损失函数及其梯度
    激活函数及其梯度
    梯度下降简介
    误差计算
    输出方式
    全连接层
    测试(张量)- 实战
    数据加载
    高阶操作
    java网络基础知识的简述
  • 原文地址:https://www.cnblogs.com/hda37210/p/1967899.html
Copyright © 2020-2023  润新知