AVIQ2 - data[i] = q.data[i]; } stack&...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; class stack { private: int max; int count; int *data; static int grow; public: stack(void); ~stack(void); stack(const stack& q); stack& operator=(const stack& q); void push(int item); int pop(void); void show(ostream& out) const; }; int stack::grow = 5; int main() { int i, x; stack s; ofstream fout ("stackgrows.out"); for(i=1; i<=12; i++) s.push(i); cout << "The stack is:\n"; s.show(cout); cout << "\n\n"; fout << "The stack is:\n"; s.show(fout); fout << "\n\n"; fout.close(); system("notepad stackgrows.out"); return 0; } stack::stack(void) { max = 0; data = NULL; count = 0; } stack::~stack(void) { delete [] data; } { int i; max = q.max; count = q.count; data = new int[max]; for(i=0; i < count; i++)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: data[i] = q.data[i]; } stack&amp; stack::operator=(const stack&amp; q) { int i; if(this != &amp;q) { delete data; max = q.max; count = q.count; data = new int[max]; for(i=0; i &lt; count; i++) data[i] = q.data[i]; } return *this; } void stack::push(int iteam){ int i; if(count==max){ int*temp; temp=new int [count]; for(i=0;i&lt;max;i++) temp[i]=data[i]; delete data; max=max+grow; data=new int [max]; for(i=0;i&lt;max;i++) data[i]=temp[i]; delete temp; } data[count]=iteam; count++; } int stack::pop(){ if(count==0){ cerr&lt;&lt;&quot;stack is empty\n&quot;; exit(1); } int y=data[count-1]; count--; return y; } void stack::show(ostream&amp; out) const{ int i; for(i=0;i&lt;=count-1;i++) out&lt;&lt;data[i]&lt;&lt;&quot;,&quot;; out&lt;&lt;endl; }...
View Full Document

Page1 / 2

AVIQ2 - data[i] = q.data[i]; } stack&amp;amp;amp;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online