目录
4.3
#include <stdio.h>
#include <stdlib.h>
// 定义栈的最大容量
#define MAX_SIZE 100
// 定义栈结构
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int isEmpty(Stack *s) {
return s->top == -1;
}
// 入栈操作
void push(Stack *s, char c) {
if (s->top == MAX_SIZE - 1) {
printf("Stack is full\n");
exit(1);
}
s->data[++(s->top)] = c;
}
// 出栈操作
char pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
exit(1);
}
return s->data[(s->top)--];
}
// 车厢调度函数
void trainDispatch(char *carriages, int n) {
Stack s;
initStack(&s);
int i = 0;
while (i < n) {
// 如果栈为空或者栈顶车厢不是硬卧车厢,则将当前车厢入栈
if (isEmpty(&s) || s.data[s.top] != 'H') {
push(&s, carriages[i++]);
} else {
// 否则将栈顶车厢出栈,并输出出栈操作
char c = pop(&s);
printf("出栈: %c\n", c);
}
}
// 将栈中剩余的车厢依次出栈
while (!isEmpty(&s)) {
char c = pop(&s);
printf("出栈: %c\n", c);
}
}
int main() {
int n;
printf("请输入车厢数量: ");
scanf("%d", &n);
char carriages[MAX_SIZE];
printf("请输入车厢序列(例如:SHSHS): ");
scanf("%s", carriages);
trainDispatch(carriages, n);
return 0;
}
4.4
#include <stdio.h>
#include <string.h>
int isPattern(char *sequence) {
int length = strlen(sequence);
int i, j, k;
for (i = 1; i < length - 1; i++) {
if (sequence[i] == '&') {
// 分割序列
char sequence1[50];
char sequence2[50];
strncpy(sequence1, sequence, i);//strncpy用于将一个字符串的一部分复制到另一个字符串中
sequence1[i] = '\0';
strncpy(sequence2, sequence + i + 1, length - i - 1);
sequence2[length - i - 1] = '\0';
// 检查序列2是否是序列1的逆序列的逆序列
int len1 = strlen(sequence1);
int len2 = strlen(sequence2);
if (len1 != len2) {
return 0;
}
for (j = 0, k = len2 - 1; j < len1 && k >= 0; j++, k--) {
if (sequence1[j] != sequence2[k]) {
return 0;
}
}
if (j != len1 || k != -1) {
return 0;
}
return 1;
}
}
return 0;
}
int main() {
char sequence[100];
printf("请输入一个以'@'为结束符的字符序列:\n");
scanf("%[^@]s", sequence);//表示除了@之外的字符输入,^表示取反
if (isPattern(sequence)) {
printf("该字符序列符合模式。\n");
} else {
printf("该字符序列不符合模式。\n");
}
return 0;
}
4.5
#include <stdio.h>
#include <stdbool.h>
// 定义栈结构
#define MAX_SIZE 100
typedef struct {
char items[MAX_SIZE];
int top;
} Stack;
// 初始化栈
void initStack(Stack *stack) {
stack->top = -1;
}
// 压栈
void push(Stack *stack, char c) {
stack->items[++stack->top] = c;
}
// 弹栈
char pop(Stack *stack) {
return stack->items[stack->top--];
}
// 判断括号是否合法
bool isValidParentheses(char *expression) {
Stack stack;
initStack(&stack);
while (*expression) {
if (*expression == '(' || *expression == '{' || *expression == '[') {
push(&stack, *expression);
} else if (*expression == ')' || *expression == '}' || *expression == ']') {
if (stack.top == -1) {
return false; // 右括号多余,返回 false
}
char topChar = pop(&stack);
if ((*expression == ')' && topChar != '(') ||
(*expression == '}' && topChar != '{') ||
(*expression == ']' && topChar != '[')) {
return false; // 括号不匹配,返回 false
}
}
expression++;
}
return stack.top == -1; // 栈为空则括号匹配,否则返回 false
}
int main() {
char expression[] = "{[()]}";
if (isValidParentheses(expression)) {
printf("括号匹配\n");
} else {
printf("括号不匹配\n");
}
return 0;
}
4.8
#include <stdio.h>
#include <stdlib.h>
// 定义栈节点结构体
typedef struct StackNode {
int m;
int n;
int result;
struct StackNode* next;
} StackNode;
// 定义栈结构体
typedef struct {
StackNode* top;
} Stack;
// 初始化栈
void initStack(Stack* stack) {
stack->top = NULL;
}
// 入栈
void push(Stack* stack, int m, int n, int result) {
StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->m = m;
newNode->n = n;
newNode->result = result;
newNode->next = stack->top;
stack->top = newNode;
}
// 出栈
void pop(Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty.\n");
exit(1);
}
StackNode* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}
// 取栈顶元素
StackNode* top(Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty.\n");
exit(1);
}
return stack->top;
}
// 判断栈是否为空
int isEmpty(Stack* stack) {
return stack->top == NULL;
}
// 非递归形式的 g 函数
int g(int m, int n) {
Stack stack;
initStack(&stack);
int result = 0;
push(&stack, m, n, result);
while (!isEmpty(&stack)) {
StackNode* topNode = top(&stack);
if (topNode->m == 0 && topNode->n >= 0) {
topNode->result = 0;
} else if (topNode->m > 0 && topNode->n >= 0) {
pop(&stack);
push(&stack, topNode->m - 1, 2 * topNode->n, 0);
push(&stack, 0, 0, topNode->n);
continue;
}
if (!isEmpty(&stack)) {
StackNode* parent = top(&stack);
parent->result += topNode->result;
}
pop(&stack);
}
return result;
}
int main() {
int m, n;
printf("Enter the values of m and n: ");
scanf("%d %d", &m, &n);
printf("g(%d, %d) = %d\n", m, n, g(m, n));
return 0;
}
4.9
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* rear;
} Queue;
void initQueue(Queue* q) {
q->rear = NULL;
}
void enqueue(Queue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = value;
if (q->rear == NULL) {
q->rear = newNode;
newNode->next = newNode;
} else {
newNode->next = q->rear->next;
q->rear->next = newNode;
q->rear = newNode;
}
}
int dequeue(Queue* q) {
if (q->rear == NULL) {
printf("Queue is empty.\n");
exit(1);
}
Node* front = q->rear->next;
int value = front->data;
if (q->rear == front) {
free(front);
q->rear = NULL;
} else {
q->rear->next = front->next;
free(front);
}
return value;
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printf("Dequeued element: %d\n", dequeue(&q));
printf("Dequeued element: %d\n", dequeue(&q));
printf("Dequeued element: %d\n", dequeue(&q));
return 0;
}
4.11
#include <stdio.h>
#include <stdlib.h>
#define maxSize 5 // 假设队列最大容量为 5
typedef struct {
int data[maxSize];
int rear; // 队尾指针
int length; // 队列中元素个数
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->rear = 0;
q->length = 0;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->length == 0;
}
// 判断队列是否已满
int isFull(Queue *q) {
return q->length == maxSize;
}
// 入队
void enQueue(Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full, cannot enqueue.\n");
return;
}
q->data[q->rear] = value;
q->rear = (q->rear + 1) % maxSize;
q->length++;
}
// 出队
int deQueue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty, cannot dequeue.\n");
exit(1);
}
int value = q->data[(q->rear - q->length + maxSize) % maxSize];
q->length--;
return value;
}
int main() {
Queue q;
initQueue(&q);
// 入队操作
enQueue(&q, 1);
enQueue(&q, 2);
enQueue(&q, 3);
enQueue(&q, 4);
enQueue(&q, 5);
// 队满,无法再入队
enQueue(&q, 6);
// 出队操作
printf("Dequeued element: %d\n", deQueue(&q));
printf("Dequeued element: %d\n", deQueue(&q));
printf("Dequeued element: %d\n", deQueue(&q));
printf("Dequeued element: %d\n", deQueue(&q));
printf("Dequeued element: %d\n", deQueue(&q));
// 队空,无法再出队
deQueue(&q);
return 0;
}
4.12
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_LENGTH 100
// 定义栈结构
typedef struct {
char data[MAX_LENGTH];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
bool isEmpty(Stack *s) {
return s->top == -1;
}
// 判断栈是否已满
bool isFull(Stack *s) {
return s->top == MAX_LENGTH - 1;
}
// 入栈
void push(Stack *s, char c) {
if (isFull(s)) {
printf("栈已满,无法入栈\n");
return;
}
s->data[++(s->top)] = c;
}
// 出栈
char pop(Stack *s) {
if (isEmpty(s)) {
printf("栈已空,无法出栈\n");
return '\0';
}
return s->data[(s->top)--];
}
int main() {
char str[MAX_LENGTH];
char ch;
int length = 0;
Stack s;
initStack(&s); // 初始化
printf("请输入以#结束的字符序列:\n");
while ((ch = getchar()) != '#') {
if (length >= MAX_LENGTH - 1) {
printf("字符序列过长!\n");
return 1;
}
str[length++] = ch;
push(&s, ch); // 将字符压入栈中
}
str[length] = '\0'; // 添加字符串结束符
// 依次弹出栈中的字符与字符序列比较
bool isPalindrome = true;
int i;
for (i = 0; i < length; i++) {
if (str[i] != pop(&s)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
printf("是回文字符串\n");
} else {
printf("不是回文字符串\n");
}
return 0;
}
标签:return,课后,int,top,Stack,集锦,printf,习题,stack
From: https://blog.csdn.net/m0_72674633/article/details/136858423