Homework 3

advertisement
CSC 1052 Homework Stacks Short Answers Student Version
1. True or False:
(a) ___A stack is a first in first out structure.
(b) ___The item that has been in a stack the longest is at the "bottom" of
the stack.
(c) ___If you push 5 items onto an empty stack, and then pop the stack 5
times, the stack will be empty again.
(d) ___If you push 5 items onto an empty stack, and then perform the top
operation 5 times, the stack will be empty again.
(e) ___The push operation should be classified as a "transformer".
(f) ___The top operation should be classified as a "transformer".
(g) ___The pop operation should be classified as an "observer".
(h) ___If we first push itemA onto a stack, and then push itemB, then the
top of the stack is itemB.
2. I am going to execute this code with THREE pushes and ONE pop using an array:
Stack s = new Stack( );
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop( ));
Suppose that s is represented by a partially filled array. Draw the state of the private
instance variables of s after the above code. Place Top in its proper position on the
array:
_______________________________
Top = -1
|
|
|
|
|
|
|_ ____ |_____ |______|_____|______|...
[0] [1]
[2]
[3] [4]
3. Assume myStack is an object of Class ArrayStack. You may call any of the public
methods of ArrayStack. Write Pseudocode for the following
a. Set bottom equal to the bottom element in myStack , leaving myStack without its
original top two elements. Use methods such as mystack.push() and mystack.pop()
You need to create a temp stack to store the stack while is emptied so you can rebuild it
again.
ArrayStack tempStack = new ArrayStack();
// Loop- you will need to have another stack to hold the popped items so you can
restore the stack
1
b. Make a copy of myStack, leaving myStack unchanged. Use methods such as
mystack.push() and mystack.pop() and isEmpty() - Similar to above
While(!myStack().isEmpty() )
…..
4. Consider the usual algorithm to convert an infix expression to a postfix expression.
Suppose that you have read 10 input characters during a conversion and that the stack
now contains these symbols:
|
|
| + |
| ( |
bottom |__*__|
Now, suppose that you read and process the 11th symbol of the input. Draw the stack
for the case where the 11th symbol is:
A ) A number:
B) A left parenthesis:
C) A minus sign:
5. Describe the state of an initially empty stack after each of the following sequences of
operations. Indicate the values held by any variables that are declared; also indicate
any errors that may occur You may assume stk represents a stack of integer
objects.
a)
stk.push(new Integer(3));
Object a = stk.pop();
stk.push();
stk.pop();
b.
stk.push(new Integer(3));
stk.push(new Integer(4));
stk.pop();
stk.pop();
Object a = stk.pop();
6. Change the following expression into Polish notation(Postfix) and show your work :
(a *(b+ c)) + (b/d) * a
7. One operation that we might want to implement on a Stack is isfull(), which determines
if the data structure has room for another item to be added. This operation would be
useful
a. only if the Stack is implemented using an array
b. only if the Stack is implemented using a linked list
c. only for a vector – a type of array
d. only for a linked list
e. none of the above, a full operation is not useful at all
2
8. In order to gain a last-in first-out access to data, which type of structure should be used?
a) Vector – a type of array
b) Array
c) Linked List
d) Queue
e) Stack
For questions 9-10 assume a Stack class stores int values. Consider the following sequence
of instructions.
Stack s = new Stack( );
s.push(16);
s.push(12);
s.push(19);
int x = s.pop( );
s.push(5);
s.push(9);
s.push(4);
int y = s.pop( );
int z = s.pop( );
9) After the instructions execute, x has the value
a) 16
b) 12
c) 19
d) 0
e) none of the above, the instruction int x = s.pop( ) results in an Exception being
thrown
10) After the instructions execute, z has the value
a) 4
b) 9
c) 5
d) 12
e) 16
11. A Stack s stores int values. Show what s will look like after each of the following
instructions is executed.
s.push(5);
s.push(1);
s.push(4);
s.push(3);
s.pop( );
s.push(2);
s.pop( );
s.pop( );
s.push(8);
s.push(7);
s.pop( );
3
s.push(3);
12. Fill in the execution time for each operation:
Operation
Array
LinkedList
Push();
O( )
O( )
Pop();
O( )
O( )
Peek();
O( )
O( )
Multiple Choice
1..Entries in a stack are "ordered". What is the meaning of this statement?
A. A collection of Stacks can be sorted.
B. Stack entries may be compared with the '<' operation.
C. The entries must be stored in a linked list.
D. There is a first entry, a second entry, and so on.
2. The operation for adding an entry to a stack is traditionally called:
A. add
B. append
C. insert
D. push
3. The operation for removing an entry from a stack is traditionally called:
A. delete
B. peek
C. pop
D. remove
4. Which of the following stack operations could result in stack underflow?
A. is_empty
B. pop
C. push
D. Two or more of the above answers
5. Which of the following applications may use a stack?
A. A parentheses balancing program.
B. Keeping track of local variables at run time.
C. Syntax analyzer for a compiler.
D. All of the above.
6. Consider the following pseudocode:
declare a stack of characters
while ( there are more characters in the word to read )
{
read a character
push the character on the stack
}
while ( the stack is not empty )
{
4
pop a character off the stack
write the character to the screen
}
What is written to the screen for the input "carpets"?
A. serc
B. carpets
C. steprac
D. ccaarrppeettss
7. Here is an INCORRECT pseudocode for the algorithm which is supposed to
determine whether a sequence of parentheses is balanced:
declare a character stack
while ( more input is available)
{
read a character
if ( the character is a '(' )
push it on the stack
else if ( the character is a ')' and the stack is not empty )
pop a character off the stack
else
print "unbalanced" and exit
}
print "balanced"
Which of these unbalanced sequences does the above code think is balanced?
A. ((())
B. ())(()
C. (()()))
D. (()))()
8. Consider the usual algorithm for determining whether a sequence of parentheses is
balanced. Suppose that you run the algorithm on a sequence that contains 2 left
parentheses and 3 right parentheses (in some order). What is the maximum number of
parentheses that will ever appear on the stack AT ONE TIME during the computation?
A. 1
B. 2
C. 3
D. 4
E. 5 or more
9. Suppose we have an array implementation of the stack class, with items in the stack
stored at data[0] through data[9]. The Size(number of items in the array) is __
10. If the capacity of the array is 11, where does the push method place the new entry in
the array?
A ) data[0]
B) data[1]
C)data[9]
D) data[10]
E) None of the above
5
11. Consider the implementation of the Stack using a partially-filled array. What goes
wrong if we try to store the top of the Stack at location [0] and the bottom of the Stack
at the last used position of the array?
A. Both peek and pop would require linear time.
B. Both push and pop would require linear time.
C. The Stack could not be used to check balanced parentheses.
D. The Stack could not be used to evaluate postfix expressions.
12. In the linked list implementation of the stack class, where does the push method
place the new entry on the linked list?
A. At the head
B. At the tail
C. After all other entries that are greater than the new entry.
D. After all other entries that are smaller than the new entry.
13 In the array version of the Stack class, which operations require linear time for their
worst-case behavior?
A. is_empty
B. peek
C. pop
D. push when the stack is below capacity
E. None of these operations require linear time.
14. In the linked-list version of the Stack class, which operations require linear time for
their worst-case behavior?
A. is_empty
B. peek
C. pop
D. push
E. None of these operations require linear time.
15. What is the value of the postfix expression 6 3 2 4 + - *:
A. Something between -15 and -100
B. Something between -5 and -15
C. Something between 5 and -5
D. Something between 5 and 15
E. Something between 15 and 100
16. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual Stack
algorithm to convert the expression from infix to postfix notation. What is the
maximum number of symbols that will appear on the stack AT ONE TIME during the
conversion of this expression?
A. 1
B. 2
C. 3
D. 4
E. 5
6
17. Show what is written by the following segments of code, given that item1,
item2, and item3 are int variables, and stack is an object that fits the abstract
description of a stack as given in the section. Assume that you can store and retrieve
variables of type int on stack.
a)
item1 = 1;
item2 = 0;
item3 = 4;
stack.push(item2);
stack.push(item1);
stack.push(item1 + item3);
item2 = stack.top();
stack.push (item3*item3);
stack.push(item2);
stack.push(3);
item1 = stack.top();
stack.pop();
System.out.println(item1 + " " + item2 + " " + item3);
while (!stack.isEmpty())
{
item1 = stack.top();
stack.pop();
System.out.println(item1);
}
b)
item1 = 4;
item3 = 0;
item2 = item1 + 1;
stack.push(item2);
stack.push(item2 + 1);
stack.push(item1);
item2 = stack.top();
stack.pop();
item1 = item2 + 1;
stack.push(item1);
stack.push(item3);
while (!stack.isEmpty())
{
item3 = stack.top();
stack.pop();
System.out.println(item1);
}
System.out.println(item1 + " " + item2 + " " + item3);
7
Download