/////////////////////////////////////////////////////////////////////////////////////////////// // // // CSE 428 - SPRING 2001 // // // // Solution of Assignment #3 // // // // file S3.C // // // /////////////////////////////////////////////////////////////////////////////////////////////// // #include "A3_code_stack.C" // Original code provided for the assignment #include "S3_test.C" // Code for testing the solutions of the students /////////////////////////////////////////////////////////////////////////////////////////////// stack1::~stack1() { while (top != NULL) { pop(); } } void stack1::pop() { element* tmp = top; if (top != NULL) { top = top->next; delete tmp; } else cout << "Error: pop() on an empty stack"; } int stack1::position(int x) { element* tmp = top; bool found = false; int pos = 0; for(; tmp != NULL && !found; pos++) { found = (tmp->info == x); tmp = tmp->next; } if (found) return pos; else return 0; } /////////////////////////////////////////////////////////////////////////////////////////////// void stack2::pop() { stack2* tmp = next; if (tmp != NULL) { next = next->next; tmp->next = NULL; //Necessary to avoid recursive calls of the stack2 destructor delete tmp; } else cout << "Error: pop() on an empty stack"; } int stack2::position(int x) { stack2* tmp = next; bool found = false; int pos = 0; for(; tmp != NULL && !found; pos++) { found = (tmp->info == x); tmp = tmp->next; } if (found) return pos; else return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////