Sunday, 23 November 2014

Lab 5 cont class Queue New

class Data {
  String name;
  int priority;
  Data(String name, int priority) {
    this.name = name;
    this.priority = priority;
  }
  String toString() {
    String print = "Name#"+name+" Priority#"+priority;
    return print;
  }
}
class Queue {
  Data[] wait;
  int limit;
  Queue(int limit) {
    this.limit = limit;
    this.wait = new Data[limit];
  }
  void show_queue() {
    for (int i = 0; i < this.wait.length; i++) {
      if (this.wait[i] != null) {
        println(this.wait[i]);
      }
    }
  }
  void add(Data in) {
    for (int i = 0; i < this.wait.length; i++) {
      if ( this.wait[i] == null ) {
        this.wait[i] = in;
        i = this.wait.length;
      }
    }
  }
  void remove() {
    int high = 0;
    for (int i = 0; i < this.wait.length; i++) {
      if (this.wait[i] != null) {
        if (this.wait[high].priority < this.wait[i].priority) {
          high = i;
        }
      }
    }
    this.wait[high] = null;
    Data[] copy = new Data[this.limit];
    for (int i = 0; i < copy.length; i++) {
      copy[i] = this.wait[i];
      this.wait[i] = null;
    }
    int count = 0;
    for (int i = 0; i < copy.length; i++) {
    if (copy[i] != null) {
        this.wait[count] = copy[i];
        count++;
      }
    }
  }
}
Data[] human = new Data[322];
Queue hospital = new Queue(200);//max --> 2147483647
void setup() {
  human[0] = new Data("Somsak", 30);
  human[1] = new Data("Prasert", 30);
  hospital.add(human[0]);
  hospital.add(human[1]);
  //hospital.remove();
  //hospital.remove();
  hospital.show_queue();
}

No comments:

Post a Comment