我要一个用C语言堆栈实现圆桌会议安排
时间: 2023-06-11 08:08:25 浏览: 63
好的,以下是一个用C语言实现的圆桌会议安排的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 堆栈的最大容量
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
int is_empty(Stack *s) {
return (s->top == -1);
}
int is_full(Stack *s) {
return (s->top == MAX_SIZE - 1);
}
void push(Stack *s, int x) {
if (is_full(s)) {
printf("Stack Overflow\n");
exit(1);
}
s->top++;
s->data[s->top] = x;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("Stack Underflow\n");
exit(1);
}
int x = s->data[s->top];
s->top--;
return x;
}
int main() {
int n, i;
printf("Enter the number of people in the meeting: ");
scanf("%d", &n);
Stack s;
s.top = -1; // 初始化堆栈
// 将人员编号推入堆栈中
for (i = 1; i <= n; i++) {
push(&s, i);
}
// 开始安排会议
printf("Meeting order: ");
while (!is_empty(&s)) {
int x = pop(&s);
printf("%d ", x);
if (!is_empty(&s)) {
int y = pop(&s);
push(&s, y);
}
}
return 0;
}
```
运行程序后,输入参加会议的人数,程序会将人员编号推入堆栈中,并按照圆桌会议的顺序依次弹出堆栈中的元素,直到堆栈为空。输出的结果为会议的顺序。