Thursday, October 22, 2009

Challenge question: DsStack copy constructor


Stack::Stack(const Stack& stack) {
SNode *p = stack.top;
top = (SNode*)0;

// get a reversed stack
while(p) {
Push(p->data);
p = p->next;
}

// reverse it again
p = top;
top = (SNode*)0;
SNode* del;

while(p) {
Push(p->data);
del = p;
p = p->next;
delete del;
}
}

No comments:

Post a Comment