#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define QUEUE_INIT_SIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
// Stack definition
typedef struct {
char *base;
char *top;
int stacksize;
} SqStack;
// Queue definition
typedef struct {
char *data;
int front;
int rear;
int queuesize;
} SqQueue;
// Stack operations
Status InitStack(SqStack *S) {
S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status DestroyStack(SqStack *S) {
free(S->base);
S->base = S->top = NULL;
S->stacksize = 0;
return OK;
}
Status ClearStack(SqStack *S) {
S->top = S->base;
return OK;
}
Status StackEmpty(SqStack S) {
return S.top == S.base ? TRUE : FALSE;
}
int StackLength(SqStack S) {
return (int)(S.top - S.base);
}
Status GetTop(SqStack S, char *e) {
if (S.top == S.base) return ERROR;
*e = *(S.top - 1);
return OK;
}
Status Push(SqStack *S, char e) {
if (S->top - S->base >= S->stacksize) return ERROR;
*S->top++ = e;
return OK;
}
Status Pop(SqStack *S, char *e) {
if (S->top == S->base) return ERROR;
*e = *--S->top;
return OK;
}
Status StackTraverse(SqStack S, void (*visit)(char)) {
char *p = S.top;
while (p > S.base) {
visit(*--p);
}
return OK;
}
// Queue operations
Status InitQueue(SqQueue *Q) {
Q->data = (char *)malloc(QUEUE_INIT_SIZE * sizeof(char));
if (!Q->data) exit(OVERFLOW);
Q->front = Q->rear = 0;
Q->queuesize = QUEUE_INIT_SIZE;
return OK;
}
Status DestroyQueue(SqQueue *Q) {
free(Q->data);
Q->data = NULL;
Q->front = Q->rear = 0;
Q->queuesize = 0;
return OK;
}
Status ClearQueue(SqQueue *Q) {
Q->front = Q->rear = 0;
return OK;
}
Status QueueEmpty(SqQueue Q) {
return Q.front == Q.rear ? TRUE : FALSE;
}
int QueueLength(SqQueue Q) {
return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
}
Status GetHead(SqQueue Q, char *e) {
if (Q.front == Q.rear) return ERROR;
*e = Q.data[Q.front];
return OK;
}
Status EnQueue(SqQueue *Q, char e) {
if ((Q->rear + 1) % Q->queuesize == Q->front) return ERROR;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % Q->queuesize;
return OK;
}
Status DeQueue(SqQueue *Q, char *e) {
if (Q->front == Q->rear) return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % Q->queuesize;
return OK;
}
Status QueueTraverse(SqQueue Q, void (*visit)(char)) {
int i = Q.front;
while (i != Q.rear) {
visit(Q.data[i]);
i = (i + 1) % Q.queuesize;
}
return OK;
}
// Helper to print character
void visit(char c) {
printf("%c ", c);
}
// Main program
int main() {
SqStack S;
SqQueue Q;
char ch, e1, e2;
int isPalindrome = 1;
InitStack(&S);
InitQueue(&Q);
printf("Enter a string (press Enter to finish): ");
while ((ch = getchar()) != '\n' && ch != EOF) {
Push(&S, ch);
EnQueue(&Q, ch);
}
printf("\nStack contents: ");
StackTraverse(S, visit);
printf("\nQueue contents: ");
QueueTraverse(Q, visit);
printf("\n");
if (StackLength(S) != QueueLength(Q)) {
printf("Length mismatch\n");
isPalindrome = 0;
} else {
while (!StackEmpty(S) && !QueueEmpty(Q)) {
GetTop(S, &e1);
GetHead(Q, &e2);
if (e1 != e2) {
isPalindrome = 0;
break;
}
Pop(&S, &e1);
DeQueue(&Q, &e2);
}
}
printf(isPalindrome ? "\nIs Palindrome\n" : "\nNOT Palindrome\n");
ClearStack(&S);
ClearQueue(&Q);
DestroyStack(&S);
DestroyQueue(&Q);
return 0;
}