From f2a86f124ffb35eded479fe8b53de034d861a9b5 Mon Sep 17 00:00:00 2001 From: abc <13579519+wm2109020144@user.noreply.gitee.com> Date: Mon, 18 Dec 2023 11:22:57 +0000 Subject: [PATCH] =?UTF-8?q?add=202109020144/chap3/=E5=AE=9E=E9=AA=8C1.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: abc <13579519+wm2109020144@user.noreply.gitee.com> --- .../chap3/\345\256\236\351\252\2141.cpp" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "2109020144/chap3/\345\256\236\351\252\2141.cpp" diff --git "a/2109020144/chap3/\345\256\236\351\252\2141.cpp" "b/2109020144/chap3/\345\256\236\351\252\2141.cpp" new file mode 100644 index 00000000..3a4be03a --- /dev/null +++ "b/2109020144/chap3/\345\256\236\351\252\2141.cpp" @@ -0,0 +1,61 @@ +#include +using namespace std; +#define maxsize 10 + +typedef struct {//定义栈 + char data[maxsize]; + int top; +}sq; +void stackinit(sq& s){//初始化栈 + s.top = -1; +} +bool stackempty(sq s){//判断栈空 + if (s.top == -1) return true; + else return false; +} +bool push(sq& s, char x){//放入元素 + if (s.top == maxsize - 1) return false; + s.data[++s.top] = x; + return true; +} +void pop(sq& s){//出栈 + if (s.top == -1) cout << "栈空" << endl; + s.top--; +} +char gettop(sq s){//获取栈顶 + int x; + if (s.top == -1) return -1; + x = s.data[s.top]; + return x; +} +void destorystack(sq& s) {//释放栈 + if (s.top != -1) free(s.data); + s.top = -1; +} + +int main() { + sq s; + cout << "创建栈s" << endl; + stackinit(s);//初始化栈 + if (stackempty(s)) { cout << "当前栈s为空" << endl; } + else { cout << "当前栈s不为空" << endl; } + push(s, 'a'); + push(s, 'b'); + push(s, 'c'); + push(s, 'd'); + push(s, 'e'); + cout << "输入元素a,b,c,d,e" << endl; + if (stackempty(s)) { cout << "当前栈s为空" << endl; } + else { cout << "当前栈s不为空" << endl; } + cout << "当前的出栈序列为:"; + while (!stackempty(s)) + { + cout << gettop(s) << " "; + s.top--; + } + cout << endl; + if (stackempty(s)) { cout << "当前栈s为空" << endl; } + else { cout << "当前栈s不为空" << endl; } + destorystack(s);//释放栈 + return 0; +} \ No newline at end of file -- Gitee