CS210
Tutorial 3
1. Assume that Stack is the class described in section 7.2 of your text with StackType set to char and STACK_CAPACITY or myCapacity set to 5. Give the value of myTop and the contents of the array myArray in the stack s after the code segment is executed or indicate why an error has occurred:
Stack s;
char x, y,z ;
x = ‘D’;
y = ‘B’;
z = ‘C’;
s.push(x);
s.push(y);
s.push(‘y’);
x=s.top();
s.pop();
y=s.top();
s.push (z);
s. push(x);
cout << x << endl << y << endl<< z << endl;
while (! s.empty( )){
y = s.top();
cout << y << endl;
}
s.push(y);
s.push(z);
2. Write documentation, a prototype, and a definition for a member function bottom() for a Stack class that returns the bottom element of the stack. Assume static-array implementation.
3. Repeat exercise 2, but for a function bottom() that is neither a member function nor a friend function of the class Stack.
4. Assume the following declarations (which are used to process
singly-linked lists as described in this section):
class Node{
public:
int data;
Node *next;
};
Node * p1, *p2, *p3;
Tell what is displayed after the following code segment:
p1 = new (nothrow) Node;
p2 = new (nothrow) Node;
p3 = new (nothrow)Node;
p1->data = 12;
p2->data = 34;
p3->data = 56;
p1->next = p2;
p2->next = p3;
p3->next = 0;
cout <<p1->data <<" " <<p1->next->data <<endl;
cout<<p2->data <<" " <<p2->next->data<<endl;
cout<<p3->data<<endl;
p2->next = p1;
cout<<p1->next->next->data<<endl;
p1=p2;
cout<<p1->data<<" " <<p2->data<<endl;
5. For the List class, add a Boolean-valued function that determines whether the data items in the linked list are arranged in ascending order.