Heap (stack) C and C + + template implementations (NULL class default member functions first talk about referencing inline functions)

Source: Internet
Author: User

The basic operations of the stack include <iostream>using namespace std;const int maxn = 3;typedef struct stack{//numtype num; int num;} Stack;int top = 0;//The previous element in the position of the current element, stack Stack[maxn];bool is_empty (), bool Is_full (), int pop (), void push (const int &key)    ; int get_top (); bool Make_empty (); int main () {int tmp; cout << get_top () << "<< pop () << endl;//This sentence has the same error as the following cout <<" Input a set of Intege    RS: ( -1 is over) "<< Endl;        while (CIN >> tmp) {if (TMP = =-1) break;    Push (TMP);    } TMP = Get_top ();    cout << tmp << Endl;    TMP = Pop ();    cout << tmp << Endl;    cout << pop () << "<< get_top () << Endl; cout << get_top () << "<< pop () << endl;//so the output will happen very wonderful thing to return 0;} If stack is empty return Truebool Is_empty () {return (top = = 0);} If stack is full return Truebool Is_full () {return (top = = MAXN);} Pop the top number if not Emptyint Pop () {if (Is_empty ()) {cout << "The Stack is empty,cannot pop ..." << Endl;    return-1;        } else {top--;    return stack[top].num; }}//push one number,if not fullvoid push (const int &key) {if (Is_full ())} {cout << "the Stack is Fu        Ll,cannot push ... "<< Endl;    Return        } else {stack[top].num = key;    Top + +; }}//get the top number,not popint get_top () {if (Is_empty ()) {cout << "The Stack is Empty,cannot get t        Op ... "<< Endl;    return-1;    } else {return stack[top-1].num; }}bool Make_empty () {top = 0;}

/*
Learning experience:
1, this stack with the linked list implementation, and the above with the array implementation is not the same; Use the template class stack<dt>
2. This class uses the copy constructor Stack (const stack<dt> &original), operator overloaded function
stack<dt>& operator = (const stack<dt> &original)
3.default constructor for initialization stack (): Top (NULL) {}, remember that functions declared in a class can not have a function body,
However, the empty function body ({} only) is possible (that is, the default constructor; All member variables are not initialized.
However, it can be initialized with a default constructor, such as Stack (): Top (NULL) {}, or defined as static.

4.when a &element reference is used as a parameter: (1) const stack<dt> &original as the immutable variable
Reference Pass (2) the reference parameter of the DT &top_element function as the return value (multiple return values can be implemented)
5, the introduction of inline functions, inline extension is used to eliminate the time overhead of function calls. It is commonly used for frequent
The function to execute. A small memory space function is very beneficial, similar to macro definitions, but macros also have a lot of undesirable places:
(1) Macros cannot access private members of an object. (2) The definition of a macro can easily produce two semantics.
6, template and typedef can not be used at the same time: templates are not types! typedef can only give
The type takes an alias. A separate node is a template, and node<int> is a real type.
The Using keyword can alias a template! such as: Template<class t>
Using Test = node<t>; Test<int> T;
equivalent to:node<int> t;
7. The default constructor is not automatically defined as long as any one of the constructors is defined. If you want to use the default constructor at this point,
You must manually define a default constructor. The so-called default constructor is the constructor that does not have to be given arguments when the constructor is called.

8. If any one of the constructors is defined in a class, and no default constructor is defined, it is not possible to define the object of the class as: Classa{}; a AA; (this is wrong)

But a *pa; (this is legal, because only a pointer is defined, no constructor is called, no memory is allocated for the class object)

9: Write an empty class; What functions will be generated when the Class A () {} is compiled?
Default constructor
Destructors
Copy constructor
Assignment operator (operator=)
Accessor operator (operator&) (pair, one non-const, one const)
Of course, all of this can only be produced if needed. For example, if you define a class, but never define an object of that class, or use a function parameter of that type, then there is no basic result. If, for example, you have never had an assignment between objects of that type, then operator= will not be generated.
The last pair of access operators is controversial, I remember there was a post to discuss this, is said to be related to the specific compiler, some generated, some do not generate, only the first four
*/

Class Empty{public:               Empty ();   default constructor               Empty (const   empty&);   copy Constructor               ~empty ();   destructor               empty&  perator= (const empty&);  The   assignment operator               empty*  operator& ();              The   value operator               const  empty*  operator& ()  const;   The   value operator};

here is the template class:

#include <iostream> #include <string>using namespace std;//stack//into the stack with "linked list", the time complexity of the Stack is O (1)//copy,    The time complexity of emptying and traversing the stack is O (n) template<class dt>//typedef struct node//template cannot be used typedefstruct node{DT info; Node<dt> *next;}; Template<class dt>class stack{public:stack (): Top (NULL) {};//Initial top constructor ...        Initializes the default mode of the Stack (const stack<dt> &original);        ~stack ();        stack<dt> &operator = (const stack<dt> &original);        void push (const DT &element);        BOOL Pop (DT &element);    BOOL Peek (DT &top_element);//get stack top data bool Is_empty () const;//is empty without full void make_empty (); Private:node<dt> *top,*pre;//Private member, pointing to the current member, to the head of the list of inline void deepcopy (const stack<dt> &orig inal);//Deep copy};template<class dt>stack<dt>::stack (const stack<dt> &original) {deepCopy ( original);} Template<class Dt>stack<dt>::~stack () {make_empty ();//Direct Bar in classThe function of the polygon}template<class dt>void stack<dt>::p ush (const DT &element) {pre = new node<dt>;    Pre->info = element;    Pre->next = top; top = Pre;} Front insertion method, top pointing to the head node of the list template<class dt>stack<dt>& stack<dt>::operator = (const stack<dt>    &original) {deepcopy (original); return this;} operator overloaded function, returned by reference Template<class Dt>bool Stack<dt>::p op (DT &element) {if (top = = NULL) return Fals    E       else {element = top->info;       Pre = top;       top = top->next;       Delete pre;    Pre = NULL;    }//cout << "*****pop" << Endl; return true;}    Template<class Dt>bool stack<dt>::p eek (DT &top_element) {if (top = = NULL) return false;    else {top_element = top->info;    }//cout << "***peek" << Endl; return true;} Template<class dt>bool stack<dt>::is_empty () const{return (top = = NULL); Template<class dt>void StAck<dt>::make_empty () {if (top = = NULL) return;       while (top! = NULL) {pre = top;       top = top->next;       Delete pre;    Pre = NULL; }}template<class dt>void stack<dt>::d eepcopy (const stack<dt> &original) {Node<DT> *    Oriptr = Original.top;    Node<dt> *copyptr = top = new node<dt>;    Copyptr->info = oriptr->info;        while (oriptr->next! = NULL) {copyptr->next = new node<dt>;//classic!!!        Copyptr = copyptr->next;        Oriptr = oriptr->next;    Copyptr->info = oriptr->info; } Copyptr->next = NULL;} int main () {stack<string> obj;//The constructor of the template class string str[5] = {"Hello", "the", "World", "Welcome", "to"};string strtemp; for (size_t Ix=0;ix!=5;++ix) {Obj.push (Str[ix]), Obj.peek (strtemp);cout<<strtemp<< ",";} cout<<endl;  Stack<string> obj1 (obj); Test the copy constructor for (size_t Ix=0;ix!=5;++ix) {obj1.pop (strtemp);cout<<strtemp<< ",";} Cout<< Endl;if (Obj1.is_empty ()) cout<< "Stack is empty!" <<endl; Stack<string> obj2 (obj); Test copy constructor Obj2.make_empty (); if (Obj2.is_empty ()) cout<< "Stack is empty!" <<endl;    stack<string> obj3 = obj; Test overloading "=" operator for (size_t Ix=0;ix!=5;++ix) {obj3.pop (strtemp);cout<<strtemp<< ",";} Cout<<endl;for (size_t ix=0;ix!=5;++ix) {obj.pop (strtemp);cout<<strtemp<< ",";} Cout<<endl;return 0;}

/*
Inline functions are functions declared using the inline keyword, as well as inline functions, and its main function is to solve the program's operation
Efficiency. When using inline functions, be aware that:

1. Recursive functions cannot be defined as inline functions
2. The inline function is generally suitable for small functions that do not have complex structures such as while and switch, and only have the sentence of the
Otherwise the compilation system treats the function as a normal function.
3. Inline functions can only be defined and used, otherwise the compiled system will consider it a normal function.
4. The inline function cannot make an exception to the interface declaration.
*/

Heap (stack) C and C + + template implementations (NULL class default member functions first talk about referencing inline functions)

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.