java代码实现队列

java代码实现队列

单向队列:

 

import java.util.LinkedList;

//单向队列
public class Queue {

	public Queue() {
	}

	private LinkedList list = new LinkedList();

	public void put(Object v) {
		list.addFirst(v);
	}

	public Object get() {
		return list.removeLast();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}
}

 

 

双向队列:

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

//  双向队列
public class DQueue implements Collection {
	private LinkedList lists;

	public DQueue() {
		lists = new LinkedList();
	}

	public void put_front(Object v) {
		lists.addFirst(v);
	}

	public void put_back(Object v) {
		lists.addLast(v);
	}

	public Object get_front() {
		return lists.removeFirst();
	}

	public Object get_Back() {
		return lists.removeLast();
	}

	public boolean isEmpty() {
		return lists.isEmpty();
	}

	public int size() {
		return lists.size();
	}

	public boolean contains(Object o) {
		return lists.contains(o);
	}

	public Iterator iterator() {
		return lists.iterator();
	}

	public Object[] toArray() {
		return lists.toArray();
	}

	public Object[] toArray(Object a[]) {
		return lists.toArray(a);
	}

	public boolean add(Object o) {
		lists.addLast(o);
		return true;
	}

	public boolean remove(Object o) {
		return lists.remove(o);
	}

	public boolean containsAll(Collection c) {
		return lists.containsAll(c);
	}

	public boolean addAll(Collection c) {
		return lists.addAll(c);
	}

	public boolean removeAll(Collection c) {
		return lists.removeAll(c);
	}

	public boolean retainAll(Collection c) {
		return lists.retainAll(c);
	}

	public void clear() {
		lists.clear();
	}
}

 

 

 

队列是一种常见的数据结构,简单来说,就是先进先出(FIFO)的操作方式。在Java中,可以使用数组或链表来实现队列。下面我们分别介绍这两种方式的实现方法。 1. 使用数组实现队列 首先我们需要定义一个数组来表示队列,然后定义队列的头尾指针front和rear,初始值都为0。当往队列中插入元素时,我们先将元素插入到队列尾部,即arr[rear] = element,然后将rear指针向后移动一个位置,即rear++。当从队列中取出元素时,我们先将队列头部的元素取出,即element = arr[front],然后将front指针向后移动一个位置,即front++。 下面是使用数组实现队列Java代码: ``` public class ArrayQueue { private int capacity; private int[] arr; private int front; private int rear; public ArrayQueue(int capacity) { this.capacity = capacity; this.arr = new int[capacity]; this.front = 0; this.rear = 0; } public void enqueue(int element) { if (rear == capacity) { throw new RuntimeException("Queue is full."); } arr[rear] = element; rear++; } public int dequeue() { if (front == rear) { throw new RuntimeException("Queue is empty."); } int element = arr[front]; front++; return element; } public int size() { return rear - front; } public boolean isEmpty() { return front == rear; } } ``` 2. 使用链表实现队列 使用链表来实现队列的话,则不需要像数组一样限制队列大小,我们只需要定义一个指向队列头部的指针head和指向队列尾部的指针tail,对于每个元素,我们将其插入到tail的后面,并将tail指针向后移动一个位置,当从队列中取出元素时,我们将head指针向后移动一个位置,并返回head指向的元素。 下面是使用链表实现队列Java代码: ``` public class LinkedQueue { private Node head; private Node tail; private int size; private class Node { private int data; private Node next; public Node(int data) { this.data = data; } } public void enqueue(int element) { Node newNode = new Node(element); if (tail == null) { head = tail = newNode; } else { tail.next = newNode; tail = tail.next; } size++; } public int dequeue() { if (head == null) { throw new RuntimeException("Queue is empty."); } int element = head.data; head = head.next; size--; if (head == null) { tail = null; } return element; } public int size() { return size; } public boolean isEmpty() { return head == null; } } ``` 以上两种实现方式各有优缺点,在实际开发中应该根据实际需求选择最适合的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值