Archived
1
0
This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
SomeLab/CPP1/CPP1.cpp
2025-10-24 17:19:46 +08:00

84 lines
1.3 KiB
C++
Raw 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.
#include<stdio.h>;
#include<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define ElemType int
// 定义结构体
typedef struct
{
ElemType *elem;
int BSS;
int size;
int increment;
} SqStack;
// 初始化栈
int InitStack(SqStack& S, int size, int inc)
{
S.elem = (ElemType*)malloc(size * sizeof(ElemType));
if (NULL == S.elem) return OVERFLOW;
S.BSS = 0;
S.size = size;
S.increment = inc;
return OK;
}
// 入栈
int Push(SqStack& S, ElemType e)
{
int* newbase;
if (S.BSS >= S.size) {
newbase = (ElemType*)realloc(S.elem, (S.size + S.increment) * sizeof(ElemType));
if (NULL == newbase) return OVERFLOW;
S.elem = newbase;
S.size += S.increment;
}
S.elem[S.BSS++] = e;
return OK;
}
// 倒置元素
void ReverseStack(SqStack& S)
{
int i = 0, j = S.BSS - 1;
while (i < j) {
int temp = S.elem[i];
S.elem[i] = S.elem[j];
S.elem[j] = temp;
i++;
j--;
}
}
// 打印
void PrintStack(SqStack& S)
{
for (int i = S.BSS - 1; i >= 0; i--)
{
printf("%d ", S.elem[i]);
}
}
int main()
{
SqStack BSS;
InitStack(BSS,50,1);
//填充元素从0-49的整数
for (int i = 8562;i <= 8812;i += 5) {
Push(BSS, i);
}
//打印原栈
printf("原栈:\n");
PrintStack(BSS);
printf("\n");
//反转并打印
printf("反转后栈:\n");
ReverseStack(BSS);
PrintStack(BSS);
return 0;
}