java实现顺序队列
package xianxinTable; import java.util.ArrayList; import java.util.Iterator; import com.sun.java_cup.internal.internal_error; public class queue_self<E> implements Iterator<E> { // 定义一个简单的迭代器 public iterator itertor(){ return new iterator(); } class iterator implements Iterator<E> { // 要迭代的长度 private int size; // 当前迭代到达位置 private int nowsize; iterator() { size = queue_self.this.length; nowsize = 1; } public boolean hasNext() { if (nowsize != size+1) { // TODO Auto-generated method stub return true; } else { return false; } } public E next() { // TODO Auto-generated method stub int page=nowsize; nowsize++; return queue_self.this.datalist.get(page-1); } public void remove() { // TODO Auto-generated method stub } } // 定义数组长度 private int length; // 存放数组内容 private ArrayList<E> datalist; // 队列头 private int head; // 队列尾 private int rear; // 队列初始化 public queue_self() { datalist = new ArrayList<E>(); this.length = 0; head = 0; rear = 0; } // 入队 public boolean offer(E data) { if (true) { this.datalist.add(data); length++; return true; } else { return false; } } // 出队 public E poll() { E e = null; if (length == 0) { return e; } else { e = this.datalist.get(head); this.datalist.remove(head); length--; return e; } } // 查看队首元素 public E peek() { E e = null; if (length == 0) { return e; } else { e = this.datalist.get(head); return e; } } // 返回队列长度 public int length() { return this.length; } public static void main(String[] args) { queue_self<Integer> q = new queue_self<Integer>(); q.offer(2); q.offer(44); q.offer(66); q.poll(); Iterator<Integer> it=q.itertor(); while(it.hasNext()){ System.out.println(it.next()); } } public boolean hasNext() { // TODO Auto-generated method stub return false; } public E next() { // TODO Auto-generated method stub return null; } public void remove() { // TODO Auto-generated method stub } }
输出:
44 66