C + + templates and a two-way linked list for queue queues

Source: Internet
Author: User

Using C + + templates:

Template <class or you can use typename t>
Template <class T>//template is an identifier Description: Template is a keyword that declares templates, which declares that a template keyword class cannot be omitted, and if the type parameter is extra, the class < type parameter list > can contain the base data type can contain the class type, before each parameter. Note:Template <class t> is valid only once, how many minor declarations are required, as long as you use node<t> to instantiate: template <class t>struct Stack {t* St;int tp;stack () {TP =-1;st = new t[1000];}void Pop () {if (tp = =-1) {int a = 1/0;}--TP;}T Top () {if (tp = =-1) {int a = 1/0;}return st[tp--];}void push (T a) {ST[++TP] = A;} bool Empty () {return TP = =-1;}void Clear () {TP =-1;}};int Main () {Stack<char> St;Char s[1000];While (CIN >> s) {st.clear ();bool F = true;For (char* k = s; *k; k++) {if (*k = = ' (') St.push (*k);else {if (St.empty ()) {F = false;Break ;} else {St.pop ();}}}cout<<f<<endl;}}//above for the master code above to implement the basic functions of the stack and use the written stack to achieve parentheses matching. Use a doubly linked list to implement a queue and use the heap you write to implement a hierarchical traversal of a tree # include <iostream> #include <vector>using namespace std;template <class T >struct node{node *next;Node *prior;T data;node () { }};template <class t>//Valid only once struct q{node<t>* front;//.node<t>* end;Q () {Front=null;End=null;}void push (T a) {node<t> *n=new node<t> ();n->data=a;if (!front) {front=n;front->prior=null;}if (!end) {end=n;//n-> prior=null; }else{n->prior=end;end->next=n;end=n;}end->next=null;}void Pop () {if (!front) {cout<< "error:: The queue is NULL" <<endl;return;}node<t> *n;N=front;front=n->next;if (front)front->prior=null;else End=null;Delete (n);}T visittop () {if (front)return front->data;else {cout<< "error";return NULL;}}bool Empty () {if (!front) return 1;else return 0;} };struct TreeNode {int val;      TreeNode *left;      TreeNode *right;    TreeNode (int x): Val (x), left (null), right (null) {}};struct Node {TreeNode *u;    int level; Node (TreeNode *a=null,int b=0) {U=a;level=b;}};class Solution {public:vector<vector<int> > Levelorder (TreeNode *root) {vector<vector<int> > vv;Q<node> Q;        Vector<int> E; if (root!=null){Q.push (Node (root,0));}while (!q.empty ()) {Node temp=q.visittop (); Q.pop ();int level=temp.level;Special handling of If (Vv.size () ==level) {//null tree Vv.push_back (E); special treatment of//null treesspecial treatment of}//null treeif (temp.u->left) {Q.push (Node (temp.u->left,level+1));}if (temp.u->right) {Q.push (Node (temp.u->right,level+1));}Vv[level].push_back (temp.u->val);}//very silly method, to see more master code return VV; }};int Main () {TreeNode root (1);solution S;S.levelorder (&root);return 0;}

Master Blog: http://ofpsxx.com

C + + templates and a two-way linked list for queue queues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.