In: Computer Science
1.Read the Lab6-Background.pdf first.2.Start the “Stack Queue”
applet.3.The purpose of this exercise is to see how you could
reverse a list of names. Push the three names “Alice,” “Bob,” and
“Charles” onto the stack. When you pop the stack 3 times, what do
you see in the text field next to “Pop”?4.Write an algorithm in
pseudo-codethat would describe how to use a stack to reversea
sequence (list)of letters and print them. For instance, if the
original sequence is COMPUTERthe output will be RETUPMOC. Review
pages 249-250 of the textbook(Chapter8)to see the related sample
pseudo-codes.5.A palindrome word is one that has the same sequence
of letters if you read it from right to left or left to right(e.g.,
radar or madam). Describe (in terms of a pseudo-code) how you could
use a stack in conjunction with the original listto determine if a
sequence is a palindrome.Hint:Given that the letters have been
already assigned to both stackand the queueand these two data
structures are ready to be processed (compared).
Since, a stack works on LIFO(Last In First Out) so when you push the three names onto the stack, the top refers to the index where "Charles" is stored. On calling Pop(), the value of last top is removed and the new top becomes "Bob". On second Pop(), the value of last top is removed and the new top becomes "Alice". After the third call for Pop(), since the stack has only one item left, it deletes the last item and the stack becomes null or empty.
SEQUENCE REVERSAL
function Reverse(Argument One)
create a new stack equal to the capacity of the string
for counter from 0 to length of string
push the character at counter index onto the stack
for counter from 0 to length of string
pop the top of stack and replace it with the character at counter position in the string
now print the string (i.e. reversed string)
CHECK FOR PALINDROME
function isPalindrome(Argument One)
find the length of the string
calculate the variable mid by dividing length by 2
create a new stack
for counter1 from 0 to length of string
if counter1 < mid
push the character at counter1 index onto the stack
if length%2 is not equal 0
neglect mid
if counter1 from mid to length - 1
pop the character from stack and compare with the character at counter1 index
if same then continue else return false