数据结构 队列基本操作的代码
时间: 2023-10-12 18:08:43 浏览: 60
以下是队列基本操作的代码示例,包括入队、出队、获取队头元素和判断队列是否为空:
```python
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if self.is_empty():
raise Exception("queue is empty")
return self.items.pop(0)
def get_front(self):
if self.is_empty():
raise Exception("queue is empty")
return self.items[0]
```
其中,`enqueue` 方法用于将元素入队,即添加到队列的末尾;`dequeue` 方法用于将队头元素出队,即删除并返回队列的第一个元素;`get_front` 方法用于获取队头元素,但不进行出队操作;`is_empty` 方法用于判断队列是否为空。
相关问题
数据结构队列的操作代码
队列是一种先进先出(FIFO)的线性数据结构,它支持在队列尾部插入元素,从队列头部删除元素。以下是队列的基本操作代码:
```
// 定义队列结构体
typedef struct queue {
int *arr; // 队列元素数组
int front; // 队列头
int rear; // 队列尾
int size; // 队列当前元素个数
int capacity; // 队列容量
} Queue;
// 创建队列
Queue* createQueue(int capacity) {
Queue *q = (Queue*) malloc(sizeof(Queue));
q->arr = (int*) malloc (sizeof(int) * capacity);
q->front = 0;
q->rear = -1;
q->size = 0;
q->capacity = capacity;
return q;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->size == 0;
}
// 判断队列是否已满
int isFull(Queue *q) {
return q->size == q->capacity;
}
// 入队
void enqueue(Queue *q, int data) {
if (isFull(q)) {
printf("Queue is full.\n");
return;
}
q->rear = (q->rear + 1) % q->capacity;
q->arr[q->rear] = data;
q->size++;
}
// 出队
void dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
q->front = (q->front + 1) % q->capacity;
q->size--;
}
// 获取队列头部元素
int front(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return -1;
}
return q->arr[q->front];
}
// 获取队列尾部元素
int rear(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return -1;
}
return q->arr[q->rear];
}
```
以上是队列的基本操作代码,可以通过调用这些函数来创建、操作队列。需要注意的是,队列的实现中使用了环形数组,这样可以避免数组元素移动导致的空间浪费。
数据结构中队列基本操作的完整代码
以下是队列基本操作的完整代码:
```
#include <stdio.h>
#include <stdlib.h>
// 队列结构体
typedef struct Queue
{
int front; // 队首指针
int rear; // 队尾指针
int size; // 队列大小
int *arr; // 队列数组指针
} Queue;
// 初始化队列
Queue *initQueue(int n)
{
Queue *Q = (Queue *)malloc(sizeof(Queue));
if (Q == NULL)
{
printf("初始化失败\n");
return NULL;
}
Q->front = 0;
Q->rear = 0;
Q->size = n;
Q->arr = (int *)malloc(sizeof(int) * n);
printf("初始化成功\n");
return Q;
}
// 判断队列是否为空
int isEmpty(Queue *Q)
{
return Q->front == Q->rear ? 1 : 0;
}
// 判断队列是否满了
int isFull(Queue *Q)
{
return (Q->rear + 1) % Q->size == Q->front ? 1 : 0;
}
// 入队
void enQueue(Queue *Q, int x)
{
if (isFull(Q))
{
printf("队列已满\n");
return;
}
Q->arr[Q->rear] = x;
Q->rear = (Q->rear + 1) % Q->size;
printf("入队成功:%d\n", x);
}
// 出队
int deQueue(Queue *Q)
{
if (isEmpty(Q))
{
printf("队列为空\n");
return -1;
}
int data = Q->arr[Q->front];
Q->front = (Q->front + 1) % Q->size;
printf("出队成功:%d\n", data);
return data;
}
// 打印队列元素
void printQueue(Queue *Q)
{
printf("当前队列元素为:");
for (int i = Q->front; i != Q->rear; i = (i + 1) % Q->size)
{
printf("%d ", Q->arr[i]);
}
printf("\n");
}
int main()
{
Queue *myQueue = initQueue(5);
enQueue(myQueue, 1);
enQueue(myQueue, 2);
enQueue(myQueue, 3);
enQueue(myQueue, 4);
enQueue(myQueue, 5);
enQueue(myQueue, 6); //队列已满,应该提示队列已满,而不是入队成功:6
printQueue(myQueue);
deQueue(myQueue);
deQueue(myQueue);
printQueue(myQueue);
enQueue(myQueue, 6);
enQueue(myQueue, 7);
printQueue(myQueue);
return 0;
}
```
这个代码实现了队列的基本操作:初始化队列、判断队列是否为空、判断队列是否满了、入队、出队、打印队列元素。你可以在编程中调用这些函数来实现队列的功能。