main.cpp #include #include #include "list.h" using namespace std; int main(/*int argc, char* argv[]*/){ list *l = new list; //FILE* fp = fopen(argv, "r"); FILE* fp = fopen("test.txt", "r"); int i; if (fp != NULL){ while (fscanf(fp, "%d", &i) != EOF){ //cout << "about to insert " << i << endl; l->insert(i); /*cout << "head = " << l->head->data << endl; cout << "tail = " << l->tail->data << endl; cout << "inserted " << i << endl;*/ } fclose(fp); } cout << *l << endl; l->reverse(); cout << *l << endl; delete l; return 0; } list.cpp #include "list.h" #include using namespace std; ostream& operator<<(ostream&, const list&); list::list(void){ head = NULL; tail = NULL; s = 0; } list::~list(){ node *cur = head; int i = 0; for (node *cur = head; cur != NULL; cur = cur->next){ delete cur->prev; } delete tail; } void list::insert(int i){ node *n = new node(i); if (head == NULL){ head = n; tail = n; }else if (n->data > tail->data){ tail->next = n; n->prev = tail; tail = n; }else{ for (node *cur = head; cur != NULL; cur = cur->next){ if (n->data > cur->data){ //dunno }else if (n->data == cur->data){ //nothin }else if (n->data < cur->data){ if (cur == head){ n->next = head; head->prev = n; head = n; }else{ n->next = cur; n->prev = cur->prev; cur->prev->next = n; cur->prev = n; } } break; } } s++; } int& list::operator[](int i){ if (i > s) throw "index too bigzor"; node *cur = head; for (int j = 0; j < s; j++){ cur = cur->next; } return cur->data; } int list::size(void){ return s; } void list::reverse(void){ node *temp = head; head = tail; tail = temp; } ostream& operator<<(ostream& out, list& l){ cout << "in overload<< " << endl; for (int i = 0; i < l.size(); i++){ cout << l[i] << endl; out << l[i] << "\n"; } return out; }