#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
/* 定义循环队列结构体 */
typedef struct {
int data[MAX_SIZE]; // 存储元素的数组
int front, rear; // 队头指针和队尾指针
int count; // 记录元素个数
} Queue;
/* 初始化队列 */
void initQueue(Queue *queue) {
queue->front = queue->rear = 0;
queue->count = 0;
}
/* 判断队列是否已满 */
int isFull(Queue *queue) {
return queue->count == MAX_SIZE;
}
/* 判断队列是否为空 */
int isEmpty(Queue *queue) {
return queue->count == 0;
}
/* 元素入队 */
void enqueue(Queue *queue, int value) {
if (isFull(queue)) {
printf("The queue is full!\n");
return;
}
/* 在这里进行一次判断,防止把元素直接存入队列中 */
if (value > MAX_SIZE) {
printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
return;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->count++;
printf("队列元素为: %d\n", value);
}
/* 元素出队 */
int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("The queue is empty!\n");
return 0;
}
int value = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
queue->count--;
printf("出队列元素为: %d\n", value);
return value;
}
/* 打印队列 */
void printQueue(Queue *queue) {
printf("队列为: ");
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
/* 主函数 */
int main() {
Queue queue;
initQueue(&queue);
/* 从键盘输入元素数据,一旦输入超限,把超出部分未入队的元素暂存起来,等前面的元素出队后再入队 */
int n, value, tmp_n = 0, tmp_data[MAX_SIZE];
printf("请输入队列中元素的个数为:");
scanf("%d", &n);
while (n > MAX_SIZE) {
printf("输入数字大于容纳量,请重新输入元素个数:");
scanf("%d", &n);
}
printf("请输入%d个元素:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &value);
if (value > MAX_SIZE) {
printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
tmp_data[tmp_n] = value;
tmp_n++;
} else {
enqueue(&queue, value);
}
}
// 一直出队,直到队列为空
while (!isEmpty(&queue)) {
int value = dequeue(&queue);
printQueue(&queue);
// 如果有暂存的元素,在最后再入队
while (tmp_n > 0) {
tmp_n--;
enqueue(&queue, tmp_data[tmp_n]);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 4
/* 定义循环队列结构体 */
typedef struct {
int data[MAX_SIZE]; // 存储元素的数组
int front, rear; // 队头指针和队尾指针
int count; // 记录元素个数
} Queue;
/* 初始化队列 */
void initQueue(Queue *queue) {
queue->front = queue->rear = 0;
queue->count = 0;
}
/* 判断队列是否已满 */
int isFull(Queue *queue) {
return queue->count == MAX_SIZE;
}
/* 判断队列是否为空 */
int isEmpty(Queue *queue) {
return queue->count == 0;
}
/* 元素入队 */
void enqueue(Queue *queue, int value) {
if (isFull(queue)) {
printf("The queue is full! The element cannot be added temporarily!\n");
return;
}
/* 在这里进行一次判断,防止把元素直接存入队列中 */
if (value > MAX_SIZE) {
printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
return;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->count++;
printf("Enqueued element: %d\n", value);
}
/* 元素出队 */
int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("The queue is empty!\n");
exit(1);
}
int value = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
queue->count--;
printf("Dequeued element: %d\n", value);
return value;
}
/* 打印队列 */
void printQueue(Queue *queue) {
printf("Queue: ");
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
/* 主函数 */
int main() {
Queue queue;
initQueue(&queue);
/* 从键盘输入元素数据,一旦输入超限,把超出部分未入队的元素暂存起来,等前面的元素出队后再入队 */
int n, value, tmp_n = 0, tmp_data[MAX_SIZE];
printf("Please input the number of elements:");
scanf("%d", &n);
while (n > MAX_SIZE) {
printf("The number of elements cannot exceed the maximum size of the queue! Please enter again:");
scanf("%d", &n);
}
printf("Please input %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &value);
if (value > MAX_SIZE) {
printf("The input value exceeds the maximum value allowed by the queue! It will be stored temporarily\n");
tmp_data[tmp_n] = value;
tmp_n++;
} else {
enqueue(&queue, value);
}
}
// 一直出队,直到队列为空
while (!isEmpty(&queue)) {
int value = dequeue(&queue);
printQueue(&queue);
// 如果有暂存的元素,在最后再入队
while (tmp_n > 0) {
tmp_n--;
enqueue(&queue, tmp_data[tmp_n]);
}
}
return 0;
}
#include <stdio.h>
#define MAX 100
#define FALSE 0
#define TRUE 1
//循环队列
typedef struct {
int element[MAX];
int front; //头指针
int rear; //尾指针
} SeqQueue;
//初始化循环队列
void InitQueue(SeqQueue *q)
{
q->front = q->rear = 0;
}
//入队
int EnterQueue(SeqQueue *q, int x) {
if ((q->rear + 1) % MAX == q->front) {
printf("---队列已满---");
return FALSE;
}
q->element[q->rear] = x;
q->rear = (q->rear + 1) % MAX;
return TRUE;
}
//出队
int DeleteQueue(SeqQueue *q, int *x) {
if (q->front == q->rear) {
printf("---队列为空---");
return FALSE;
}
*x = q->element[q->front];
q->front = (q->front + 1) % MAX;
return TRUE;
}
//取对头元素
int GetHead(SeqQueue *q, int *x) {
if (q->front == q->rear)
return FALSE;
*x = q->element[q->front];
return TRUE;
}
//判断队列是否为空
int IsEmpty(SeqQueue *q) {
if (q->front == q->rear)
return TRUE;
else
return FALSE;
}
//打印杨辉三角
void YangHuiTriangle(int N) {
SeqQueue q;
InitQueue(&q);
int n, i, x, temp;
EnterQueue(&q, 1); //第一行元素入队
for (n = 2; n <= N; n++) {
EnterQueue(&q, 1); //第n行第一个元素入队
for (i = N; i >= n; i--) //给数字间加空格,打印出金字塔形状
printf(" "); // N为打印的行数,n为每行的元素个数
for (i = 1; i <= n - 2; i++) { //利用队中第n-1行元素产生第n行的中间n-2个元素并入队
DeleteQueue(&q, &temp); //出队元素赋给temp
printf("%6d", temp); //打印第n-1行的元素
GetHead(&q, &x);
temp = temp + x; //利用第n-1行元素产生第n行元素
EnterQueue(&q, temp); //可以利用画图理解
}
DeleteQueue(&q, &x);
printf("%6d", x); //打印n-1行最后一个元素
EnterQueue(&q, 1);
printf("\n");
}
while (!IsEmpty(&q)) { //打印最后一行
DeleteQueue(&q, &x);
printf("%6d", x);
}
}
//主函数:
int main() {
int N;
printf("请输入想打印的行数:");
scanf("%d", &N);
YangHuiTriangle(N);
printf("\n");
return 0;
}