Class template implementation of chain stack of [data structure] Stack

Source: Internet
Author: User

The implementation of the abstract base class of the stack: (not the abstract base class is also possible, in order to use virtual functions convenient)


Abstract base class for #ifndef stack#define stack//stack template<class t>class stack{public:    stack () {}    ~stack () {}    virtual void Push (const t& x) =0;    virtual bool Pop (t& x) =0;    virtual bool GetTop (t& x) const=0;    virtual bool IsEmpty () const=0;    virtual bool Isfull () const=0;    virtual int getsize () const=0;}; #endif


The concrete implementation of the chain Stack template class: (Overrides all pure virtual functions in the abstract base class, and adds some members)


#include "Stack.h" #include <iostream> #include <cstdlib> #include <cassert      >using namespace Std;template<class t> struct Linknode//List node class {T data;        linknode<t>* link; Linknode (linknode<t>* ptr=null): Link (ptr) {} linknode (const t& item,linknode<t>* ptr=null):d ATA (item ), link (ptr) {}};template<class T>class linkedstack:public stack<t>{public:linkedstack (): Top (NULL) {} Li    Nkedstack (const linkedstack<t>& RHS);    linkedstack<t>& operator= (const linkedstack<t>& RHS);    ~linkedstack () {makeempty ();}    void Push (const t& x);    BOOL Pop (t& x);    BOOL GetTop (t& x) const;    BOOL IsEmpty () Const{return Top==null?true:false;} BOOL Isfull () Const{return false;}    Meaningless, cannot be full int getsize () const;    void Makeempty ();    Friend ostream& operator<< <T> (ostream& out,linkedstack<t>& RHS);//Plus <t>private: linknode<t>* top;};    Template<class t>void Linkedstack<t>::makeempty () {linknode<t>* P=NULL;        while (top!=null) {p=top;        top=top->link;    Delete p;    }}template<class t>void linkedstack<t>::P ush (const t& x) {top=new linknode<t> (x,top); ASSERT (Top!=null);}    Template<class T>bool Linkedstack<t>::P op (t& x) {if (IsEmpty ()) return false;    linknode<t>* P=top;    top=top->link;    x=p->data;    Delete p; return true;}     Template<class T>bool Linkedstack<t>::gettop (t& x) const{if (IsEmpty ()) return false;     x=top->data; return true;}    Template<class t>int linkedstack<t>::getsize () const{int k=0;    linknode<t>* P=top;        while (p!=null) {++k;    p=p->link; } return k;}    Template<class t>ostream& operator<< (ostream& out,linkedstack<t>& RHS)//Not add <t>{ out<< "Elements nums:" <<rhs.getsize () &LT;&LT;endl;    linknode<t>* P=rhs.top;    out<< "Elements is:";        while (p!=null) {out<<p->data<< "";    p=p->link;    } out<<endl; return out;} Template<class t>linkedstack<t>::linkedstack (const linkedstack<t>& RHS) {LinkNode<T>*    Src=rhs.top;    linknode<t>* Dest=null;    linknode<t>* Newnode=null;        while (src!=null) {newnode=new linknode<t> (src->data);            if (dest==null) {dest=newnode;        Top=dest;            }else{dest->link=newnode;        Dest=newnode;    } src=src->link; }}template<class t>linkedstack<t>& linkedstack<t>::operator= (const LinkedStack<T>    & RHS) The essence of//copy assignment is: destructor +copy constructor {if (Top!=null)//First destructor, release resource Makeempty ();    linknode<t>* src=rhs.top;//copy tectonic linknode<t>* dest=null;    linknode<t>* Newnode=null; while (src!=null) {newnode=new linknode<t> (src->data);            if (dest==null) {dest=newnode;        Top=dest;            }else{dest->link=newnode;        Dest=newnode;    } src=src->link; } return *this;}

The test code for the class is as follows:

int main (int argc, char* argv[]) {        linkedstack<int> s;    int a=1,b=2,c=3,d=4,e=5;    S.push (a);    S.push (b);    S.push (c);    S.push (d);    S.push (e);    cout<<s;    S.pop (e);    cout<<s;       cout<<std::boolalpha;//control Explicit    cout<< "IsEmpty () for an explicitly bool value:" <<s.isempty () <<endl;    cout<< "Isfull ():" <<s.isfull () <<endl;    cout<<std::noboolalpha;        S.gettop (e);    cout<< "GetTop ():" <<e<<endl;    S.makeempty ();    cout<<s;    S.push (a);    S.push (b);    S.push (c);    cout<<s;    linkedstack<int> S1 (s), S2;    cout<<s1;    S2=S1;    cout<<s2;    System ("pause");    return 0;}

The test results are as follows:


Elements nums:5elements is:5 4 3 2 1elements nums:4elements is:4 3 2 1IsEmpty (): Falseisfull (): Falsegettop (): 4elemen  TS nums:0elements is:elements nums:3elements is:3 2 1elements nums:3elements is:3 2 1elements nums:3elements is:3 2 1 Please press any key to continue ...

Precautions:

1. When implementing and declaring a friend function for a template class: The declared time is added to the additional parameter <t>, which is not added at the time of implementation.

2. The implementation of the use of a single linked list without additional head nodes, so pay attention to the copy, the list to distinguish whether it is an empty list or the first time to establish a linked list.

3. The stack top of the chain stack is in the head of the list, and the insertion and deletion of the new node are done at the top of the stack, which is the operation of the top element.

4. The linked list implementation stack is not full, so always return false

5. Use Cout<<std::boolaapha and Cout<<std::noboolaapha to explicitly control the explicit way of Boolean values.







Class template implementation of chain stack of [data structure] Stack

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.