赞
踩
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #define MAX_SIZE 10
-
- int queue[MAX_SIZE];
- int front = -1, rear = -1;
-
- void enqueue(int data) {
- if (rear >= MAX_SIZE - 1) {
- printf("队列已满,无法插入数据\n");
- }
- else {
- if (front == -1)
- front = 0;
- rear++;
- queue[rear] = data;
- }
- }
-
- int dequeue() {
- if (front == -1 || front > rear) {
- printf("队列为空,无法删除数据\n");
- return -1;
- }
- else {
- int data = queue[front];
- front++;
- return data;
- }
- }
-
- void display() {
- printf("当前队列中的元素为:\n");
- for (int i = front; i <= rear; i++) {
- printf("%d ", queue[i]);
- }
- printf("\n");
- }
-
- int main() {
- int n = 0;
- int flag = 1;//判断用户选择数是否合法
- while (flag)
- {
- printf("请输入队列的一到十内的目标长度\n");
- scanf("%d", &n);
- if (n >= 1 && n <= 10)
- {
- flag = 0;
- }
- }
- int dight = 0;
- for (int i = 0; i < n; i++)
- {
- printf("请输入第%d个元素的值:\n", i + 1);
- scanf("%d", &dight);
- enqueue(dight);
- }
- display();//显示刚刚压入的队列元素
-
- int flags = 1;
- while (flags)
- {
- int choice = 1;
- int sth = 0;
- printf("-------------------------------------------------------\n");
- printf("请选择你想要的执行的操作:1.添加队列 2.删除队列 0.退出\n");
- printf("-------------------------------------------------------\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- printf("请输入待添加的元素:\n");
- scanf("%d", &sth);
- enqueue(sth);
- display();
- break;
- case 2:
- dequeue();
- display();
- break;
- case 0:
- flags = 0;
- break;
- default:
- printf("请输入有效值!\n");
- break;
- }
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。