Quick actions

cmd+k|ctrl+k

Navigation

Languages

顺序存储队列 基本操作

Snippet info

Language

Cpp

Visibility

public

Author

qwas

Created

2024-05-20T09:24:15.638904Z

Updated

2024-05-20T09:57:43.364908Z

#include <stdio.h>

#define MaxSize 50
#define ElemType int

typedef struct
{
  ElemType data[MaxSize];
  int front, rear;
} SqQueue;

// 初始化队列
void InitQueue(SqQueue &Q)
{
  Q.rear = Q.front = 0; // 初始化 队首、队尾指针
}

// 判队空
bool isEmpty(SqQueue Q)
{
  if (Q.rear = Q.front) // 队空
    return true;
  else
    return false;
}

// 入队
bool EnQueue(SqQueue &Q, ElemType x)
{
  if ((Q.rear + 1) % MaxSize == Q.front) // 队满
    return false;
  Q.data[Q.rear] = x;
  Q.rear = (Q.rear + 1) % MaxSize; // 队尾指针加1取模
  return true;
}

// 出队
bool DeQueue(SqQueue &Q, ElemType &x)
{
  if (Q.rear = Q.front) // 队空,返回错误
    return false;
  x = Q.data[Q.front];
  Q.front = (Q.front + 1) % MaxSize; // 队头指针加1取模
  return true;
}

int main()
{
  printf("hello world");
}
INFO