AVIQ1 - end = q.end; data = new int[max]; for(i=0;...

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

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> using namespace std; class queue { private: int max; int count; int start; int end; int *data; public: queue(int size); queue(const queue& q); ~queue(void); queue& operator=(const queue& q); void put(int item); int get(void); void show(ostream& out); }; int main() { queue myqueue(4); ofstream fout ("queue.out"); char ch; int x; do { cout << "\n\np = put \n"; cout << "g = get\n"; cout << "s = show\n"; cout << "f = print to file\n"; cout << "q = quit\n\n"; cin >> ch; if (ch == 'p') { cout <<"\ndata to put :"; cin >> x; myqueue.put(x); } else if(ch == 'g') cout << "\n\ndata gotten : " << myqueue.get(); else if(ch == 's') myqueue.show(cout); else if(ch == 'f') myqueue.show(fout); }while(ch != 'q'); return 0; } queue::queue(int size) { max = size; data = new int[max]; start = end = count = 0; } queue::~queue(void){ delete [] data; } int i; max = q.max; start = q.start;
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: end = q.end; data = new int[max]; for(i=0; i&lt;max; i++) data[i] = q.data[i]; } queue&amp; queue::operator=(const queue&amp; q) { int i; if(this != &amp;q) { max = q.max; start = q.start; end = q.end; delete data; data = new int[max]; for(i=0; i&lt;max; i++) data[i] = q.data[i]; } return *this; } void queue::put(int iteam){ if(count&gt;max-1){ cerr&lt;&lt;&quot;queue is full\n&quot;; system(&quot;pause&quot;); exit(1); } data[end]=iteam; end++; count++; if(end&gt;max-1) end=0; } int queue::get(){ int r; if(count==0){ cerr&lt;&lt;&quot;queue is empty\n&quot;; system(&quot;pause&quot;); exit(1); } r=data[start]; start++; count--; if(start&gt;max-1) start=0; return r; } void queue::show(ostream&amp; out){ int i; if(start&lt;=end-1) for(i=start;i&lt;=end-1;i++) out&lt;&lt;data[i]&lt;&lt;&quot;,&quot;; else if(start&gt;=end-1&amp;&amp;count!=0){ for(i=start;i&lt;=max-1;i++) out&lt;&lt;data[i]&lt;&lt;&quot;,&quot;; for(i=0;i&lt;=end-1;i++) out&lt;&lt;data[i]&lt;&lt;&quot;,&quot;; } out&lt;&lt;endl; }...
View Full Document

This note was uploaded on 02/21/2010 for the course ENGM 2282 taught by Professor Phillips during the Winter '10 term at Dalhousie.

Page1 / 2

AVIQ1 - end = q.end; data = new int[max]; for(i=0;...

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