AVIQ3 - } list::list(const list& x) { int i;...

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 list{ friend ostream& operator << (ostream& out, list p); private: int max; int count; int *data; public: list(int size); list(const list& x); ~list(void) { delete [] data;} list& operator=(const list& x); void add(int item); void del(int item); }; int main() { list mylist(20); ofstream fout ("list.out"); char ch; int x; do { cout << "\n\na = add\n"; cout << "d = delete\n"; cout << "s = show\n"; cout << "f = print to file\n"; cout << "q = quit\n\n"; cin >> ch; if (ch == 'a') { cout <<"\ndata to add :"; cin >> x; mylist.add(x); } else if(ch == 'd') { cout << "\n\ndata to delete :"; cin >> x; mylist.del(x); } else if(ch == 's') cout << mylist; else if(ch == 'f') fout << mylist; }while(ch != 'q'); fout.close(); system("notepad list.out"); return 0; } list::list(int size) { max = size; data = new int[max]; count = 0;
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: } list::list(const list&amp; x) { int i; max = x.max; count = x.count; data = new int[max]; for(i=0; i&lt;count; i++) data[i] = x.data[i]; } list&amp; list::operator=(const list&amp; x) { int i; if(this != &amp;x) { max = x.max; count = x.count; data = new int[max]; for(i=0; i&lt;count; i++) data[i] = x.data[i]; } return *this; } void list::add(int iteam){ int i(0),location; while(1){ if(data[i]&gt;=iteam){ location=i; for(i=count;i&gt;location;i--) data[i]=data[i-1]; data[location]=iteam; count++; break; } else if(i==count){ data[count]=iteam; count++; break; } i++; } } void list::del(int iteam){ int i(0),j; while(i&lt;=count){ if(data[i]==iteam){ for(j=i;j&lt;=count;j++) data[j]=data[j+1]; count--; } else i++; } } ostream&amp; operator&lt;&lt;(ostream&amp; out,list p){ for(int i=0;i&lt;p.count;i++) out&lt;&lt;p.data[i]&lt;&lt;&quot;,&quot;; out&lt;&lt;endl; return out; }...
View Full Document

Page1 / 2

AVIQ3 - } list::list(const list&amp;amp;amp; x) { int i;...

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