Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class Ochered implements Iterable<Student> {
- private Student[] q;
- private int head;
- private static Ochered o;
- private Ochered(int n) {
- q = new Student[n+1];
- head = -1;
- }
- public static Ochered getInstance(int n) {
- if (o == null) {
- return o = new Ochered(n);
- }
- else return o;
- }
- public boolean isEmpty(){
- if (head == -1)
- return true;
- else return false;
- }
- public void push(Student stud) throws Exception{
- if (head == q.length-1) throw new Exception("Overflow");
- head++;
- q[head] = stud;
- }
- public Student peek() throws Exception{
- if (isEmpty()) throw new Exception("Underflow");
- return q[0];
- }
- public Student pop() throws Exception{
- if (head == -1) throw new Exception("Underflow");
- Student tmp = q[0];
- for (int i = 0; i < head; i ++) {
- q[i] = q[i+1];
- }
- q[head] = null;
- head--;
- return tmp;
- }
- public Iterator<Student> iterator() {
- return new Iterator<Student>() {
- int i = 0;
- Student next = q[0];
- public boolean hasNext() {
- return next != null;
- }
- public Student next() {
- if (i > head) throw new NoSuchElementException();
- Student current = next;
- i++;
- next = q[i];
- return current;
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- }
Add Comment
Please, Sign In to add comment