假设所贴脸谱的颜色顺序是白色、黄色、黑色、红色、青色、金色、银色,请用C语言栈输出扯脸的序列
时间: 2023-10-01 20:08:40 浏览: 40
以下是用C语言栈输出扯脸的序列的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 7
typedef struct {
int top;
int data[MAX_SIZE];
} Stack;
void push(Stack *s, int value) {
if (s->top == MAX_SIZE - 1) {
printf("Stack overflow!\n");
exit(1);
}
s->top++;
s->data[s->top] = value;
}
int pop(Stack *s) {
if (s->top == -1) {
printf("Stack underflow!\n");
exit(1);
}
int value = s->data[s->top];
s->top--;
return value;
}
int main() {
Stack s;
s.top = -1;
push(&s, 1); // 白色
push(&s, 2); // 黄色
push(&s, 3); // 黑色
push(&s, 4); // 红色
push(&s, 5); // 青色
push(&s, 6); // 金色
push(&s, 7); // 银色
while (s.top != -1) {
printf("%d ", pop(&s));
}
return 0;
}
```
输出结果为:
```
7 6 5 4 3 2 1
```
即银色、金色、青色、红色、黑色、黄色、白色。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)