C# 显示队列中的项目

C# 显示队列中的项目,c#,C#,我创建了一个简单的Windows窗体应用程序,它允许用户插入名称,并从队列中删除名称 我已经创建了所需的所有方法,但我希望每次单击Add和Remove按钮时都显示队列中的所有项目 当前,我的代码仅在使用.Peek()时显示队列的头 请参阅下图: 队列类别代码 class Queue { private readonly int maxsize = 10; private string[] store; private int head =

我创建了一个简单的Windows窗体应用程序,它允许用户插入
名称
,并从
队列中删除
名称

我已经创建了所需的所有方法,但我希望每次单击
Add
Remove
按钮时都显示队列中的所有项目

当前,我的代码仅在使用
.Peek()
时显示队列的

请参阅下图:

队列类别代码

class Queue
    {
        private readonly int maxsize = 10;
        private string[] store;
        private int head = 0;
        private int tail = 0;
        private int numItems;

        public Queue()
        {
            store = new string[maxsize];
        }

        public Queue(int size)
        {
            maxsize = size;
            store = new string[maxsize];
        }

        public void Enqueue(string value)
        {
            numItems++;
            store[tail] = value;
            if (++tail == maxsize)
            {
                tail = 0;
            }
        }

        public string Dequeue()
        {
            string headItem;
            //numItems--;
            headItem = store[head];
            if (headItem != null)
            {
                store[head] = null;
                numItems--;
                if (++head == maxsize)
                    head = 0;
            }

            return headItem;

        }

        public bool IsEmpty()
        {
            return tail == 0; //returns the boolean result of the comparison between head and 0
        }

        public bool IsFull()
        {
            return tail == maxsize;
        }
        public int CountQueue() //counts the number of items inside the queue
        {
            return tail - head;
        }

        public int Tail //property
        {
            set { this.tail = value; }
            get { return tail; }
        }

        public int Head //property
        {
            set { this.head = value; }
            get { return head; }
        }

        public string Peek()
        {
            return store[head];
        }

    }
public partial class Form1 : Form
    {
        Queue names = new Queue();
        public Form1()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {

            names.Enqueue(NameTextBox.Text);
            CountLabel.Text = "Number of entries: " +  Convert.ToString(names.CountQueue());

            if (names.CountQueue() > 0)
            {
                DisplayLabel.Text = names.Peek();
            }
            else
                DisplayLabel.Text = "No names entered";

        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            names.Dequeue();
            if (names.CountQueue() == 0)
                CountLabel.Text = "Queue is empty";

            else
                CountLabel.Text = "Number of entries: " + Convert.ToString(names.CountQueue());

            if (names.CountQueue() > 0)
            {
                DisplayLabel.Text = names.Peek();
            }
            else
                DisplayLabel.Text = "No names entered";


        }
    }
表格代码

class Queue
    {
        private readonly int maxsize = 10;
        private string[] store;
        private int head = 0;
        private int tail = 0;
        private int numItems;

        public Queue()
        {
            store = new string[maxsize];
        }

        public Queue(int size)
        {
            maxsize = size;
            store = new string[maxsize];
        }

        public void Enqueue(string value)
        {
            numItems++;
            store[tail] = value;
            if (++tail == maxsize)
            {
                tail = 0;
            }
        }

        public string Dequeue()
        {
            string headItem;
            //numItems--;
            headItem = store[head];
            if (headItem != null)
            {
                store[head] = null;
                numItems--;
                if (++head == maxsize)
                    head = 0;
            }

            return headItem;

        }

        public bool IsEmpty()
        {
            return tail == 0; //returns the boolean result of the comparison between head and 0
        }

        public bool IsFull()
        {
            return tail == maxsize;
        }
        public int CountQueue() //counts the number of items inside the queue
        {
            return tail - head;
        }

        public int Tail //property
        {
            set { this.tail = value; }
            get { return tail; }
        }

        public int Head //property
        {
            set { this.head = value; }
            get { return head; }
        }

        public string Peek()
        {
            return store[head];
        }

    }
public partial class Form1 : Form
    {
        Queue names = new Queue();
        public Form1()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {

            names.Enqueue(NameTextBox.Text);
            CountLabel.Text = "Number of entries: " +  Convert.ToString(names.CountQueue());

            if (names.CountQueue() > 0)
            {
                DisplayLabel.Text = names.Peek();
            }
            else
                DisplayLabel.Text = "No names entered";

        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            names.Dequeue();
            if (names.CountQueue() == 0)
                CountLabel.Text = "Queue is empty";

            else
                CountLabel.Text = "Number of entries: " + Convert.ToString(names.CountQueue());

            if (names.CountQueue() > 0)
            {
                DisplayLabel.Text = names.Peek();
            }
            else
                DisplayLabel.Text = "No names entered";


        }
    }

为清楚起见,您是否询问如何显示完整队列? 如果是这样,最好使用列表框而不是标签来显示名称,每次添加名称时,您只需说

lstNames.Items.Add(names.Peek());
lstNames.Items.RemoveAt(0);
添加名称后,lstNames将成为Listbox实例。然后,当您决定从队列中删除一个项目时,可以说

lstNames.Items.Add(names.Peek());
lstNames.Items.RemoveAt(0);
希望这有帮助,如果我误解了这个问题,很抱歉

编辑:不确定你是否编辑了你的问题,或者我只是误读了,但我很清楚你现在在问什么


Edit2:对不起,我指的是lstNames.Items.Add(NameTextBox.Text),而不是names.peek()

为了清楚起见,您是否在询问如何显示完整的队列? 如果是这样,最好使用列表框而不是标签来显示名称,每次添加名称时,您只需说

lstNames.Items.Add(names.Peek());
lstNames.Items.RemoveAt(0);
添加名称后,lstNames将成为Listbox实例。然后,当您决定从队列中删除一个项目时,可以说

lstNames.Items.Add(names.Peek());
lstNames.Items.RemoveAt(0);
希望这有帮助,如果我误解了这个问题,很抱歉

编辑:不确定你是否编辑了你的问题,或者我只是误读了,但我很清楚你现在在问什么


Edit2:对不起,我指的是lstNames.Items.Add(NameTextBox.Text),而不是names.peek()

是的,我正在尝试显示完整的队列。我正在尝试实现您的代码:
lstName.add(names.Peek())
.add()
似乎不起作用,它不会显示在IntelliSense上。我尝试使用您的答案,但在测试时,每次输入不同的名称时,它都会显示输入的第一个名称。例如:名字:约翰,第二个名字:迈克,第三个名字:大卫。但是名单上有3份约翰纳的照片,我搞错了!在Add()调用中不使用names.Peek(),只需输入名称即可。在清除文本框之前,可以使用lstNames.Items.Add(NameTextBox.Text)。是的,我正在尝试显示完整的队列。我正在尝试实现您的代码:
lstName.add(names.Peek())
.add()
似乎不起作用,它不会显示在IntelliSense上。我尝试使用您的答案,但在测试时,每次输入不同的名称时,它都会显示输入的第一个名称。例如:名字:约翰,第二个名字:迈克,第三个名字:大卫。但是名单上有3份约翰纳的照片,我搞错了!在Add()调用中不使用names.Peek(),只需输入名称即可。在清除文本框之前,可以使用lstNames.Items.Add(NameTextBox.Text)。您不使用
System.Collections.Generic.Queue
类的原因是什么?它实现了
IEnumerable
,这使得显示项目变得非常简单。您可以添加一个
公共字符串[]GetAllItems
方法,该方法返回
存储的副本
…您不使用
系统.Collections.Generic.Queue
类的任何原因?它实现了
IEnumerable
,这使得显示项目变得非常容易。您只需添加一个
public string[]GetAllItems
方法,该方法返回
store
的副本。。。