Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the stack of C++

Shulou Source: shulou.com Published: 2022-06-03 02:33:50 09月23日 Update

This article will explain in detail how to implement the stack on C++. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

/ *

1. Determine whether the stack is empty

two。 Get the length of the stack

3. Elements into the stack

4. Element out of stack

5. Clear the stack

6. Stack top element

7. Stack bottom element

, /

The space of the stack increases dynamically:

/ *

Dynamically increase the size of the space instead of defining a very large space during initialization

Principle:

Note: when dynamically increasing the size, configure a new piece of space at twice the original size

Then copy the original content, release the original space, and point the pointer to the new space.

, /

# include

Using namespace std

# pragma once

Class MyStack

{

Public:

MyStack (void)

~ MyStack (void)

Void push (int elem)

Void pop (int& elem)

Void clearStack ()

Bool isEmpty ()

Int stackLength ()

Void stackTraverse (bool isFromBottom); / / Calendar of elements

Int begin (); / / Top stack element

Int back (); / / bottom element of the stack

Private:

Int* masks pBufferwitter / stack space address

Int massively top; / / points to the top of the stack and determines the number of elements in the stack.

Int MacroiSize; / / initial size of the stack

}

MyStack::MyStack (void)

{

M_iTop = 0

M_iSize = 5

M_pBuffer = new int [m _ iSize]

}

MyStack::~MyStack (void)

{

Delete [] m_pBuffer

}

/ *

The following uses to dynamically increase the size of the space, rather than defining a very large space during initialization

Principle:

Note: when dynamically increasing the size, configure a new piece of space at twice the original size

Then copy the original content, release the original space, and point the pointer to the new space.

, /

Void MyStack::push (int elem)

{

If (m_iTop = = m_iSize)

{

M_iSize * = 2

Int* pTemp = new int [m _ iSize]

For (int I = 0 * * I < m\% I Top; + + I)

{

PTemp [I] = m _ pBuffer [I]

}

Delete [] m_pBuffer

M_pBuffer = NULL

M_pBuffer = pTemp

}

M _ pBuffer [m _ iTop] = elem

Massively iTopstructures +

}

Void MyStack::pop (int& elem)

{

Massively Topvilli-

Elem = m _ pBuffer [m _ iTop]

}

Void MyStack::clearStack ()

{

M_iTop = 0

}

Bool MyStack::isEmpty ()

{

If (m_iTop = = 0)

{

Return true

}

Return false

}

Int MyStack::stackLength ()

{

Return m_iTop

}

Void MyStack::stackTraverse (bool isFromBottom)

{

/ / start traversing at the bottom of the stack

If (isFromBottom)

{

For (int I = 0 ~ (th) I < m ~ (top) (s)

{

Cout

Tags: Space element size dynamic content pointing article clocking principle copy pointer is in more configuration good practical number at the same time address article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Huawei Xiaomi Shulou Technology