按照严蔚敏数据结构实现栈的基本操作
Stack.h文件
#include<iostream>
#include<cstdlib>
using namespace std;
#define STACK_INIT_SIZE 100//存储空间初始分配量
#define STACKINCREMENT 10//存储空间分配量
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
//#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
//栈的顺序结构表示
typedef struct {
ElemType* base;
ElemType* top;
int stacksize;
}SqStack;
//栈的初始化
Status InitStack(SqStack& S);
//销毁栈
Status DestroyStack(SqStack& S);
//栈置空
Status ClearStack(SqStack& S);
//判断栈空
Status StackEmpty(SqStack S);
//返回元素个数
Status StackLength(SqStack S);
//返回栈顶元素
Status GetTop(SqStack S, ElemType& e);
//插入元素
Status Push(SqStack& S, ElemType e);
//删除栈顶元素
Status Pop(SqStack& S, ElemType& e);
//遍历栈
Status StackTraverse(SqStack S);
Stack.cpp文件
//初始化
Status InitStack(SqStack& S) {
S.base = new ElemType[STACK_INIT_SIZE];
if (!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
//销毁栈
Status DestroyStack(SqStack& S) {
S.top = NULL;
S.stacksize = 0;
delete(S.base);
return OK;
}
//栈置空
Status ClearStack(SqStack& S) {
S.top = S.base;
return OK;
}
//判断栈空
Status StackEmpty(SqStack S) {
if (S.top == S.base) {
return TRUE;
}
else {
return FALSE;
}
}
//返回元素个数
Status StackLength(SqStack S) {
if (S.top == S.base) {
return FALSE;
}
else {
return(S.top - S.base);
}
}
//返回栈顶元素
Status GetTop(SqStack S, ElemType& e) {
if (StackLength) {
e = *(S.top - 1);
return e;
}
else {
return ERROR;
}
}
//插入元素
Status Push(SqStack& S, ElemType e) {
if (S.top - S.base >= S.stacksize ) {
S.base = (ElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType));//这里其实有问题,realloc和new不能同时用,realloc有可能返回null指针,导致内存泄露
if (!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
//删除栈顶元素
Status Pop(SqStack& S, ElemType& e) {
if (StackEmpty(S)) {
return ERROR;
}
else {
--S.top;
e = *S.top;
return e;
}
}
//遍历栈
Status StackTraverse(SqStack S) {
if (S.base == NULL) {
return NULL;
}
if (S.top == S.base) {
cout << "栈为空,无元素" << endl;
}
ElemType* p = S.top;
while (p > S.base) {
cout << *(p - 1)<<" ";
p--;
}
cout << endl;
return OK;
}
源.cpp文件
int main() {
SqStack S;
InitStack(S);
while (true) {
cout << "栈的基本操作使用!" << endl;
cout << "***1.销毁栈" << endl;
cout << "***2.清空栈" << endl;
cout << "***3.栈判空" << endl;
cout << "***4.求栈长" << endl;
cout << "***5.返栈顶" << endl;
cout << "***6.插入栈" << endl;
cout << "***7.删栈顶" << endl;
cout << "***8.遍历栈" << endl;
cout << "--请输入你的选择:" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
if (DestroyStack(S)) {
cout << "栈销毁成功!" << endl;
}
break;
}
case 2: {
if (ClearStack(S)) {
cout << "栈清空成功!" << endl;
}
break;
}
case 3: {
if (StackEmpty(S)) {
cout << "栈为空!" << endl;
}
else {
cout << "栈不为空!" << endl;
}
break;
}
case 4: {
if (StackLength(S)) {
cout << "栈的长度为:" << StackLength(S) << endl;
}
else {
cout << "栈的长度为0!" << endl;
}
break;
}
case 5: {
int e = 0;
if (StackEmpty(S)) {
cout << "无栈顶元素,栈为空!" << endl;
}
else {
cout << "栈顶元素为" << GetTop(S, e) << endl;
}
break;
}
case 6: {
cout << "请输入要插入的元素为:" << endl;
int insertElem = 0;
cin >> insertElem;
if (Push(S, insertElem)) {
cout << "入栈成功!" << endl;
}
else {
cout << "入栈失败!" << endl;
}
break;
}
case 7: {
int e = 0;
if (StackEmpty(S)) {
cout << "删除失败!" << endl;
}
else {
cout << "删除成功!" << endl;
cout << "删除的元素为:" << Pop(S, e) << endl;
}
break;
}
case 8: {
StackTraverse(S);
break;
}
default:
cout << "请输入正确的选项!" << endl;
break;
}
cout << "请问是否继续操作:\n1.是\t2.否" << endl;
int yes = 0;
cin >> yes;
if (yes == 2)
break;
system("pause");
system("cls");
}
}```
**写完代码提出疑问:**
初始化时top指针和base指针均指向栈底,假设顺序栈容量为10,已经存入9个元素,top指针此时指向栈顶元素的后一个位置,也就是最后一个空位置。1. 那么此时栈是否已满了呢?2. 如果没满,继续插入的话,是否需要申请新空间?如果不需要申请的话,那么top指针此时指向位置在哪?3. new和delete以及malloc和free和realloc之间的区别,是否能够混用?
标签:Status,return,SqStack,ElemType,top,base,基本操作
From: https://www.cnblogs.com/HanXiaoCao/p/16909081.html