The purpose of this assignment is to provide experience with memory management, and also with the implementation of the data structure "stack". We have noted that in previous assignment several students used a double linked list to implement the environment, while a simple linked list would have been sufficient. The first exercise of this assignment is about two alternative definitions of stack using simple lists. The environment in previous assignment could have been implemented in the same way. (The function lookup of previous assignment is very similar to the function "position" in this assignment.)
stack1::~stack1(); //destructor: it should delete all the elements of the stack, thus avoiding memory leaks void stack1::pop(); //it should eliminate the element on top and avoid memory leaks int stack1::position(int); //see the code for the description of this function void stack2::pop(); //it should eliminate the element on top and avoid memory leaks int stack2::position(int); //see the code for the description of this functionAll the other methods relevant for this exercise (including the destructor for stack2) are defined in the given code. Please make sure that your methods (in particular, pop() and the destructor of stack1) don't leave memory leaks, namely garbage. Also, please make sure that they do not produce dangling references.
Download this solution frame and fill it in with your definition. Save it with name XXXX.C, where XXXX are the last 4 digits of your student id. Compile XXXX.C with the Unix GNU C++ compiler g++, i.e. give the Unix command
g++ XXXX.CNote that XXXX.C contains an instruction to include the file A3_code_stack.C (which should be saved in the same directory). Hence it will include our function main() and the rest of the stacks definitions.
Now run the compiled code by giving the command
a.outIf your definitions are correct, you should get the following output:
Stack1 created. Pushing the numbers 5, 5, 6 and 7 Testing the presence of number 5 5 is in position 3 Popping one element from the stack1 Testing the presence of number 5 5 is in position 2 Stack2 created. Pushing the numbers 5, 6 and 6 Testing the presence of number 6 6 is in position 1 Popping an element from the stack2 Pushing the numbers 7, 8 and 9 Testing the presence of number 6 6 is in position 4 Testing the presence of memory leaks element count: 0 stack1 count: 0 stack2 count: 0 No memory leaks. Great!If this happens, then you can be reasonably confident that your program is correct. Please keep it in mind however that we will run your solution under a different main() function (with additional test cases) to make sure that your solution is correct in general, not just with the test cases illustrated above.
Please note the following:.
void p(){ stack2* s = new stack2(); s->push(2); cout << s->position(2); } void main() { ... p(); ...}
stack2* s = new stack2(); // global variable void p(){ stack2* t = s; t->push(2); cout << s->position(2); delete t; } void main() { p(); ...}
stack2* s = NULL; // global variable void p(){ stack2 t; t.push(2); s = &t; } void main() { p(); ...}
stack2* p(){ stack2* t = new stack2(); t->push(2); return t; } void main() { stack2* t; t = p(); ...}
stack2* p(){ stack2* t = new stack2(); t->push(2); return t; } void main() { cout << p()->position(2); ...}