c14

来源:互联网 发布:盒子鱼软件下载 编辑:程序博客网 时间:2024/05/29 15:07

1.链表、栈、队列实现:

#include <iostream>#include <fstream>#include <string>using namespace std;template <typename T>class list{struct Node{T data;Node *next;Node(const T& t = T()):data(t),next(NULL){}};Node *head;public:list():head(NULL){}void clear(){while (head){Node *p = head->next;delete head;head = p;}}~list(){clear();}void insert_front(const T& t){Node *p = new Node(t);p->next = head;head = p;}void insert_back(const T& t){Node* p = new Node(t);if (!head)head = p;elsegetPointer(size() - 1)->next = p;}void travel(){Node *p = head;while (p){cout<<p->data<<' ';p = p->next;}cout<<endl;}int size(){int cnt = 0;Node *p = head;while (p){++cnt;p = p->next;}return cnt;}T getHead(){if (!head)throw "no head";return head->data;}T getTail(){if (!head)throw "no tail";Node* p = head;while (p->next)p = p->next;return p->data;}bool empty(){return head == NULL;}int find(const T& t){int pos = 0;Node* p = head;while (p){if (p->data == t)return pos;p = p->next;++pos;}return -1;}bool update(const T& o, const T& n){int pos = find(o);if (pos == -1)return false;getPointer(pos)->data = n;return true;}bool erase(const T& t){int pos = find(t);if (pos == -1)return false;if (pos == 0){Node *p = head->next;delete head;head = p;}else{Node *p = getPointer(pos - 1);Node *q = p->next;p->next = q->next;delete q;q = NULL;}}private:Node* getPointer(int pos){Node *p = head;for (int i = 0; i < pos; ++i)p = p->next;return p;}};template <typename T>class Stack{list<T> l;public:void push(const T&t){l.insert_front(t);}void pop(){l.erase(l.getHead());}T top(){return l.getHead();}bool empty(){return l.empty();}int size(){return l.size();}void clear(){l.clear();}};template <typename T>class Queue{list<T> l;public:void push(const T& t){l.insert_back(t);}void pop(){l.erase(l.getHead());}T front(){return l.getHead();}T back(){return l.getTail();}bool empty(){return l.empty();}int size(){return l.size();}void clear(){l.clear();}};int main(){Stack<string> s;s.push("good");s.push("morning");s.push("everyone");while (!s.empty()){cout<<s.top()<<' ';s.pop();}cout<<endl;Queue<double> d;d.push(1.0);d.push(2.0);d.push(3.0);d.push(4.0);d.push(5.0);cout.setf(ios::showpoint);while (!d.empty()){cout<<d.front()<<' ';d.pop();}cout<<endl;system("pause");return 0;}

2.try{ throw 数据; } catch(子类 e){} catch(父类 e){} catch(...){}逐层上抛

3.数组实现stack:

#include <iostream>#include <fstream>#include <string>using namespace std;template <typename T>class Stack{T a[10];int n;public:void push(const T&t){if (full())throw "overflow";a[n++] = t;}void pop(){if (empty()){throw "empty stack";}--n;}T top(){if (empty()){throw "no top";}return a[n-1];}bool empty(){return n == 0;}bool full(){return n == 10;}int size(){return n;}int capacity(){return 10;}void clear(){n = 0;}Stack():n(0){}};int main(){Stack<string> s;s.push("good");s.push("morning");s.push("everyone");while (!s.empty()){cout<<s.top()<<' ';s.pop();}cout<<endl;system("pause");return 0;}

4.throw "hello" catch(const char* e){}

5.二叉树:BST: binary searh/sort tree

二叉树实现


0 0