I am trying to write an algorithm/pseudo-code to check if a given array A is a palindrome using Stack.
Our approach would be to: - “push” elements of array a [left….right] onto a newly created stack, such that the element on the left is pushed first. Thus, the array elements are reversed such that the last element of the array is at the top of the stack.
Thereafter, we “pop” each element from the Stack onto a newly created array b. This means the last element of the original array a becomes the first element of new array b.
Finally, we compare each element of a and b and if they are equal, then the original array a is a palindrome
Algorithm Steps:
Make an empty stack S
Set l to left and set r to right (of array a)
For each element in array a [l…r], repeat: 3.1. Add element to the top of stack S
Create a new empty array b such that b [left…..right]
While stack S is not empty, repeat: 5.1 Remove each element from the top of S to array b
Set l to left and r to right for both arrays a and b. 6.1 If a [left….right] ≠ b [left….right]; terminate with answer false 6.2 Else a [left…right] = b [left…..right];
Terminate with answer isPalindrome.
Pheww !!!!