Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace World2
- {
- class Queue<T>
- {
- private const int CAPACITY = 5;
- private T[] entities;
- private int counter;
- public T this[int i]
- {
- get
- {
- // This indexer is very simple, and just returns or sets
- // the corresponding element from the internal array.
- return entities[i];
- }
- set
- {
- entities[i] = value;
- }
- }
- public Queue()
- : this(CAPACITY)
- { }
- public Queue(int capacity)
- {
- entities = new T[capacity];
- }
- public void Enqueue(T entity)
- {
- if (counter >= entities.Length) {
- throw new InvalidOperationException("The queue is full");
- }
- entities[counter] = entity;
- counter++;
- }
- public T Dequeue()
- {
- if (counter == 0) {
- throw new InvalidOperationException("The queue is empty");
- }
- T temp = entities[0];
- for (int i = 0; i < entities.Length - 1; i++)
- {
- entities[i] = entities[i + 1];
- }
- entities[counter - 1] = default(T);
- counter--;
- return temp;
- }
- public int GetNumberOfEntities()
- {
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment