zoukankan      html  css  js  c++  java
  • java代码实现顺序队列

    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
    

      

  • 相关阅读:
    取石子(二)巴仕博弈+尼姆博弈
    hdu2430Beans(单调队列)
    LCD: 2D-3D匹配算法
    如何选择视觉CV光源颜色
    gpgpu-sim卡分配程序设计实例分析
    PointRCNN: 点云的3D目标生成与检测
    3D点云重建原理及Pytorch实现
    GPU加速计算
    红外传感器技术
    Linux架构思维导图
  • 原文地址:https://www.cnblogs.com/zhongyimeng/p/9946615.html
Copyright © 2011-2022 走看看