Thursday, March 11, 2010

a question about stack

in class we learned how S.pop() delete data in stack. My question is that whether we should delete data in reverse order?

S.push(Student("Fardad", "oop344", 56));

S.push(Student("John", "int322", 100));
S.push(Student("Jack", "int422", 90));
S.pop();
S.pop();
while(!S.IsEmpty()){
cout< }
Student StdStack::pop(){
Student ret = top->data;
Node* ToDel = top;
top = top->next;
delete ToDel;
return ret;
}
the output is Fardad, oop344: 56

i read some material in btp300
Element pop() {
Element data;
if (head) {
Node* p = head;
data = head->data;
head = head->next;
delete p;
}
return data;
}

Stack s;

// Push Data Onto Stack
s.push(3);
s.push(5);
s.push(9);
s.push(8);

// Remove first Node
s.pop();

// Pop Data Off Stack
while (!s.empty())
cout << s.pop().out() << ' ';
cout << endl;

the output is 9 5 3

could any one explain the difference between this two example why the pop() are different?