线性表-顺序栈
1: //sqstack.cpp
2: #ifndef SQSTACK_CPP_CPP3: #define SQSTACK_CPP_CPP4:5: #include "sqstack.h"
6: #include <iostream>7:8: template<class T>9: SqStack<T>::SqStack():10: top(-1),maxsize(MAXSIZE)11: {12: stack = new T[maxsize];
13: if(stack == NULL)
14: {15: cerr<<"Fail!"<<endl;
16: exit(1);17: }18: }19:20: template<class T>21: SqStack<T>::SqStack(int size):
22: top(-1),maxsize(size)23: {24: stack = new T[maxsize];
25: if(stack == NULL)
26: {27: cerr<<"Fail!"<<endl;
28: exit(1);29: }30: }31:32: template<class T>33: SqStack<T>::SqStack(T data[],int size):
34: top(-1),maxsize(size)35: {36: stack = new T[maxsize];
37: if(stack == NULL)
38: {39: cerr<<"Fail!"<<endl;
40: exit(1);41: }42:43: for(int i = 0; i < maxsize; ++i)44: {45: stack[i] = data[i];46: }47: top += maxsize;48: }49:50: template<class T>51: SqStack<T>::~SqStack()52: {53: delete [] stack;
54: }55:56: template<class T>57: void SqStack<T>::Push(const T &item) //压栈58: {59: if(Full())
60: {61: cerr<<"stack is full."<<endl;
62: exit(1);63: }64:65: top++;66: stack[top] = item;67: }68:69: template<class T>70: T SqStack<T>::Pop()//弹栈
71: {72: if(Empty())
73: {74: cerr<<"stack is empty."<<endl;
75: exit(1);76: }77:78: T data = stack[top];79: top--;80: return data;
81: }82:83: template<class T>84: T SqStack<T>::GetTop()//返回栈顶元素
85: {86: if(Empty())
87: {88: cerr<<"stack is empty."<<endl;
89: exit(1);90: }91: return stack[top];
92: }93:94: template<class T>95: bool SqStack<T>::Empty() const96: {97: return (-1 == top);
98: }99:100: template<class T>101: bool SqStack<T>::Full() const102: {103: return (maxsize -1 == top);
104: }105:106: template<class T>107: void SqStack<T>::Clear()
108: {109: top = -1;110: }111:112:113: #endif
1: //sqstack.h
2:3: #ifndef SQSTACK_H_H4: #define SQSTACK_H_H5:6: #include <iostream>7: using namespace std;8:9: #define MAXSIZE 1010:11: template<class T>12: class SqStack
13: {14: int top;
15: T * stack;16: int maxsize;
17: public:
18: SqStack();19: SqStack(int size);
20: SqStack(T data[],int size);
21: ~SqStack();22:23: void Push(const T &item);24: T Pop();25: T GetTop();26:27: bool Empty() const;28: bool Full() const;29:30: void Clear();
31:32: };33:34: #endif
1: //test.cpp
2:3: #include "sqstack.h"
4: #include "sqstack.cpp"
5: #include <iostream>6:7: using namespace std;8:9: int main(int argc, char * argv[])10: {11: int data[] = {1,2,3,4,5,6,7,8,9,10};
12: SqStack<int> stack(data,10);
13:14: while(!stack.Empty())
15: {16: cout<<stack.Pop()<<endl;17: }18: return 0;
19: }
另一个问题:两栈共享空间
1: template<class T>2: class SqStack
3: {4: int top1;
5: int top2;
6: T * stack;7: int maxsize;
8: public:
9: ...10: };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器