题目:用递归颠倒一个栈。例如输入栈{1, 2, 3, 4, 5},1在栈顶。 颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。
时间: 2023-11-19 20:08:09 浏览: 28
这个问题属于计算机编程问题,可以给出如下递归的解法:
```python
def reverse_stack(stack):
if len(stack) == 1:
return stack
else:
last_item = stack.pop()
reversed_stack = reverse_stack(stack)
reversed_stack.append(last_item)
return reversed_stack
```
其中,`stack.pop()`表示弹出栈顶元素,`stack.append(x)`表示在栈顶添加元素x。
相关问题
用递归颠倒一个栈。例如输入栈{1, 2, 3, 4, 5},1在栈顶。 颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。用C语言写
下面是用C语言实现递归颠倒一个栈的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top >= MAX_SIZE - 1) {
printf("Stack overflow!\n");
exit(1);
}
stack[++top] = item;
}
int pop() {
if (top < 0) {
printf("Stack underflow!\n");
exit(1);
}
return stack[top--];
}
int is_empty() {
return top < 0;
}
void display() {
printf("[");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("]\n");
}
void insert_at_bottom(int x) {
if (is_empty()) {
push(x);
} else {
int item = pop();
insert_at_bottom(x);
push(item);
}
}
void reverse() {
if (!is_empty()) {
int item = pop();
reverse();
insert_at_bottom(item);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements to push onto the stack: ", n);
for (int i = 0; i < n; i++) {
int item;
scanf("%d", &item);
push(item);
}
printf("Before reversal: ");
display();
reverse();
printf("After reversal: ");
display();
return 0;
}
```
实现的思路是先将栈原来的元素逐个弹出,然后再递归地将它们插入到栈的底部,这样就达到了颠倒的效果。
用C++写一个通过递归颠倒一个栈
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct stack {
int data[MAX_SIZE];
int top;
} Stack;
Stack *createStack() {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->top = -1;
return s;
}
int isFull(Stack *s) {
return s->top == MAX_SIZE - 1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
void push(Stack *s, int x) {
if (isFull(s)) {
printf("Error: Stack is full\n");
return;
}
s->data[++s->top] = x;
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Error: Stack is empty\n");
return -1;
}
return s->data[s->top--];
}
void printStack(Stack *s) {
printf("Stack: ");
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->data[i]);
}
printf("\n");
}
void reverse(Stack *s) {
if (isEmpty(s)) {
return;
}
int x = pop(s);
reverse(s);
push(s, x);
}
int main() {
Stack *s = createStack();
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
push(s, 5);
printf("Before reverse:\n");
printStack(s);
reverse(s);
printf("After reverse:\n");
printStack(s);
return 0;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)