用c语言,输入一个n,然后在顺序栈中压入并输出n个字符串
时间: 2023-05-25 12:05:02 浏览: 72
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 50
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
void push(Stack *s, char *str) {
if (is_full(s)) {
printf("Stack is full.\n");
return;
}
s->top++;
strcpy(s->data[s->top], str);
}
char * pop(Stack *s) {
if (is_empty(s)) {
printf("Stack is empty.\n");
return NULL;
}
return s->data[s->top--];
}
void output(Stack *s) {
while (!is_empty(s)) {
printf("%s\n", pop(s));
}
}
int main() {
int n, i;
char str[MAX_SIZE];
Stack s;
init(&s);
printf("Enter the number of strings: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter string %d: ", i + 1);
scanf("%s", str);
push(&s, str);
}
printf("The strings are:\n");
output(&s);
return 0;
}
```