数据结构学习笔记(5)---静态与动态队列

本文是关于数据结构的学习笔记,主要探讨了静态队列和动态队列的概念。静态队列部分介绍了初始化、入栈和出栈操作。动态队列则在静态队列的基础上增加了判栈空的操作,详细阐述了其初始化、入栈、出栈的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)静态队列

数据结构:

typedef struct Node
{
    int date;
    struct Node *next;
}Node, *PNode;

typedef struct queue
{
    PNode top;
    PNode bottom;
}Queue, *PQueue;
(1)初始化
void InitQueue(Queue *queue)
{
    queue->top = 0;
    queue->bottom = 0;
}
(2)入栈
void Push(Queue *queue, int date)
{
    if ((queue->top + 1) % MAXSIZE == queue->bottom)
    {
        cout << "队列已满!" << endl;
        return;
    }
    queue->elem[queue->top] = date;
    queue->top = (queue->top +1)% MAXSIZE;
}
(2)出栈
void Pop(Queue * queue, int *date)
{
    if (queue->top == queue->bottom)
    {
        cout << "队列为空!" << endl;
        return;
    }
    (*date) = queue->elem[queue->bottom];
    queue->bottom = (queue->bottom + 1) % MAXSIZE;
}
(二)动态队列
(1)初始化
void InitQueue(PQueue pHead)
{
    pHead->top = NULL;
    pHead->bottom = NULL;
}
(2)入栈
void Push(PQueue pQueue,int date)
{
    PNode  pNew=  new Node;
    if (!pNew)
    {
        cout << "进队失败!" << endl;
        return;
    }
    pNew->date = date;
    pNew->next = NULL;
    if ( IsEmpty(pQueue))
    {
        pQueue->top = pQueue->bottom = pNew;
    }
    else
    {
        pQueue->top->next = pNew;
        pQueue->top = pNew;
    }
}
(3)出栈
void Pop(PQueue pQueue, int *date)
{
    if (IsEmpty(pQueue))
    {
        cout << "出队失败,队列为空!" << endl;
        return;
    }
    if (pQueue->bottom == pQueue->top)
    {
        PNode p =  pQueue->bottom;
        (*date) = pQueue->bottom->date;
        pQueue->bottom = pQueue->top = NULL;
        delete p;
    }
    else
    {
        (*date) = pQueue->bottom->date;
        PNode p = pQueue->bottom;
        pQueue->bottom = pQueue->bottom->next;
        delete p;
    }
}
(4)判栈空
bool  IsEmpty(PQueue pQueue)
{
    return pQueue->top == NULL&&pQueue->bottom == NULL;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值