链式队列的实现

简介: 链式队列的实现

一、队列的结构定义

//队列的结构定义
typedef int QDataType;
typedef struct QueueNode {
  QDataType val;
  struct QueueNode* next;
}QNode;
//将头指针和尾指针存放到一个结构体中组成队列,易于找到队列头和尾且无需使用二级指针
typedef struct QueueNode {
  QNode* phead;
  QNode* ptail;
  int size;
}Queue;

二、队列的初始化

//队列的初始化
void QueueInit(Queue* pq)
{
  assert(pq);
  pq->phead = pq->ptail = NULL;
  pq->size = 0;
}

三、队列的打印

//队列的打印
void QueuePrint(Queue* pq)
{
  assert(pq);
  if (pq->phead == NULL)
    printf("NULL\n");
  else
  {
    QNode *cur = pq->phead;
    while (cur)
    {
      printf("%d ", cur->val);
    }
    printf("\n");
  }
}

四、入队

//入队列
void QueuePush(Queue* pq, QDataType x)
{
  assert(pq);
  QNode* newnode = (QNode*)malloc(sizeof(QNode));
  if (newnode == NULL)
  {
    perror("malloc fail");
    exit(-1);
  }
  newnode->val = x;
  newnode->next = NULL;
  if (pq->phead == NULL)
  {
    pq->phead = pq->ptail = newnode;
  }
  else
  {
    pq->ptail->next = newnode;
    pq->ptail = newnode;
  }
  pq->size++;
}

五、出队

//出队列
void QueuePop(Queue* pq)
{
  assert(pq);
  assert(pq->ptail);
  if (pq->phead == pq->ptail)//队列中只有一个元素
  {
    pq->ptail = NULL;
  }
  QNode* tmp = pq->phead;
  pq->phead = pq->phead->next;
  free(tmp);
  tmp = NULL;
  pq->size--;
}

六、取队头元素

//取队头元素
QDataType QueueFront(Queue* pq)
{
  assert(pq);
  assert(pq->phead);//空队列
  return pq->phead->val;
}

七、取队尾元素

//取队尾元素
QDataType QueueBack(Queue* pq)
{
  assert(pq);
  assert(pq->ptail);//空队列
  return pq->ptail->val;
}

八、判断队列是否为空

//判断队列是否为空
bool QueueEmpty(Queue* pq)
{
  return pq->phead == NULL;
}

九、求队列大小

//求队列大小
int QueueSize(Queue* pq)
{
  assert(pq);
  return pq->size;
}

十、销毁队列

//销毁队列
void QueueDestroy(Queue* pq)
{
  assert(pq);
  QNode* cur = pq->phead;
  while (cur)
  {
    QNode* tmp = cur;
    cur = cur->next;
    free(tmp);
    tmp = NULL;
  }
  pq->phead = NULL;
  pq->ptail = NULL;
  pq->size = 0;
}

十一、测试代码

void test01()
{
    //定义一个队列
    Queue q;
    //初始化队列
    QueueInit(&q);
    //入队
    QueuePush(&q, 1);
    QueuePush(&q, 2);
    QueuePush(&q, 3);
    QueuePush(&q, 4);
    QueuePush(&q, 5);
    //队列打印
    QueuePrint(&q);
    //出队列
    QueuePop(&q);
    QueuePop(&q);
    QueuePop(&q);
    //队列打印
    QueuePrint(&q);
    //取队头元素
    printf("%d\n", QueueFront(&q));
    //取队尾元素
    printf("%d\n", QueueBack(&q));
    //判断队列是否为空
    if (QueueEmpty(&q))
        printf("空\n");
    else
        printf("非空\n");
    //求队列大小
    printf("%d\n", QueueSize(&q));
    //销毁队列
    QueueDestroy(&q);
}
int main()
{
    test01();
}


目录
相关文章
|
10天前
|
人工智能 安全 Linux
【OpenClaw保姆级图文教程】阿里云/本地部署集成模型Ollama/Qwen3.5/百炼 API 步骤流程及避坑指南
2026年,AI代理工具的部署逻辑已从“单一云端依赖”转向“云端+本地双轨模式”。OpenClaw(曾用名Clawdbot)作为开源AI代理框架,既支持对接阿里云百炼等云端免费API,也能通过Ollama部署本地大模型,完美解决两类核心需求:一是担心云端API泄露核心数据的隐私安全诉求;二是频繁调用导致token消耗过高的成本控制需求。
5542 13
|
18天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
22049 118

热门文章

最新文章