queue的数组实现(简单)
在队列中,插入和删除发生在相反的两端,因此实现并不像堆栈那么简单。
要使用数组实现队列,请创建一个大小为n的数组arr并采用两个变量front和back ,这两个变量都将被初始化为0 ,这意味着队列当前为空。元素后部是元素存储在数组中的索引,前部是数组第一个元素的索引。现在,队列操作的一些实现如下:
- 入队:向队列中添加元素。在检查队列是否已满后,将执行添加元素。如果rear < n表示数组未满,则将元素存储在arr[rear]并将rear增加1 ,但如果rear == n则称为溢出条件,因为数组已满。
- 出队:从队列中移除一个元素。只有当至少有一个元素要删除时才能删除元素,即后 > 0 。现在,可以删除arr[front]处的元素,但所有剩余元素必须向左移动一个位置,以便出队操作在另一个出队操作中删除左侧的第二个元素。
- Front:如果队列不为空,则从队列中获取最前面的元素,即arr[front] 。
- 显示:打印队列的所有元素。如果队列非空,则遍历并打印从索引front到back的所有元素。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
下面是使用数组实现队列:
C++
// C++ program to implement a queue using an array
#include
using namespace std;
struct Queue {
int front, rear, capacity;
int* queue;
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int;
}
~Queue() { delete[] queue; }
// function to insert an element
// at the rear of the queue
void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear) {
printf("\nQueue is full\n");
return;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
void queueDequeue()
{
// if queue is empty
if (front == rear) {
printf("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the left by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// decrement rear
rear--;
}
return;
}
// print queue elements
void queueDisplay()
{
int i;
if (front == rear) {
printf("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
printf(" %d <-- ", queue[i]);
}
return;
}
// print front of queue
void queueFront()
{
if (front == rear) {
printf("\nQueue is Empty\n");
return;
}
printf("\nFront Element is: %d", queue[front]);
return;
}
};
// Driver code
int main(void)
{
// Create a queue of capacity 4
Queue q(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
printf("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
return 0;
}
Java
// Java program to implement a queue using an array
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
// function to insert an element
// at the rear of the queue
static void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
return;
}
// print front of queue
static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
System.out.printf("\nFront Element is: %d", queue[front]);
return;
}
}
public class StaticQueueinjava {
// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
}
Python3
# Python3 program to implement
# a queue using an array
class Queue:
# To initialize the object.
def __init__(self, c):
self.queue = []
self.front = self.rear = 0
self.capacity = c
# Function to insert an element
# at the rear of the queue
def queueEnqueue(self, data):
# Check queue is full or not
if(self.capacity == self.rear):
print("\nQueue is full")
# Insert element at the rear
else:
self.queue.append(data)
self.rear += 1
# Function to delete an element
# from the front of the queue
def queueDequeue(self):
# If queue is empty
if(self.front == self.rear):
print("Queue is empty")
# Pop the front element from list
else:
x = self.queue.pop(0)
self.rear -= 1
# Function to print queue elements
def queueDisplay(self):
if(self.front == self.rear):
print("\nQueue is Empty")
# Traverse front to rear to
# print elements
for i in self.queue:
print(i, "<--", end = '')
# Print front of queue
def queueFront(self):
if(self.front == self.rear):
print("\nQueue is Empty")
print("\nFront Element is:",
self.queue[self.front])
# Driver code
if __name__=='__main__':
# Create a new queue of
# capacity 4
q = Queue(4)
# Print queue elements
q.queueDisplay()
# Inserting elements in the queue
q.queueEnqueue(20)
q.queueEnqueue(30)
q.queueEnqueue(40)
q.queueEnqueue(50)
# Print queue elements
q.queueDisplay()
# Insert element in queue
q.queueEnqueue(60)
# Print queue elements
q.queueDisplay()
q.queueDequeue()
q.queueDequeue()
print("\n\nafter two node deletion\n")
# Print queue elements
q.queueDisplay()
# Print front of queue
q.queueFront()
# This code is contributed by Amit Mangal
C#
// C# program to implement a queue using an array
using System;
public class Queue
{
private static int front, rear, capacity;
private static int []queue;
public Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
// function to insert an element
// at the rear of the queue
public void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear)
{
Console.Write("\nQueue is full\n");
return;
}
// insert element at the rear
else
{
queue[rear] = data;
rear++;
}
return;
}
// function to delete an element
// from the front of the queue
public void queueDequeue()
{
// if queue is empty
if (front == rear)
{
Console.Write("\nQueue is empty\n");
return;
}
// shift all the elements from index 2 till rear
// to the right by one
else
{
for (int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
public void queueDisplay()
{
int i;
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++)
{
Console.Write(" {0} <-- ", queue[i]);
}
return;
}
// print front of queue
public void queueFront()
{
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
return;
}
Console.Write("\nFront Element is: {0}", queue[front]);
return;
}
}
public class StaticQueueinjava {
// Driver code
public static void Main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
Console.Write("\n\nafter two node deletion\n\n");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
Queue is Empty
20 <-- 30 <-- 40 <-- 50 <--
Queue is full
20 <-- 30 <-- 40 <-- 50 <--
after two node deletion
40 <-- 50 <--
Front Element is: 40
入队时间复杂度:O(1)
出队时间复杂度:O(n)
优化:
我们可以在 O(1) 时间内实现入队和出队操作。为此,我们可以使用队列的链表实现或队列的循环数组实现。