Exercises 4.2
E2 Consider a linked stack that includes a method size .That method size requires a loop that moves through the entire stack to count the entires,since the number of entries in the stack is not kept as a separate member in the stack record.
(a)Write a method size for a linked stack by using a loop that moves a pointer variable from node to node to mode to node through the stack.
int Stack ::size()const
/Post: Return the number of entries in the Stack.*/
{
Node *temp=top_node;
int count=0;
while{temp!=NULL){
temp=temp->next;
Count ++;
}
Return count;
}
Exercise 4.3 E2
Void Stack::operator=(const Stack &original)
/Post:The Stack is reset as a copy of Stack original.*/
{
Stack new_copy(original);
Node *temp=top_node;
top_node=new_copy.top_node;
new_copy.top_node=temp;
}