//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.h #ifndef LIST_H #define LIST_H #include "node.h" #include using namespace std; class list{ private: int s; public: node *head; node *tail; list(void); ~list(); void insert(int);// - insert the value into the list (in sorted order) int& operator[](int);// return the element at the specified index in the list int size(void);// returns the number of nodes in the list void reverse(void);// - reverse all nodes in the list }; ostream& operator<<(ostream&, list&); //Add each list element to the output stream //separated by spaces #endif //list.cpp #include "list.h" #include using namespace std; 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; } cur->prev = n; } } break; } } s++; } int& list::operator[](int i){ if (i > s - 1) throw "index too bigzor"; node *cur = head; for (int j = 0; j < i; j++){ cur = cur->next; } return cur->data; } int list::size(void){ return s; } void list::reverse(void){ node *temp = head; while (temp != NULL){ head = temp; temp = temp->next; head->next = head->prev; head->prev = temp; } delete 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; } //node.h #ifndef NODE_H #define NODE_H #define NULL 0 class node{ public: int data; node *prev; node *next; node(int); }; #endif //node.cpp #include "node.h" node::node(int i){ data = i; next = NULL; prev = NULL; }