qfedu-cpp-level/day7/homework/h3.cpp

88 lines
2.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计一个模板类 Stack实现基本的堆栈功能包括入栈push、出栈pop、获取栈顶元素top和判断栈是否为空isEmpty等操作。
// 【提示】Stack栈结构的特性是FILO(先进后出)
#include <iostream>
using namespace std;
template <typename T>
class Stack
{
private:
int capacity; // 容量
int size; // 已存数量计数器
T *arr; // 指向数组的指针
public:
Stack(int capacity)
{
this->capacity = capacity;
this->arr = new T[capacity];
this->size = 0;
}
Stack(const Stack<T> &other)
{
this->capacity = other.capacity;
this->size = other.size;
this->arr = new T[capacity];
for (int i = 0; i < size; i++)
{
this->arr[i] = other.arr[i];
}
}
~Stack()
{
delete[] arr;
}
public:
// & 用于返回引用,支持链式调用
Stack &push(T val)
{
if (size == capacity)
{
cout << "栈已经满了!" << endl;
return *this;
}
arr[size++] = val;
return *this; // 返回自身,支持链式调用
}
T pop()
{
if (size == 0)
{
cout << "栈已经空了!" << endl;
return -1;
}
return arr[--size]; // 先减再取因为size是计数器指向下一个空位
}
T top()
{
if (size == 0)
{
cout << "栈已经空了!" << endl;
return -1;
}
return arr[size - 1]; // 直接取顶部元素
}
string isEmpty()
{
return size == 0 ? "栈空" : "栈未空"; // 判断计数器是否为 0为 0 表示栈空
}
};
int main()
{
Stack<int> s(5);
s.push(1).push(2).push(3).push(4).push(5);
cout << s.pop() << endl; // 5
cout << s.pop() << endl; // 4
cout << s.top() << endl; // 3
cout << s.isEmpty() << endl; // 栈未空
cout << s.pop() << endl; // 3
cout << s.pop() << endl; // 2
cout << s.pop() << endl; // 1
cout << s.isEmpty() << endl; // 栈空
cout << s.pop() << endl; // 栈已经空了! -1
return 0;
}