So anything in the stack that goes last, comes out first.
To better understand the concept, think of the stack or pile of books.
In order to pick a book from the end of the stack, you may need to remove books from the top all the way to the bottom.
Similarly, the last book in the stack was the first to become part of that stack whereas the book at the top was the last to go in.
I think that’s simple enough to understand.
Example of Stacking in Computers
Just to fully grasp the concept let’s review an example.
Have you ever pressed CTRL+Z while working on any MS-Word or MS-Excel or even simple text file?
What happens when you do it?
Your last activity is simply undone and everything returns to a state as they were before the last action you performed.
How does this happen? Simple, all the programs mentioned above are stacking the information of the events caused by you.
As soon as you hit CTRL+Z, the last action is undone and the event which was in the list before your last event is displayed.
Following are the basic functions available to access/alter/edit or even retrieve information from the stack.
push(): for “pushing” or placing data at the end (or at the very top) of the stack
pop(): for removing the top most data (or last piece of information) from the stack
shift(): for removing the bottom most element (or very first piece of information) from top of the stack
size(): to determine the number of elements (or the size) of the stack.
What is a Palindrome
A Palindrome is a word or phrase, which spells the same (ignoring spaces) from right to left or left to right or backward and forward.
For e.g. the word, BOB is a palindrome as does not matter you start reading the word from right to left or left to right.
Similarly, if we were to talk about a complete phrase, “Never odd or even” can be a good example of a palindrome.
Remember, the spaces don’t matter.
- Take the word from the user in a variable
- Convert it into an array (or stack it)
- Now loop through the stack and pop every letter from the array to the new variable.
- Once done, compare the new variable with the variable in point 1 and then display the message based on the result.
That’s our simple logic. Now, let’s implement it.
Note that the above is a very basic program of figuring out palindrome as it does not strip and spaces, numeric characters and/or punctuation and the reason behind it is that writing an impeccable program to identify palindrome was never the objective of the article.
However, I would encourage you to go ahead and write your own and much more “robust” program and see how much water are you in.
Once done, do share your experience in the comment section.