Вы находитесь на странице: 1из 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<l_str.length(); l_ndx++) { String l_char = l_str.substring(l_ndx, l_ndx+1); if (l_char.equalsIgnoreCase("*")) { // Pop() System.out.print(l_c.Get_Top() + " "); l_c.Pop(); } else { // Push the letter into the stack l_c.Push(l_char); } } //for (...

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_ndx<l_str.length(); l_ndx++) l_c.Push(l_str.substring(l_ndx, l_ndx+1)); // then while the stack is not empty, read the top then pop while (l_c.Get_Size()>0) { System.out.print(l_c.Get_Top() + " "); l_c.Pop(); } //while (...

04 LAB A Implementing Stacks & Queues - Answers

Page 1 of 2

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_ndx<l_str.length(); l_ndx++) l_c.Push(l_str.substring(l_ndx, l_ndx+1)); // read the top then convert, then pop // note: popping moves from the least to the most significant bit int l_factor, l_decimal=0; for (int l_exp=0; l_c.Get_Size()>0; 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);

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<l_str.length(); l_ndx++) { String l_char = l_str.substring(l_ndx, l_ndx+1); if (l_char.equalsIgnoreCase("*")) { System.out.print(l_c.Get_Next() + " "); l_c.DeQueue(); } else { l_c.EnQueue(l_char); } } //for (...

04 LAB A Implementing Stacks & Queues - Answers

Page 2 of 2

Вам также может понравиться