Queue 队列

队列是一种遵循先进先出(FIFO)原则的数据结构。插入发生在队尾(EnQueue),删除发生在队头(Dequeue)。文章介绍了如何使用数组和链表来实现队列,强调了在数组实现中使用取模运算来管理循环数组以优化空间利用,以及链表实现中维护尾指针以实现O(1)的时间复杂度。
摘要由CSDN通过智能技术生成

Queue 队列

队列与栈相反,具有先进先出(First-In-First-Out) 的特点。

插入要在队尾操作,删除在队头操作。队列有以下几种操作:

  1. EnQueue(x) / Push(x) 插入一个元素
  2. Dequeue() / Pop() 删除队头一个元素
  3. Front() / Peek() 返回队列头部的元素
  4. IsEmpty() 检查队列是否为空

以上操作时间复杂度一定要为O(1)

使用数组实现队列

#define MAX_SIZE 101

class Queue
{
private:
    int A[MAX_SIZE];
    int front, rear;
public:
    Queue()
    {
        front = -1;
        rear = -1;
    }

    bool IsEmpty()
    {
        return (front == -1 && rear == -1);
    }

    bool IsFull()
    {
        return (rear + 1) % MAX_SIZE == front ? true : false;
    }

    void Enqueue(int x)
    {
        cout<<"Enqueuing"<<x<<" \n";
        if (IsFull())
        {
            cout<<"Error: Queue is full\n";
            return;
        }
        if (IsEmpty())
        {
            front = rear = 0;
        }
        else
        {
            rear = (rear + 1) % MAX_SIZE;
        }
        A[rear] = x;
    }

    void Dequeue()
    {
        cout<<"Dequeuing \n";
        if (IsEmpty())
        {
            cout<<"Error: Queue is empty\n";
            return;
        }
        else if(front == rear) //代表队列中只有一个数
        {
            rear = front = -1;
        }
        else
        {
            front = (front + 1) % MAX_SIZE;
        }
    }

    int Front()
    {
        if (front == -1)
        {
            cout<<"Error: cannot return front from empty queue\n";
            return -1;
        }
        return A[front];
    }

代码中几处取模运算的含义是:利用循环数组,使得在 rear 到顶且 front 不在 0 处时数组中的几处删除过的废弃空间得以利用。

使用链表实现队列

我们使用链表实现队列,想在队尾插入新元素时,需要从头节点遍历到链表末尾然后进行插入操作,这样就会是O(n) 的复杂度。但如果我们在创建链表时多设一个指向尾部的指针,并且随着链表的变化而变化,这样队列的任何操作就都是O(1)的复杂度了。

typedef struct Node
{
    int data;
    struct Node* next;
}Node;

Node* front = NULL;
Node* rear = NULL;

void Enqueue(int x)
{
    Node* temp = (Node*) malloc(sizeof(Node));
    temp->data = x;
    temp->next = NULL;
    if(front == NULL && rear == NULL)//队列为空时
    {
        front = rear = temp;
        return;
    }
    rear->next = temp;
    rear = temp;
}

void Dequeue()
{
    Node* temp = front;
    if (front == NULL)
    {
        printf("Queue is empty\n");
        return;
    }
    if (front == rear)
        front = rear = NULL;//仅剩一个元素时
    else front = front->next;

    free(temp);
}

int Front()
{
    if (front == NULL)
    {
        printf("Queue is empty/n");
        return -1 ;
    }
    return front->data;
}

void Print()
{
    Node *temp = front;
    while (temp != NULL)
    {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值