List Queue

//Author:Shawn
//Email:stepfencurryxiao@gmail.com

const int MAX_SIZE = 10;

class ListQueue<T> {
  int count = 0;
  List<T> queue = new List<T>(MAX_SIZE);

  //Checks if the queue has elements (not empty)
  bool hasElements() {
    if (queue.length == 0) {
      return false;
    } else {
      return true;
    }
  }

  //Add an element to the queue
  void enque(T element) {
    if (count == MAX_SIZE) {
      print("The queue is full!!!");
    } else {
      queue[count] = element;
      count++;
    }
  }

  //Takes the next element from the queue
  T deque() {
    T result = null;
    if (count == 0) {
      print("The queue is empty!!!");
    } else {
      result = queue[0];
      for (int i = 0; i < queue.length - 1; i++) {
        queue[i] = queue[i + 1];
      }
    }
    return result;
  }
}

void main() {
  ListQueue<int> Queue = new ListQueue<int>();
  Queue.enque(12);
  Queue.enque(2);
  Queue.enque(7);
  print(Queue.queue);
  print("Enqueue:");
  var returnData = Queue.deque();
  print("$returnData\n");
  print("Enqueue:");
  returnData = Queue.deque();
  print("$returnData\n");
  print("Enqueue:");
  returnData = Queue.deque();
  print("$returnData\n");
  print("Now the queue is: " + (Queue.queue).toString());
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.