当前位置:网站首页>Two implementation methods of stack
Two implementation methods of stack
2022-06-24 22:00:00 【Weng Weiqiang】
Dynamic array implementation :
#define CURL_STATICLIB
# include<iostream>
# include<string>
#include<assert.h>
using namespace std;
template<typename T>
class Mystack
{
private:
T* arr;
int _count;
int cap;// Capacity
int length;
public:
Mystack<T>()
{
this->cap = 1024;
_count = 0;
arr = new T[cap];
length = 0;
}
Mystack<T>(int cap)
{
this->cap = cap;
_count = 0;
arr = new T[cap];
length = 0;
}
~Mystack()
{
delete[]arr;
}
void push(T val)
{
assert(_count < cap);// If it is true, no error will be reported , Avoid out of bounds access
arr[_count] = val;
++_count;
++length;
}
T pop()
{
assert(length>0);
--length;
return arr[--_count];
}
T top()
{
return arr[_count-1];//return arr[_count-1]
}
bool empty()
{
return length == 0;
}
int size()
{
return length;
}
int capacity()
{
return cap;
}
T get_count(int num)
{
return arr[num];
}
};
int main()
{
Mystack<string>s;
s.push("abc");
s.push("def");
s.push("ghi");
s.push("jkl");
s.push("mno");
int len = s.size();
for (int i = 0; i <len; i++)
{
cout << s.top() << endl;
s.pop();
}
if (s.empty())
{
cout << "I like coding" << endl;
}
return 0;
}
Linked list implementation :
1. In the process of stacking , Use inverse interpolation ;
That is, fill elements between the head node and the first node :
temp->next = topNode;
topNode = temp;// Use the head insertion method to realize the built-in of the linked list 2. Head insertion and tail insertion
// The tail interpolation
rear->next=newNode
rear=rear->next
// The first interpolation
temp->next=head->next
head->next=tempComplete code :
#define CURL_STATICLIB
# include<iostream>
# include<string>
#include<assert.h>
using namespace std;
template<typename T>
class Node
{
public:
Node<T>* last;
Node<T>* next;
T val;
Node(T val) :val(val), next(NULL), last(NULL) {}
~Node() { delete next; delete last; }
};
template <typename T>
class Mystack
{
private:
Node<T>* topNode;
int _size;
public:
Mystack() :topNode(NULL), _size(0) {}
~Mystack() { delete topNode; }
void push(T x)
{
if (NULL == topNode) { topNode = new Node<T>(x); topNode->last = NULL; ++_size; return; }
Node<T>* temp = new Node<T>(x);
temp->next = topNode;// Inverse interpolation
topNode = temp;
//temp->next=topNode;topNode=temp;
//rear->next=newNode;rear=rear->next Of queues and stacks pop() It's all location-based movement Insert first and then determine the position movement
//newNode->next=head->next;
//head->next=newNode;
}
void pop()
{
assert(NULL != topNode);
Node<T>* temp = topNode;
topNode = topNode->next;
--_size;
}
T top()
{
assert(NULL != topNode);
return topNode->val;
}
void size()
{
return _size;
}
bool empty()
{
return topNode == NULL;
}
};
int main()
{
Mystack<string>s;
s.push("passion");
s.push("optimistic");
s.push("Tencent");
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
}边栏推荐
- Prompt that the device has no permission when using ADB to connect to the device
- ST表+二分
- Want to be a test leader, do you know these 6 skills?
- Minimum spanning tree based on Kruskal
- Detailed installation and use of performance test tool wrk
- Machine learning: gradient descent method
- 03---增反膜
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
- Object.defineProperty和Reflect.defineProperty的容错问题
- Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
猜你喜欢

【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
![[notes of Wu Enda] multivariable linear regression](/img/b1/60a702aaca58b0afa57ac2f552dabf.png)
[notes of Wu Enda] multivariable linear regression

最大流问题

leetcode-201_2021_10_17

Development trend and path of SaaS industry in China

EasyBypass

openGauss内核:简单查询的执行

【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割

直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓

壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
随机推荐
网络层 && IP
2022 international women engineers' Day: Dyson design award shows women's design strength
Several schemes of traffic exposure in kubernetes cluster
socket(2)
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
Maximum flow problem
Introduce the overall process of bootloader, PM, kernel and system startup
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
拖动拖动拖动
STL+树
数据链路层 && 一些其他的协议or技术
最大流问题
双链表实现
基于 KubeSphere 的分级管理实践
leetcode_ one thousand three hundred and sixty-five
03--- antireflective film
Multithreaded finalization
985 test engineer is hanged. Who is more important in terms of education and experience?
TKKC round#3
C language - keyword 1