将下列代码添加函数的嵌套调用与递归调用:#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 typedef struct Stack{ int data[MAXSIZE]; int top; }Stack; void push(Stack *s,int x){ if(s->top==MAXSIZE-1){ printf("栈已满"); }else s->data[++s->top]=x; } int pop(Stack *s){ if(s->top==-1){ printf("栈已空"); return -1; }else return s->data[s->top--]; } void int main(){ Stack s; int m,n; printf("输入元素个数:"); scanf("%d",&m); printf("输入元素:"); for(int i=0;i<m;i++){ scanf("%d",&n); push(&s,n); } int k; printf("输入弹出元素个数:"); scanf("%d",&k); for(int i=0;i<k;i++){ printf("出栈%d:\n",pop(&s)); } return 0; }
时间: 2024-01-02 12:04:57 浏览: 76
函数的嵌套调用:
```c
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct Stack{
int data[MAXSIZE];
int top;
}Stack;
void push(Stack *s,int x){
if(s->top==MAXSIZE-1){
printf("栈已满");
}else
s->data[++s->top]=x;
}
int pop(Stack *s){
if(s->top==-1){
printf("栈已空");
return -1;
}else
return s->data[s->top--];
}
void pop_k(Stack *s, int k){
if(k == 0) return;
printf("出栈%d:\n",pop(s));
pop_k(s, k-1);
}
int main(){
Stack s;
int m,n;
printf("输入元素个数:");
scanf("%d",&m);
printf("输入元素:");
for(int i=0;i<m;i++){
scanf("%d",&n);
push(&s,n);
}
int k;
printf("输入弹出元素个数:");
scanf("%d",&k);
pop_k(&s, k);
return 0;
}
```
递归调用:
```c
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct Stack{
int data[MAXSIZE];
int top;
}Stack;
void push(Stack *s,int x){
if(s->top==MAXSIZE-1){
printf("栈已满");
}else
s->data[++s->top]=x;
}
int pop(Stack *s){
if(s->top==-1){
printf("栈已空");
return -1;
}else
return s->data[s->top--];
}
void pop_k(Stack *s, int k){
if(k == 0) return;
printf("出栈%d:\n",pop(s));
pop_k(s, k-1);
}
int main(){
Stack s;
int m,n;
printf("输入元素个数:");
scanf("%d",&m);
printf("输入元素:");
for(int i=0;i<m;i++){
scanf("%d",&n);
push(&s,n);
}
int k;
printf("输入弹出元素个数:");
scanf("%d",&k);
for(int i=0;i<k;i++){
printf("出栈%d:\n",pop(&s));
}
return 0;
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](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)