实现栈类Stack
This commit is contained in:
@@ -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)
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include "Stack.h"
|
||||||
|
#include<iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include"Stack.h"
|
||||||
|
#include<iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user