zoukankan      html  css  js  c++  java
  • 使用带有头尾节点的单链表实现队列

    package db;
    
    public class Queue<E> {
    	/**
    	 * 内部类--单链表
    	 * @author fightzhao
    	 *
    	 * @param <E>
    	 */
    	private static class Node<E> {
    		public Node<E> next;
    		public E data;
    
    		public Node(E data, Node<E> next) {
    			this.data = data;
    			this.next = next;
    		}
    	}
    
    	private Node<E> front;
    	private Node<E> rear;
    	private int theSize;
    
    	/**
    	 * 入队
    	 * @param data
    	 */
    	public void enqueue(E data) {
    		Node<E> pNode = new Node<E>(data, null);
    		if (rear != null) {
    			rear.next = pNode;
    			rear = pNode;
    		} else {
    			front = rear = pNode;
    		}
    		theSize++;
    	}
    
    	public int size() {
    		return theSize;
    	}
    
    	/**
    	 * 出队
    	 * @return
    	 */
    	public E dequeue() {
    		E data = front.data;
    		if (front.next != null) {
    			front = front.next;
    		} else {
    			front = rear = null;
    		}
    		theSize--;
    		return data;
    	}
    	
    	public Queue(){
    		front = null;
    		rear = null;
    	}
    	public void printAll(){
    		Node<E> pNode= front;
    		while(pNode!=null){
    			System.out.println(pNode.data);
    			pNode=pNode.next;
    		}
    	}
    }
    

      

  • 相关阅读:
    SVG:中国地图
    网页编程工具:EditPlus
    js插件
    html: 仿制soundmanager2右上角面板
    代码:页面布局(含图片列表布局)
    写着玩: 图片 圆盘
    表格
    按钮
    插件:左侧下拉菜单
    颜色
  • 原文地址:https://www.cnblogs.com/fightzhao/p/5243247.html
Copyright © 2011-2022 走看看