{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

AVIQ3 - list:list(const list& x int i max = x.max count...

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 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 Document Right 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& x) { int i; max = x.max; count = x.count; data = new int[max]; for(i=0; i<count; i++) data[i] = x.data[i]; } list& list::operator=(const list& x) { int i; if(this != &x) { max = x.max; count = x.count; data = new int[max]; for(i=0; i<count; i++) data[i] = x.data[i]; } return *this; } void list::add(int iteam){ int i(0),location; while(1){ if(data[i]>=iteam){ location=i; for(i=count;i>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<=count){ if(data[i]==iteam){ for(j=i;j<=count;j++) data[j]=data[j+1]; count--; } else i++; } } ostream& operator<<(ostream& out,list p){ for(int i=0;i<p.count;i++) out<<p.data[i]<<","; out<<endl; return out; }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

AVIQ3 - list:list(const list& x int i max = x.max count...

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

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