fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4. #include <cctype> // for tolower
  5. #include <string>
  6. using namespace std;
  7.  
  8. #define SIZE 5
  9.  
  10. // -------------------------
  11. // Circular Queue Class
  12. // -------------------------
  13. class CircularQueue {
  14. private:
  15. int items[SIZE], front, rear;
  16.  
  17. public:
  18. CircularQueue() {
  19. front = -1;
  20. rear = -1;
  21. }
  22.  
  23. bool isFull() {
  24. return (front == 0 && rear == SIZE - 1) || (rear == (front - 1 + SIZE) % SIZE);
  25. }
  26.  
  27. bool isEmpty() {
  28. return front == -1;
  29. }
  30.  
  31. void enqueue(int value) {
  32. if (isFull()) {
  33. cout << "Queue is Full\n";
  34. return;
  35. }
  36.  
  37. if (front == -1) {
  38. front = rear = 0;
  39. } else if (rear == SIZE - 1 && front != 0) {
  40. rear = 0;
  41. } else {
  42. rear++;
  43. }
  44.  
  45. items[rear] = value;
  46. }
  47.  
  48. void dequeue() {
  49. if (isEmpty()) {
  50. cout << "Queue is Empty\n";
  51. return;
  52. }
  53.  
  54. cout << "Removed element: " << items[front] << endl;
  55.  
  56. if (front == rear) {
  57. front = -1;
  58. rear = -1;
  59. } else if (front == SIZE - 1) {
  60. front = 0;
  61. } else {
  62. front++;
  63. }
  64. }
  65.  
  66. void display() {
  67. if (isEmpty()) {
  68. cout << "Queue is Empty\n";
  69. return;
  70. }
  71.  
  72. cout << "Queue elements: ";
  73. if (rear >= front) {
  74. for (int i = front; i <= rear; i++)
  75. cout << items[i] << " ";
  76. } else {
  77. for (int i = front; i < SIZE; i++)
  78. cout << items[i] << " ";
  79. for (int i = 0; i <= rear; i++)
  80. cout << items[i] << " ";
  81. }
  82. cout << endl;
  83. }
  84. };
  85.  
  86. // -------------------------
  87. // Palindrome Checker Function
  88. // -------------------------
  89. void checkPalindrome(const string& input) {
  90. // Reject input with spaces
  91. if (input.find(' ') != string::npos) {
  92. cout << "Please enter only one word without spaces.\n";
  93. return;
  94. }
  95.  
  96. stack<char> s;
  97. queue<char> q;
  98.  
  99. // Insert characters into stack and queue
  100. for (char c : input) {
  101. char lower = tolower(c); // تحويل الحرف إلى صغير
  102. s.push(lower);
  103. q.push(lower);
  104. }
  105.  
  106. bool isPalindrome = true;
  107. while (!s.empty()) {
  108. if (s.top() != q.front()) {
  109. isPalindrome = false;
  110. break; // إذا حدث اختلاف، لا داعي لاستكمال المقارنة
  111. }
  112. s.pop();
  113. q.pop();
  114. }
  115.  
  116. if (isPalindrome)
  117. cout << "Palindrome\n";
  118. else
  119. cout << "Not palindrome\n";
  120. }
  121.  
  122. // -------------------------
  123. // Main
  124. // -------------------------
  125. int main() {
  126. // ------------------------
  127. // Test Circular Queue
  128. // ------------------------
  129. CircularQueue cq;
  130. cq.enqueue(10);
  131. cq.enqueue(20);
  132. cq.enqueue(30);
  133. cq.enqueue(40);
  134. cq.enqueue(50);
  135. cq.display();
  136. cq.dequeue();
  137. cq.enqueue(60);
  138. cq.display();
  139.  
  140. // ------------------------
  141. // Palindrome Check
  142. // ------------------------
  143. string word;
  144. cout << "\nEnter a word to check if it's a palindrome: ";
  145. cin.ignore(); // مهم علشان نتفادى مشكلة getline بعد cin
  146. getline(cin, word);
  147.  
  148. checkPalindrome(word);
  149.  
  150. return 0;
  151. }
  152.  
Success #stdin #stdout 0.01s 5288KB
stdin
saas
stdout
Queue elements: 10 20 30 40 50 
Removed element: 10
Queue elements: 20 30 40 50 60 

Enter a word to check if it's a palindrome: Not palindrome