Guest User

Untitled

a guest
Jan 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4.  
  5. public class Ochered implements Iterable<Student> {
  6.        
  7.         private Student[] q;
  8.         private int head;
  9.         private static Ochered o;
  10.        
  11.         private Ochered(int n) {
  12.             q = new Student[n+1];
  13.             head = -1;
  14.         }
  15.        
  16.         public static Ochered getInstance(int n) {
  17.            
  18.             if (o == null) {
  19.                 return o = new Ochered(n);
  20.             }
  21.             else return o;
  22.         }
  23.        
  24.         public boolean isEmpty(){
  25.             if (head == -1)
  26.                 return true;
  27.             else return false;
  28.         }
  29.        
  30.         public void push(Student stud) throws Exception{
  31.             if (head == q.length-1) throw new Exception("Overflow");
  32.             head++;
  33.             q[head] = stud;
  34.         }
  35.        
  36.         public Student peek() throws Exception{
  37.             if (isEmpty()) throw new Exception("Underflow");
  38.            
  39.             return q[0];
  40.         }
  41.        
  42.         public Student pop() throws Exception{
  43.              if (head == -1) throw new Exception("Underflow");
  44.              
  45.                 Student tmp = q[0];
  46.                
  47.                 for (int i = 0; i < head; i ++) {
  48.                  q[i] = q[i+1];
  49.                 }
  50.                
  51.                 q[head] = null;
  52.                 head--;
  53.                
  54.                
  55.              return tmp;   
  56.        
  57.         }
  58.        
  59.         public Iterator<Student> iterator() {
  60.               return new Iterator<Student>() {
  61.                 int i = 0;
  62.                 Student next = q[0];
  63.                 public boolean hasNext() {
  64.                     return next != null;
  65.                 }
  66.                 public Student next() {
  67.                     if (i > head) throw new NoSuchElementException();
  68.                     Student current = next;
  69.                     i++;
  70.                     next = q[i];
  71.                     return current;
  72.                 }
  73.                 public void remove() {
  74.                     throw new UnsupportedOperationException();
  75.                 }
  76.               };
  77.             }
  78.        
  79.  
  80.    
  81. }
Add Comment
Please, Sign In to add comment