mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-09 13:26:34 +08:00
support bazel complie this project and format code.
This commit is contained in:
60
practical_exercises/key_exercises/stack.cpp
Normal file
60
practical_exercises/key_exercises/stack.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/* 类模板之栈.cpp */
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T, int MAXSIZE>
|
||||
|
||||
class Stack {
|
||||
public:
|
||||
Stack() {}
|
||||
void init() { top = -1; }
|
||||
bool isFull() {
|
||||
if (top >= MAXSIZE - 1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
bool isEmpty() {
|
||||
if (top == -1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
void push(T e);
|
||||
T pop();
|
||||
|
||||
private:
|
||||
T elems[MAXSIZE];
|
||||
int top;
|
||||
};
|
||||
|
||||
template <typename T, int MAXSIZE> void Stack<T, MAXSIZE>::push(T e) {
|
||||
if (!isFull()) {
|
||||
elems[++top] = e;
|
||||
} else {
|
||||
cout << "栈已满,请不要再加入元素!";
|
||||
return;
|
||||
}
|
||||
}
|
||||
template <typename T, int MAXSIZE> T Stack<T, MAXSIZE>::pop() {
|
||||
if (!isEmpty()) {
|
||||
return elems[top--];
|
||||
} else {
|
||||
cout << "栈已空,请不要再弹出元素!";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
Stack<int, 10> s1;
|
||||
s1.init();
|
||||
int i;
|
||||
for (i = 1; i < 11; i++)
|
||||
s1.push(i);
|
||||
for (i = 1; i < 11; i++)
|
||||
cout << s1.pop() << "\t";
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user