Access over 20 million homework & study documents

4 Write a stack class using an array (from the heap) to implement i

Content type
User Generated
Rating
Showing Page:
1/4
4. Write a stack class using an array (from the heap) to
implement it. What is a stack? We
saw the run-time stack in class. It is a data structure that
allows us to push an element to the
top, to pop from the top. In fact, for the no -recursive
towers of hanoi, we used a vector as a stack.
class stack{
private:
int top; // top is the index into the array for the current top
int * p; // p is a pointer into the heap where the stack is
actually
//located. Just like for SA class.
int size; // size of the array allocated for the stack
public:
stack(); // a stack to hold 100
stack(int n) // a stack to hold n elements
~stack();
bool empty();
void push( int ); // dont forget to test if stack is full
int pop(); // return and remove the top element of the
stack.
// dont forget to test if the stack is empty
}
Test it with the following main function:
int main(){
stack s(10);
s.push(1);
s.push(2);
s.push(3);
for (i=0; i<3; i++)
cout<<s.pop(); // should print 3,2 1
return 0;
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
Solution
#include <iostream>
#include <string>
using namespace std;
class Stack {
private:
int top;
int size;
int *storage; // storage is a pointer into the heap where
the stack is actually located
public:
Stack(int size) {
if (size <= 0)
throw string(\"Stack\'s size must be positive\");
storage = new int[size];
this->size = size;
top = -1;
}
void push(int value) {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
4. Write a stack class using an array (from the heap) to implement it. What is a “stack”? We saw the run-time stack in class. It is a data structure that allows us to “push” an element to the “top”, to “pop” from the “top”. In fact, for the no -recursive towers of hanoi, we used a vector as a stack. class stack{ private: int top; // top is the index into the array for the current top int * p; // p is a pointer into the heap where the stack is actually //located. Just like for SA class. int size; // size of the array allocated for the stack public: stack(); // a stack to hold 100 stack(int n) // a stack to hold n elements ~stack(); bool empty(); void push( int ); // don’t forget to test if stack is full int pop(); // return and remove the “top” element of the stack. // don’t forget to test if the stack is empty } Test it with the following main function: int main(){ stack s(10); s.push(1); s.push(2); s.push(3); for (i=0; i ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Great! Studypool always delivers quality work.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4