In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.