当前位置:网站首页>Implement a fixed capacity stack
Implement a fixed capacity stack
2022-07-05 05:35:00 【Raise items】
List of articles
Constant volume string stack
Consider implementing a Fixed capacity The stack , This stack can only store character string .
Realization function
fix_string_stack.h
#ifndef FIX_STRING_STACK
#define FIX_STRING_STACK
#include <vector>
#include <string>
using std::vector;
using std::string;
class FixStringStack {
public:
FixStringStack(size_t n);
void Push(const string &str);
string Pop();
bool IsEmpty() const;
size_t Size() const;
private:
vector<string> vStr;
size_t N;
};
#endif
fix_string_stack.cpp
#include "fix_string_stack.h"
FixStringStack::FixStringStack(size_t n) : vStr(n), N(0) {
}
void FixStringStack::Push(const string &str)
{
vStr[N++] = str;
}
string FixStringStack::Pop()
{
return vStr[--N];
}
bool FixStringStack::IsEmpty() const
{
return N == 0;
}
size_t FixStringStack::Size() const
{
return N;
}
Write tests
fix_string_stack_test.h
#ifndef FIX_STRING_TEST_H
#define FIX_STRING_TEST_H
void FixStringStackTest();
#endif
fix_string_stack_test.cpp
#include "fix_string_stack.h"
#include <iostream>
#include <string>
void FixStringStackTest()
{
FixStringStack st(100);
std::string str;
while (std::cin >> str) {
if (str != string("-")) {
st.Push(str);
} else if (!st.IsEmpty()) {
std::cout << st.Pop() << std::endl;
}
}
std::cout << st.Size() << std::endl;
}
function
main.cpp
#include "fix_string_stack_test.h"
int main()
{
FixStringStackTest();
return 0;
}
Running results :
Fixed volume stack advantage Every time Push
and Pop
Required for operation Time is independent of the length of the stack .
FixStringStack
The disadvantages are also obvious :(1)
Can only store string
object .(2)
Before use, you must Estimate the maximum capacity of the stack , It may cause Memory waste .(3)
Push
Time detection Is the stack full .
Let's ignore vector The ability to dynamically allocate memory , Think of it as an ordinary array .
Constant volume stack
For the above shortcomings , Use the following method to optimize , And try to ensure that its advantages are still .(1)
Generic Programming .(2)
Time dynamic Allocate memory .
Realization
fix_stack.h
#ifndef FIX_STACK
#define FIX_STACK
#include <cstddef>
template <typename Item>
class FixStack {
public:
FixStack(size_t cap);
~FixStack();
void Push(const Item &item);
Item Pop();
bool IsEmpty() const;
size_t Size() const;
private:
void ReSize(size_t max);
size_t cap_;
size_t sz_;
Item *data_;
};
template <typename Item>
FixStack<Item>::FixStack(size_t cap) : cap_(cap), sz_(0)
{
data_ = new Item[cap_];
}
template <typename Item>
FixStack<Item>::~FixStack()
{
delete[] data_;
data_ = nullptr;
}
template <typename Item>
void FixStack<Item>::ReSize(size_t max)
{
Item *newData = new Item[max];
for (size_t i = 0; i < sz_; ++i) {
newData[i] = data_[i];
}
data_ = newData;
cap_ = max;
}
template <typename Item>
void FixStack<Item>::Push(const Item &item)
{
if (sz_ == cap_) {
ReSize( 2 * cap_);
}
data_[sz_++] = item;
}
template <typename Item>
Item FixStack<Item>::Pop()
{
return data_[--sz_];
}
template <typename Item>
bool FixStack<Item>::IsEmpty() const
{
return sz_ == 0;
}
template <typename Item>
size_t FixStack<Item>::Size() const
{
return sz_;
}
#endif
C++
in Templates The definition and implementation of classes must be written in The same In the document .
Dynamically allocate memory over time :Push
when , If the memory for saving data is full , Just Reallocate current capacity 2 Times more memory , And copy the existing data into the new memory .
Destructor : When the stack object dies , Free the heap memory it requested , Avoid memory leaks .
test
fix_stack_test.h
#ifndef FIX_STACK_TEST
#define FIX_STACK_TEST
void FixStackTest();
#endif
fix_stack_test.cpp
#include "fix_stack.h"
#include <string>
#include <iostream>
void FixStackTest()
{
FixStack<std::string> st(1);
std::string str;
while (std::cin >> str) {
if (str != std::string("-")) {
st.Push(str);
} else if (!st.IsEmpty()) {
std::cout << st.Pop() << std::endl;
}
}
std::cout << st.Size() << std::endl;
}
function
main.cpp
#include "fix_stack_test.h"
int main()
{
FixStackTest();
return 0;
}
Running results :
边栏推荐
- Introduction to memory layout of FVP and Juno platforms
- Sword finger offer 05 Replace spaces
- Codeforces Round #716 (Div. 2) D. Cut and Stick
- Add level control and logger level control of Solon logging plug-in
- object serialization
- On-off and on-off of quality system construction
- kubeadm系列-01-preflight究竟有多少check
- [to be continued] [depth first search] 547 Number of provinces
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Reader writer model
猜你喜欢
Graduation project of game mall
剑指 Offer 53 - II. 0~n-1中缺失的数字
Binary search basis
剑指 Offer 06.从头到尾打印链表
挂起等待锁 vs 自旋锁(两者的使用场合)
Service fusing hystrix
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
On-off and on-off of quality system construction
读者写者模型
Sword finger offer 05 Replace spaces
随机推荐
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
CF1634 F. Fibonacci Additions
卷积神经网络简介
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
二十六、文件系统API(设备在应用间的共享;目录和文件API)
MySQL数据库(一)
Sword finger offer 53 - ii Missing numbers from 0 to n-1
The number of enclaves
Codeforces Round #716 (Div. 2) D. Cut and Stick
kubeadm系列-01-preflight究竟有多少check
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Reflection summary of Haut OJ freshmen on Wednesday
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Introduction to convolutional neural network
【实战技能】如何做好技术培训?
Pointnet++ learning
A new micro ORM open source framework
Over fitting and regularization
每日一题-搜索二维矩阵ps二维数组的查找