From 935832dbe8a1b2db8fccb8de8a22e9ccd028f76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Wed, 23 Oct 2024 20:59:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=A0=88=E7=B1=BBStack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework3/CMakeLists.txt | 9 +++++++++ homework3/Stack.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ homework3/Stack.h | 21 ++++++++++++++++++++ homework3/test1.cpp | 8 ++++++++ 4 files changed, 79 insertions(+) create mode 100644 homework3/CMakeLists.txt create mode 100644 homework3/Stack.cpp create mode 100644 homework3/Stack.h create mode 100644 homework3/test1.cpp diff --git a/homework3/CMakeLists.txt b/homework3/CMakeLists.txt new file mode 100644 index 0000000..7806f16 --- /dev/null +++ b/homework3/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.29) +project(homework3) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(test1 + Stack.h + Stack.cpp + test1.cpp) diff --git a/homework3/Stack.cpp b/homework3/Stack.cpp new file mode 100644 index 0000000..66c3450 --- /dev/null +++ b/homework3/Stack.cpp @@ -0,0 +1,41 @@ +#include "Stack.h" +#include + +using namespace std; + +bool isEmpty(stack &s) { + if (s.top == -1) { + return true; + } + return false; +} + +bool isFull(stack &s) { + if (s.top == MAX_SIZE) { + return true; + } + return false; +} + +void initStack(stack &s) { + s.top = -1; +} + + +void push(stack &s, int e) { + if (isFull(s)) { + cout << "此栈已满" << endl; + return; + } + s.top++; + s.data[s.top] = e; +} + +int pop(stack &s) { + if (isEmpty(s)) { + cout << "此栈为空" << endl; + return NULL; + } + int e = s.data[s.top]; + return e; +} diff --git a/homework3/Stack.h b/homework3/Stack.h new file mode 100644 index 0000000..62a8fd5 --- /dev/null +++ b/homework3/Stack.h @@ -0,0 +1,21 @@ +#ifndef STACK_H +#define STACK_H + +#define MAX_SIZE 100 + +typedef struct stack { + int data[MAX_SIZE]; + int top; +} stack; + +bool isEmpty(stack &s); + +bool isFull(stack &s); + +void initStack(stack &s); + +void push(stack &s, int e); + +int pop(stack &s); + +#endif //STACK_H diff --git a/homework3/test1.cpp b/homework3/test1.cpp new file mode 100644 index 0000000..7a19bbc --- /dev/null +++ b/homework3/test1.cpp @@ -0,0 +1,8 @@ +#include"Stack.h" +#include +using namespace std; + +int main() { + + return 0; +}