fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define STACK_INIT_SIZE 100
  6. #define QUEUE_INIT_SIZE 100
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define OK 1
  10. #define ERROR 0
  11. #define OVERFLOW -1
  12.  
  13. typedef int Status;
  14.  
  15. // Stack definition
  16. typedef struct {
  17. char *base;
  18. char *top;
  19. int stacksize;
  20. } SqStack;
  21.  
  22. // Queue definition
  23. typedef struct {
  24. char *data;
  25. int front;
  26. int rear;
  27. int queuesize;
  28. } SqQueue;
  29.  
  30. // Stack operations
  31. Status InitStack(SqStack *S) {
  32. S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
  33. if (!S->base) exit(OVERFLOW);
  34. S->top = S->base;
  35. S->stacksize = STACK_INIT_SIZE;
  36. return OK;
  37. }
  38.  
  39. Status DestroyStack(SqStack *S) {
  40. free(S->base);
  41. S->base = S->top = NULL;
  42. S->stacksize = 0;
  43. return OK;
  44. }
  45.  
  46. Status ClearStack(SqStack *S) {
  47. S->top = S->base;
  48. return OK;
  49. }
  50.  
  51. Status StackEmpty(SqStack S) {
  52. return S.top == S.base ? TRUE : FALSE;
  53. }
  54.  
  55. int StackLength(SqStack S) {
  56. return (int)(S.top - S.base);
  57. }
  58.  
  59. Status GetTop(SqStack S, char *e) {
  60. if (S.top == S.base) return ERROR;
  61. *e = *(S.top - 1);
  62. return OK;
  63. }
  64.  
  65. Status Push(SqStack *S, char e) {
  66. if (S->top - S->base >= S->stacksize) return ERROR;
  67. *S->top++ = e;
  68. return OK;
  69. }
  70.  
  71. Status Pop(SqStack *S, char *e) {
  72. if (S->top == S->base) return ERROR;
  73. *e = *--S->top;
  74. return OK;
  75. }
  76.  
  77. Status StackTraverse(SqStack S, void (*visit)(char)) {
  78. char *p = S.top;
  79. while (p > S.base) {
  80. visit(*--p);
  81. }
  82. return OK;
  83. }
  84.  
  85. // Queue operations
  86. Status InitQueue(SqQueue *Q) {
  87. Q->data = (char *)malloc(QUEUE_INIT_SIZE * sizeof(char));
  88. if (!Q->data) exit(OVERFLOW);
  89. Q->front = Q->rear = 0;
  90. Q->queuesize = QUEUE_INIT_SIZE;
  91. return OK;
  92. }
  93.  
  94. Status DestroyQueue(SqQueue *Q) {
  95. free(Q->data);
  96. Q->data = NULL;
  97. Q->front = Q->rear = 0;
  98. Q->queuesize = 0;
  99. return OK;
  100. }
  101.  
  102. Status ClearQueue(SqQueue *Q) {
  103. Q->front = Q->rear = 0;
  104. return OK;
  105. }
  106.  
  107. Status QueueEmpty(SqQueue Q) {
  108. return Q.front == Q.rear ? TRUE : FALSE;
  109. }
  110.  
  111. int QueueLength(SqQueue Q) {
  112. return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
  113. }
  114.  
  115. Status GetHead(SqQueue Q, char *e) {
  116. if (Q.front == Q.rear) return ERROR;
  117. *e = Q.data[Q.front];
  118. return OK;
  119. }
  120.  
  121. Status EnQueue(SqQueue *Q, char e) {
  122. if ((Q->rear + 1) % Q->queuesize == Q->front) return ERROR;
  123. Q->data[Q->rear] = e;
  124. Q->rear = (Q->rear + 1) % Q->queuesize;
  125. return OK;
  126. }
  127.  
  128. Status DeQueue(SqQueue *Q, char *e) {
  129. if (Q->front == Q->rear) return ERROR;
  130. *e = Q->data[Q->front];
  131. Q->front = (Q->front + 1) % Q->queuesize;
  132. return OK;
  133. }
  134.  
  135. Status QueueTraverse(SqQueue Q, void (*visit)(char)) {
  136. int i = Q.front;
  137. while (i != Q.rear) {
  138. visit(Q.data[i]);
  139. i = (i + 1) % Q.queuesize;
  140. }
  141. return OK;
  142. }
  143.  
  144. // Helper to print character
  145. void visit(char c) {
  146. printf("%c ", c);
  147. }
  148.  
  149. // Main program
  150. int main() {
  151. SqStack S;
  152. SqQueue Q;
  153. char ch, e1, e2;
  154. int isPalindrome = 1;
  155.  
  156. InitStack(&S);
  157. InitQueue(&Q);
  158.  
  159. printf("Enter a string (press Enter to finish): ");
  160. while ((ch = getchar()) != '\n' && ch != EOF) {
  161. Push(&S, ch);
  162. EnQueue(&Q, ch);
  163. }
  164.  
  165. printf("\nStack contents: ");
  166. StackTraverse(S, visit);
  167.  
  168. printf("\nQueue contents: ");
  169. QueueTraverse(Q, visit);
  170. printf("\n");
  171.  
  172. if (StackLength(S) != QueueLength(Q)) {
  173. printf("Length mismatch\n");
  174. isPalindrome = 0;
  175. } else {
  176. while (!StackEmpty(S) && !QueueEmpty(Q)) {
  177. GetTop(S, &e1);
  178. GetHead(Q, &e2);
  179. if (e1 != e2) {
  180. isPalindrome = 0;
  181. break;
  182. }
  183. Pop(&S, &e1);
  184. DeQueue(&Q, &e2);
  185. }
  186. }
  187.  
  188. printf(isPalindrome ? "\nIs Palindrome\n" : "\nNOT Palindrome\n");
  189.  
  190. ClearStack(&S);
  191. ClearQueue(&Q);
  192. DestroyStack(&S);
  193. DestroyQueue(&Q);
  194.  
  195. return 0;
  196. }
  197.  
Success #stdin #stdout 0.01s 5288KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
Enter a string (press Enter to finish): 
Stack contents: 0 1 
Queue contents: 1 0 

NOT Palindrome