1)list.h
/* * list_2.cpp * * Created on: 2013年8月2日 * Author: 黄东东 * 为了能有章泽天这样的女朋友而不断努力。。。。。。 */ #includeusing namespace std; typedef int T; class List { struct Node { T data; Node* next; Node(const T& d = T()) : data(d), next(0) { } }; Node* head; int len; public: List() : head(NULL), len(0) { } ~List() { clear(); } void insert(const T& d, int pos) { Node*& pn = getptr(pos); Node* p = new Node(d); p->next = pn; pn = p; ++len; } void push_front(const T& d) { insert(d, 0); } List& push_back(const T& d) { insert(d, size()); return *this; } void clear() { Node* p; while (head != NULL) { p = head->next; delete head; head = p; } } void erase(int pos) { if (pos < 0 || pos >= size()) { return; } Node*& pn = getptr(pos); Node* p = pn; pn = pn->next; delete p; --len; } void remove(const T& d) { int pos; while ((pos = find(d)) != -1) { erase(pos); } } void set( int pos,const T& d) { if (pos < 0 || pos >= size()) { return; } getptr(pos)->data = d; } Node*& getptr(int pos) { if (pos < 0 || pos > size()) { pos = 0; } if (pos == 0) { return head; } Node* p = head; for (int i = 1; i < pos; ++i) { p = p->next; } return (*p).next; } int find(const T& d) { Node* p = head; int pos = 0; while (p) { if (p->data == d) { return pos; } p = p->next; pos++; } return -1; } const int& front() { if (empty()) { throw "空"; } return head->data; } int back() { if (empty()) { throw "空"; } Node* p = head; while (p->next != NULL) { p = p->next; } return (*p).data; } bool empty() { return head == NULL; } int size() { return len; } void travel() { Node* p = head; while (p != NULL) { cout << p->data << ' '; p = p->next; } cout< using namespace std; typedef int T; class List { struct Node { T data; Node* next; Node(const T& d = T()) : data(d), next(0) { } }; Node* head; int len; public: List() : head(NULL), len(0) { } ~List() { clear(); } void insert(const T& d, int pos) { Node*& pn = getptr(pos); Node* p = new Node(d); p->next = pn; pn = p; ++len; } void push_front(const T& d) { insert(d, 0); } List& push_back(const T& d) { insert(d, size()); return *this; } void clear() { Node* p; while (head != NULL) { p = head->next; delete head; head = p; } } void erase(int pos) { if (pos < 0 || pos >= size()) { return; } Node*& pn = getptr(pos); Node* p = pn; pn = pn->next; delete p; --len; } void remove(const T& d) { int pos; while ((pos = find(d)) != -1) { erase(pos); } } void set( int pos,const T& d) { if (pos < 0 || pos >= size()) { return; } getptr(pos)->data = d; } Node*& getptr(int pos) { if (pos < 0 || pos > size()) { pos = 0; } if (pos == 0) { return head; } Node* p = head; for (int i = 1; i < pos; ++i) { p = p->next; } return (*p).next; } int find(const T& d) { Node* p = head; int pos = 0; while (p) { if (p->data == d) { return pos; } p = p->next; pos++; } return -1; } const int& front() { if (empty()) { throw "空"; } return head->data; } int back() { if (empty()) { throw "空"; } Node* p = head; while (p->next != NULL) { p = p->next; } return (*p).data; } bool empty() { return head == NULL; } int size() { return len; } void travel() { Node* p = head; while (p != NULL) { cout << p->data << ' '; p = p->next; } cout