堆叠和排队 [英] stack and queue

查看:45
本文介绍了堆叠和排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我的全部编码.我不认为长度对我来说很重要,因为仅使用计数即可.我唯一的问题是入队.它希望使用class,delegate ...,但我尝试这样做.即时通讯的想法,希望我可以解决它,但一个崭新的想法会很好.欢呼雀跃

ok this is all my coding. i dont think length would be all that important to me ill just use count. the only problem im having is with enqueue. it expects class,delegate... to be used but ive tried to do that. im out of ideas hopefully i can solve it but a fresh mind would be good. cheers all

   public class patient
        {
            private string name;
            private string NHSN;
            private string urgency;
            private patient next()
            {}
            private patient next(string name, string NHSN, string urgency)
            {
                name = name;
                NHSN = NHSNnumber;
                urgency = urgency;
        }
        public string name
        {
            get { return name; }
            set { name value; }
        }
        public string NHSNnumber
        {
            get {return NHSN;}
            set { NHSN value; }
        }
            public string urgency
            { 
                get {return urgency;}
                set {urgency value;}
            }
      }
        public class Queue
        {
            private patient first;
            private patient last;
            public Queue patientqueue = new Queue();
            public Stack patientstack = new Stack();
                }
             public void enqueue(patient);////////////////heres enqueue
                 
        //static void Main(string[] args)
        {
            if(TextBox3.Text == "U")
            {
            patient U = new patient(textBox1.Text + textBox2.Text);
            }
           if(TextBox3.Text == "N")
            {
            patient N = new patient(textBox1.Text + textBox2.Text);
            }
            Queue queue = new Queue();
            Queue.patientqueue.enqueue(U);
            Queue.patientqueue.enqueue(N);
            Queue.enqueue(Queue.patientqueue);
             }
            public patient dequeue();
              {
            patient newpatient = new patient();
            patient patientnext = new patient(); //next in line examine 
            
            while (patientp.Count != 0)
            {
    if (patientstack.Count != 0)
    {
      if (patientp.Count > 0) 
        {
          patientnext = (patient)patientp.Peek(); 
        }
        if (patientnext.urgency == "U")
        { 
         newpatient = (patient)patientp.Dequeue();
         listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name);
        
        if (patientnext.urgency == "N")
        { 
            newpatient = (patient)patientnext.Pop();
           listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name);
        }
      }
    if (patientnext.Count == 0)
      
          {
            newpatient = (patient)patientp.Dequeue();
       
            if (patientp.Count > 0)
            { 
                patientnext = (patient)patientp.Peek();
            }
            if (newpatient.urgency == "U")
            {
                listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name);
            }
            if ((newpatient.urgency == "N") && (patientnext.urgency == "N"))
            {
                listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name);
            }
            if ((newpatient.urgency == "N") && 
               (patientnext.urgency == "U"))
            {
                patientnext.Push(newpatient);
                listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), patientnext.Count.ToString());
                listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name + NHSN);
            
                // get next
              newpatient = (patient)newpatient.Dequeue();
              listBox1.ToString((textbox1.Text + textBox2.Text + textBox3.Text), newpatient.name);                
            }
               }
              public int length();
              {
              new length = new Count;  
               }
               public bool isEmpty();
               if (enqueue.Count < 0)
                {
                  isEmpty == true;
                }    
             }
       public void button1_Click(object sender,EventArgs e)
        {
        if(textBox1.Text.Trim() != patientp.isEmpty)
        {
            patientp.Enqueue(textBox1.Text.Trim());
            listBox1.Items.Insert(listBox1.Items.length,
            "Added patient: " + textBox1.Text.Trim() + " At " + DateTime.Now.ToString());
        }
        else
        {
            listBox1.show("Empty Value Cannot be Added","QueueDemo");
        }
    }
    public void button2_Click(object sender, EventArgs e)
    {
        try
        {
           listBox1.show("Removed patient "  + 
            patientp.Dequeue().ToString());
            listBox1.Items.RemoveAt(0);
        }
        catch(Exception ex)
        {
            listBox1.show("Error: " + ex.Message); 
        }
public void button3_Click(object sender, EventArgs e)
{
    patient newpatient = new patient();
    Queue copyofpatientp = new Queue();
    copyofpaitenp = (Queue)patientp.Clone();
    listBox1.WriteLine(" ");
    listBox1.Show("View the queue using a copy");
    do
    {
        tempCustomer = (BankCustomer)copyoflocalQueue.Dequeue();
        listbox1.Items.add("Name: " + newpatient.name + ",number: " + newpatient.NHSN + 
                          ",  urgency: " + 
                          newpatient.urgency.ToString() + 
    } while (copyofpatientp.Count != 0);
}
      static void main()
    {
        Application.Run(new patientp());
    } 

;


发生的情况是用户输入名称和NHSN,然后如果输入U或N,则对它们进行堆栈排序.

;


What happens is the user puts in name and NHSN and then if U or N is put in, stack sorts them.

推荐答案

好,答案似乎很简单.不要使用类StackQueue;它们对于您的应用程序几乎是无用的,或者至少是不安全的.而是使用通用类 Stack<>Queue<>.在尖括号中,将所需的实际编译时类型放在Queue或Stack的元素中.

如果我了解您的代码,您将使用:

Ok, the answer seems simple. Do not use classes Stack or Queue; they are almost useless or at least unsafe for your application. Instead, use generic classes Stack<> and Queue<>. In angular brackets, put actual compile-time type you want to have for element of your Queue or Stack.

If I understand your code, you will use:

Queue<patient> Queue = new Queue<patient>();
Stack<patient> Stack = new Stack<patient>();



另外,请帮自己一个忙,请使用标准命名约定:Patient,而不是patient,因为这是类型名称.



Also, do yourself a favor, use standard naming conventions: Patient, not patient, because this is a type name.


这篇关于堆叠和排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆