当前位置:网站首页>栈的两种实现方式
栈的两种实现方式
2022-06-24 19:28:00 【翁炜强】
动态数组实现:
#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;//容量
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);//假如为真就不会报错,避免越界限访问
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;
}
链表实现:
1.在入栈的过程中,用到反插法;
即在头节点和第一个节点之间进行填充元素:
temp->next = topNode;
topNode = temp;//用头插法实现链表的内置
2.头插法和尾插法
//尾插法
rear->next=newNode
rear=rear->next
//头插法
temp->next=head->next
head->next=temp
完整代码:
#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;//反插法
topNode = temp;
//temp->next=topNode;topNode=temp;
//rear->next=newNode;rear=rear->next 队列和栈的pop()都是基于位置的移动 先插入后确定位置移动
//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();
}
}
边栏推荐
- 直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓
- Debugging Analysis of Kernel panics and Kernel oopses using System Map
- socket done
- Pattern recognition - 9 Decision tree
- 虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
- 【吴恩达笔记】多变量线性回归
- TDengine可通过数据同步工具 DataX读写
- Bld3 getting started UI
- TCP Jprobe utilization problem location
- Introduce the overall process of bootloader, PM, kernel and system startup
猜你喜欢
Tutorial on obtaining JD cookies by mobile browser
Failed to open after installing Charles without any prompt
推荐模型之多任务模型:ESMM、MMOE
Memcached full profiling – 1 Fundamentals of memcached
Alibaba cloud lightweight servers open designated ports
Multi view function in blender
Volcano becomes spark default batch scheduler
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
2022 international women engineers' Day: Dyson design award shows women's design strength
VSCode无网环境快速迁移开发环境(VIP典藏版)
随机推荐
leetcode1720_2021-10-14
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
Pattern recognition - 0 introduction
Functional analysis of ebpf tracepoint
Why are life science enterprises on the cloud in succession?
how to install clustershell
Handwritten RPC the next day -- review of some knowledge
Alibaba cloud lightweight servers open designated ports
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
Slider控制Animator动画播放进度
Rewrite, maplocal and maplocal operations of Charles
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
去掉录屏提醒(七牛云demo)
TCP specifies the source port
leetcode_191_2021-10-15
手动事务的几个类
Li Kou daily question - day 25 -496 Next larger element I
ping: www.baidu. Com: unknown name or service
memcached全面剖析–2. 理解memcached的内存存储
如何做到全彩户外LED显示屏节能环保