[Data structure] Implementation of stack-based chained Stack class templates

Source: Internet
Author: User

[Data structure] Implementation of stack-based chained Stack class templates

Implementation of the abstract base class of the stack: (you can use the abstract base class for convenience of using virtual functions)


# Ifndef STACK # define STACK // The abstract base class template of the Stack <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


Implementation of the chain stack template class: (covering all pure virtual functions in the abstract base class and adding some members)


/// // # Include "Stack. h "# include <iostream> # include <cstdlib> # include <cassert> using namespace std; template <class T> struct LinkNode // linked list node class {T data; linkNode <T> * link; LinkNode (LinkNode <T> * ptr = NULL): link (ptr) {} LinkNode (const T & item, linkNode <T> * ptr = NULL): data (item), link (ptr) {}}; template <class T> class metadata Stack: public Stack <T> {public: consumer stack (): top (NULL) {} consumer stack (c Onst consumer stack <T> & rhs); consumer stack <T> & operator = (const consumer stack <T> & rhs );~ Consumer stack () {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, consumer stack <T> & rhs); // Add <T> private: LinkNode <T> * top ;}; template <class T> void consumer stack <T>: makeEmpty () {LinkNode <T> * p = NULL; while (top! = NULL) {p = top; top = top-> link; delete p ;}} template <class T> void consumer stack <T >:: Push (const T & x) {top = new LinkNode <T> (x, top); assert (top! = NULL);} template <class T> bool consumer stack <T>: Pop (T & x) {if (IsEmpty () return false; linkNode <T> * p = top; top = top-> link; x = p-> data; delete p; return true ;} template <class T> bool consumer stack <T>: getTop (T & x) const {if (IsEmpty () return false; x = top-> data; return true ;} template <class T> int outer stack <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, consumer stack <T> & rhs) // No <T> {out <"elements nums:" <rhs. getSize () <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> consumer stack <T>: const consumer stack <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> consumer stack <T> & Consumer stack <T> :: operator = (const compute stack <T> & rhs) // the essence of copy assignment is: destructor + copy constructor {if (top! = NULL) // first analyzes the structure and releases the resource makeEmpty (); LinkNode <T> * src = rhs. top; // copy construct 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 of the class is as follows:

Int main (int argc, char * argv []) {consumer stack <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 the explicit cout of the explicit bool value <"IsEmpty (): "<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; Consumer stack <int> s1 (s), s2; cout <s1; s2 = s1; cout <s2; system ("pause"); return 0 ;}

The test results are as follows:


Elements nums: 5 elements is: 5 4 3 2 1 elements nums: 4 elements is: 4 3 2 1 IsEmpty (): falseIsFull (): falsegetTop (): 4 elements nums: 0 elements is: elements nums: 3 elements is: 3 2 1 elements nums: 3 elements is: 3 2 1 elements nums: 3 elements is: 3 2 1 press any key to continue...

Note:

1. In the implementation and declaration of the template class's friend functions, an additional parameter <T> is required for the declared time, which is not added during the implementation time.

2. The implementation uses a single-chain table without an additional header node. Therefore, when copying a table, you must identify whether it is an empty chain table or the first time you create a chain table.

3. the stack of the chain stack is at the top of the chain table, and the insertion and deletion of new nodes are at the top of the stack, which is the operation of the elements at the top of the stack.

4. The stack implemented by the linked list cannot be full, so false is always returned.

5. cout <std: boolaapha and cout <std: noboolaapha can be used to explicitly control boolean values.







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.