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;
    		}
    	}
    }
    

      

  • 相关阅读:
    树的重心备忘
    Hdu 2196
    HDU 1520
    TOJ1068 商务旅行
    携程HDU第一场1001
    USACO 4.3.2 The Primes
    Html常用标签的应用
    Html
    开班心得
    for循环练习及字符串处理
  • 原文地址:https://www.cnblogs.com/fightzhao/p/5243247.html
Copyright © 2011-2022 走看看