[数据结构]队列的实现,单链表

I'm Shendi

下面是队列的代码,使用的单链表,先进先出

/**
 * 队列,单链表.
 * @author Shendi <a href='tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=1711680493'>QQ</a>
 * @version 1.0
 */
public class 队列<E> {
	
	private static class Node<E> {
		E data;
		Node<E> next;
		public Node (E data, Node<E> next) {
			this.data = data;
			this.next = next;
		}
	}
	
	private Node<E> front;
	private Node<E> rear;
	
	public void push(E e) {
		if (front == null) {
			front = new Node<E>(e, null);
			rear = front;
		} else {
			rear.next = new Node<E>(e, null);
			rear = rear.next;
		}
	}
	
	public E pop() {
		if (front == null) return null;
		E data = front.data;
		front = front.next;
		return data;
	}
	
	public static void main(String[] args) {
		队列<Integer> queue = new 队列<>();
		queue.push(1);
		queue.push(2);
		queue.push(3);
		for (int i = 0;i < 3;i++) {
			System.out.println(queue.pop());
		}
		for (int i = 0;i < 3;i++) {
			System.out.println(queue.pop());
		}
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_41806966/article/details/107692682