Está en la página 1de 2

Last Name:

First:

This Exercise implements the Stacks and Queues concepts learned. Create all methods inside main().

 1 Implementing Stacks: A string contains a sequence of letters and asterisks. A letter means Push() the letter into a stack and and an asterisk means Pop() the stack. Given the string "DIS*T*RUC***IS***FU*N***": (a) Give the sequence of values returned by the Pop() operations when this sequence of operations is performed on an initially empty stack. And (b) Then write the code to check your answer. ANS (a) "S T C U R S I I U N F D" (b) String l_str = "DIS*T*RUC***IS***FU*N***"; C_SQ_Stack l_c = new C_SQ_Stack(); for (int l_ndx=0; l_ndx
 2 Implementing Stacks: Using only a stack, write a program that will output an input String in reverse order. For example, if "ABCD" is the input String, then the output should be "DCBA". ANS String l_str = "ABCD"; C_SQ_Stack l_c = new C_SQ_Stack(); // Push the whole string into the stack for (int l_ndx=0; l_ndx0) { System.out.print(l_c.Get_Top() + " "); l_c.Pop(); } //while (
 3 Implementing Stacks: A String contains a binary number from the most significant to the least significant bit. Using only a stack, write a program that will read calculate the decimal equivalent of the binary number. For example, if the bit String is "1100" the decimal equivalent is 12. ANS String l_str = "11100"; C_SQ_Stack l_c = new C_SQ_Stack(); // push the bits into the stack for (int l_ndx=0; l_ndx0; l_exp++) { l_factor = Integer.parseInt(l_c.Get_Top()); l_decimal += l_factor*Math.pow(2, l_exp); l_c.Pop(); } // for( System.out.println(l_str + " equals decimal " + l_decimal);
 4 Implementing Queues: A string contains a sequence of letters and asterisks. A letter means EnQueue() the letter and and an asterisk means DeQueue(). Given the string "DIS*T*RUC***IS***FU*N***": (a) Give the sequence of values returned by the DeQueue() operations when this sequence of operations is performed on an initially empty queue. Use Get_Next() to retrieve the next character in the Queue. And (b) Then write the code to check your answer. ANS (a) "D I S T R U C I S F U N" (b) String l_str = "DIS*T*RUC***IS***FU*N***"; C_SQ_Queue l_c = new C_SQ_Queue(); for (int l_ndx=0; l_ndx