zoukankan      html  css  js  c++  java
  • 链队

    public class LinkedQueue {
    
    	QueueHead qh = new QueueHead();
    	
    	boolean empty() {
    		return qh.front == null;        //or qh.rear == null
    	}
    	
    	void enQueue(int e) {
    		QNode q = new QNode(e);
    		if(empty()) {
    			qh.front = q;
    			qh.rear = q;
    		} else {
    			qh.rear.next = q;
    			qh.rear = q;
    		}
    	}
    	
    	int[] deQueue() {
    		int[] arr = new int[2];
    		if(qh.rear == null) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = qh.front.data;
    		if(qh.front.next == null) {
    			qh.front = qh.rear = null;
    		} else {
    			qh.front = qh.front.next;
    		}
    		return arr;
    	}
    	
    	void display() {
    		QNode p = qh.front;
    		while(p != null) {
    			if(p.next == null) {
    				System.out.println(p.data);
    			} else {
    				System.out.print(p.data + "->");
    			}
    			p = p.next;
    		}
    	}
    	
    	public static void main(String[] args) {
    		LinkedQueue sq = new LinkedQueue();
    		System.out.println(sq.empty());
    		sq.enQueue(4);
    		sq.enQueue(5);
    		sq.display();
    		sq.deQueue();
    		sq.deQueue();
    		System.out.println(sq.empty());
    		sq.display();
    		sq.enQueue(1);
    		sq.enQueue(3);
    		sq.display();
    		sq.deQueue();
    		sq.display();
    		sq.enQueue(2);
    		sq.deQueue();
    		sq.display();
    		System.out.println(sq.empty());
    
    	}
    
    }
    
    class QNode {
    	int data;
    	QNode next;
    	QNode(){}
    	QNode(int e) {
    		data = e;
    	}
    }
    
    class QueueHead {
    	QNode front;
    	QNode rear;
    	public QueueHead() {}
    }
    


    结果:

    true
    4->5
    true
    1->3
    3
    2
    false


  • 相关阅读:
    连通域标记
    qt&gdal
    gdal vs2013编译
    java配置
    windows下面安装Python和pip
    mfc operator new”: 没有重载函数接受 3 个参数
    std::min&std::max与mfc冲突问题
    qt中vtk易出现错误
    cmake构建qt工程
    Webstorm补丁
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5116509.html
Copyright © 2011-2022 走看看