#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// โครงสร้างของ stack
typedef struct Stack {
char *data;
struct Stack *next;
} Stack;
// ฟังก์ชัน push ตัวอักษรเข้าไปใน stack
void push(Stack **top, char c) {
Stack
*newNode
= (Stack
*)malloc(sizeof(Stack
)); newNode
->data
= (char *)malloc(sizeof(char)); *(newNode->data) = c;
newNode->next = *top;
*top = newNode;
}
// ฟังก์ชัน pop ตัวอักษรออกจาก stack
char pop(Stack **top) {
if (*top == NULL) return '\0';
Stack *temp = *top;
char c = *(temp->data);
*top = (*top)->next;
return c;
}
// ฟังก์ชันสร้าง stack จาก string
void createStack(Stack **top, char *str) {
for(int i
= 0; i
< strlen(str
); i
++) { push(top, str[i]);
}
}
// ฟังก์ชันพิมพ์ stack ออกมาเป็นประโยค
void printFromStack(Stack **top) {
char c;
while((c = pop(top)) != '\0') {
}
}
int main() {
// แบ่งประโยคเป็นหลายส่วน
char part1[] = "pen";
char part2[] = " fan";
char part3[] = " gun";
char part4[] = " mai?";
Stack *stack = NULL;
// เพิ่มแต่ละส่วนเข้าไปใน stack
createStack(&stack, part1);
createStack(&stack, part2);
createStack(&stack, part3);
createStack(&stack, part4);
// พิมพ์คำที่ได้จาก stack
printFromStack(&stack);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKLy8g4LmC4LiE4Lij4LiH4Liq4Lij4LmJ4Liy4LiH4LiC4Lit4LiHIHN0YWNrCnR5cGVkZWYgc3RydWN0IFN0YWNrIHsKICAgIGNoYXIgKmRhdGE7CiAgICBzdHJ1Y3QgU3RhY2sgKm5leHQ7Cn0gU3RhY2s7CgovLyDguJ/guLHguIfguIHguYzguIrguLHguJkgcHVzaCDguJXguLHguKfguK3guLHguIHguKnguKPguYDguILguYnguLLguYTguJvguYPguJkgc3RhY2sKdm9pZCBwdXNoKFN0YWNrICoqdG9wLCBjaGFyIGMpIHsKICA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