<pre name="code" class="cpp">#ifndef _SEQLIST_ #define _SEQLIST_ #include<iostream> using namespace std; #include<assert.h> template<class Type> class Slist; //节点类 template<class Type> class Node { public: Node() :data(Type()), next(NULL){} Node(Type d, Node<Type> *n = NULL) :data(d), next(n){} void SetData(Type d) { data = d; } Type GetData()const { return data; } ~Node(){} private: Type data; Node <Type> *next; friend class Slist< Type>; }; // template<class Type> class Slist { public: Slist( ) { Node<Type> *s= _Buynode(0); first = last = s; size = 0; } ~Slist() { } Node<Type>* _Buynode(const Type &x) { Node<Type>* s = new Node<Type>(x); assert(s!=NULL); return s; } private: Node<Type> *first; Node <Type>*last; int size; public: bool push_back(const Type &x) { Node<Type>* s = _Buynode(x); last->next = s; last = s; size++; return true; } bool push_front(const Type &x) { Node<Type>* s = _Buynode(x); s->next = first->next; first->next = s; if (size ==0)// 0 { last = s; } size++; return true; } bool pop_back() { if ( size==0 )//为什么写成(0==size),下面调用出错; return false; { Node<Type>* p = first ; Node<Type>* q = p->next; while (q->next != NULL) { q = q->next; p = p->next; } delete last; last = p; last->next = NULL;//程序崩溃; size--; } return true; } bool pop_front() { if (size == 0) return false; if (size == 1) pop_back(); else { Node<Type>* s = first->next; first->next = s->next; delete s; size--; } return true; } /*bool insert_val2(const Type &x) { Node<Type>* p = first; while (p->next->data < x && p->next != NULL) p =p->next; if (p->next == NULL) { push_back(x); } else { Node<Type>* s = _Buynode(x); s->next = p->next; p->next = s; size++; } return true; } */ bool insert_val(const Type &x) { if (size == 0) push_back(x); Node<Type> *p = first; while (p->next != NULL && p->next->data < x) p = p->next; if (p->next == NULL) { push_back(x); } else { Node<Type> *s = _Buynode(x); s->next = p->next; p->next = s; size++; } return true; } Type length() { return size; } Node<Type>* find(const Type &key) { if (size == 0) return NULL; Node<Type> *p = first->next; while (p != NULL && p->data != key) p = p->next; return p; } bool delete_val(const Type &x) { if (size == 0) return false; Node<Type>*p = find(x); if (p == NULL) return false; if (p == last) { pop_back(); } else { Node<Type>*q = p->next; p->data = q->data; p->next = q->next; delete q; size--; } return true; } void sort() { if (size == 0 || size == 1) return ; Node<Type>* s= first->next; Node<Type>* q =s->next; last = s; s->next = NULL; while (q != NULL) { s = q; q = q->next; Node<Type>*p = first; while ( p->next != NULL && p->next->data < s->data ) p = p->next; if (p->next == NULL) { s->next = NULL; last->next = s; last = s; } else { s->next = p->next; p->next = s; } } } void resver() { if (size == 0 || size == 1) return; Node<Type>* s = first->next; Node<Type>* q = s->next; last = s; s->next = NULL; while (q != NULL) { s = q; q = q->next; push_front(s->data); } } bool next(const Type &x) { Node<Type>*p=find(x); if (p == NULL) { cout << "未能找到" << x << endl; return false; } Node<Type>*q = first; while (q->next!= NULL&&q->data!=x) { q = q->next; } if (q->next == NULL) { cout << x << "没有后继" << endl; return false; } if (q->data = x) cout << x << "的后继是" << q->next->data << endl; return true; } bool prio(const Type&x) { Node<Type>*p = find(x); if (p == NULL) { cout << "未能找到" << x << endl; return false; } Node<Type>*q = first; while ( q->data != x) { q = q->next; } if (q == first->next) { cout << x << "没有前驱" << endl; return false; } if (q->data = x) { Node<Type>*s = first; while (s->next != q) { s = s->next; } cout << x << "的前驱是" << s->data << endl; } return true; } void clear() { Node<Type>*p = first->next; while (p != NULL) { first->next= p->next; delete p; p = first->next; } first->next = NULL; last = first; size = 0; } void show_list() { if (size == 0) return; Node<Type> * p = first->next; while (p != NULL ) { cout << p->data << "-->"; p=p->next; } cout << "ok" << endl; } }; #endif
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/fujinlong520/article/details/48084735