帮我编写一个可以运行的有中文操作指引的包含for循环函数的并且可以实现以下功能的一个C语言程序:1.能 利用“数组”作为存储结构方法实现“堆栈”的基本操作,包括:创建堆栈、入栈、出栈、空栈、满栈等操作。 .能 利用堆栈完成,将一组输入的数据逆序输出。能 利用“数组”作为存储结构方法实现“队列”的基本操作,包括:创建队列、入队列、出队列、队列满、队列空等基本操作。 能利用“堆栈”和“队列”,对输入的一组“字符串”判断是否“回文”
时间: 2023-06-27 21:04:18 浏览: 54
以下是一个可以实现以上要求的C语言程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
// 堆栈结构体
typedef struct Stack {
int top;
int data[MAX_SIZE];
} Stack;
// 队列结构体
typedef struct Queue {
int front;
int rear;
int data[MAX_SIZE];
} Queue;
// 创建堆栈
void createStack(Stack *s) {
s->top = -1;
}
// 判断堆栈是否为空
int isEmptyStack(Stack *s) {
return s->top == -1;
}
// 判断堆栈是否已满
int isFullStack(Stack *s) {
return s->top == MAX_SIZE - 1;
}
// 入栈
void push(Stack *s, int value) {
if (isFullStack(s)) {
printf("Stack is full.\n");
return;
}
s->top++;
s->data[s->top] = value;
}
// 出栈
int pop(Stack *s) {
if (isEmptyStack(s)) {
printf("Stack is empty.\n");
return -1;
}
int value = s->data[s->top];
s->top--;
return value;
}
// 创建队列
void createQueue(Queue *q) {
q->front = q->rear = 0;
}
// 判断队列是否为空
int isEmptyQueue(Queue *q) {
return q->front == q->rear;
}
// 判断队列是否已满
int isFullQueue(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
// 入队列
void enqueue(Queue *q, int value) {
if (isFullQueue(q)) {
printf("Queue is full.\n");
return;
}
q->data[q->rear] = value;
q->rear = (q->rear + 1) % MAX_SIZE;
}
// 出队列
int dequeue(Queue *q) {
if (isEmptyQueue(q)) {
printf("Queue is empty.\n");
return -1;
}
int value = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return value;
}
// 将一组输入的数据逆序输出
void reverseInput() {
Stack s;
createStack(&s);
int input;
printf("Please enter a group of numbers (separated by spaces):\n");
while (scanf("%d", &input) == 1) {
push(&s, input);
}
printf("Reverse order: ");
while (!isEmptyStack(&s)) {
printf("%d ", pop(&s));
}
printf("\n");
}
// 判断字符串是否回文
int isPalindrome(char *str) {
Queue q;
Stack s;
createQueue(&q);
createStack(&s);
int len = strlen(str);
for (int i = 0; i < len; i++) {
enqueue(&q, str[i]);
push(&s, str[i]);
}
while (!isEmptyQueue(&q)) {
int dequeueValue = dequeue(&q);
int popValue = pop(&s);
if (dequeueValue != popValue) {
return 0;
}
}
return 1;
}
int main() {
// 测试堆栈
Stack s;
createStack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("pop: %d\n", pop(&s));
printf("pop: %d\n", pop(&s));
printf("pop: %d\n", pop(&s));
printf("pop: %d\n", pop(&s));
// 测试队列
Queue q;
createQueue(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
printf("dequeue: %d\n", dequeue(&q));
printf("dequeue: %d\n", dequeue(&q));
printf("dequeue: %d\n", dequeue(&q));
printf("dequeue: %d\n", dequeue(&q));
// 将一组输入的数据逆序输出
reverseInput();
// 判断字符串是否回文
char str[MAX_SIZE];
printf("Please enter a string:\n");
scanf("%s", str);
printf("%s is %s\n", str, isPalindrome(str) ? "a palindrome" : "not a palindrome");
return 0;
}
```