顺序栈结构方案一: 创建栈底指针base、栈顶指针top
#include <iostream>
#include <Windows.h>
using namespace std;
#define MaxSize 50
typedef int SElemType;
typedef struct
{
SElemType* base;
SElemType* top;
int stackSize;
}SqStack;
bool InitStack(SqStack& stack);
bool StackEmpty(SqStack& stack);
bool StackFull(SqStack& stack);
int StackLength(SqStack& stack);
bool PushStack(SqStack& stack, SElemType value);
bool PopStack(SqStack& stack, SElemType& vlaue);
bool GetStack(SqStack& stack, SElemType& value);
void DestoryStack(SqStack& stack);
bool InitStack(SqStack& stack)
{
stack.base = new SElemType[MaxSize];
if (!stack.base)
{
return false;
}
stack.top = stack.base;
stack.stackSize = MaxSize;
return true;
}
bool StackEmpty(SqStack& stack)
{
if(stack.top == stack.base)
{
return true;
}
else
{
return false;
}
}
bool StackFull(SqStack& stack)
{
if ((stack.top - stack.base) == MaxSize)
{
return true;
}
else
{
return false;
}
}
bool PushStack(SqStack& stack, SElemType value)
{
if (StackFull(stack))
{
cout << "栈满" << endl;
return false;
}
*stack.top = value;
stack.top++;
return true;
}
bool PopStack(SqStack& stack, SElemType &value)
{
if (StackEmpty(stack))
{
cout << "栈为空" << endl;
return false;
}
stack.top--;
value = *stack.top;
return true;
}
bool GetStack(SqStack& stack, SElemType& value)
{
if (StackEmpty(stack))
{
cout << "栈为空" << endl;
return false;
}
value = *(stack.top - 1);
return true;
}
void DestoryStack(SqStack& stack)
{
if (stack.base)
{
delete stack.base;
stack.top = NULL;
stack.base = NULL;
stack.stackSize = 0;
cout << "栈已释放内存!" << endl;
}
}
int StackLength(SqStack& stack)
{
return (stack.top - stack.base);
}
int main10_1()
{
SqStack stack;
InitStack(stack);
int number = 0;
int value = 0;
cout << "请输入需要插入的元素个数:";
cin >> number;
while ((number--) > 0)
{
cin >> value;
PushStack(stack, value);
}
GetStack(stack, value);
cout << "当前栈顶元素是:" << value << endl;
cout << "当前栈的元素个数是:" << StackLength(stack) << endl;
cout << "出栈顺序:" << endl;
while (!StackEmpty(stack))
{
PopStack(stack, value);
cout << value << " ";
}
cout << endl;
DestoryStack(stack);
system("pause");
return 0;
}
顺序栈结构方案二:创建栈底指针、栈顶位置
#include<iostream>
#include<Windows.h>
using namespace std;
#define MaxSize 128
typedef int SElemType;
typedef struct
{
SElemType* base;
int top;
}SqStack;
bool InitStack(SqStack& stack)
{
stack.base = new SElemType[MaxSize];
if (!stack.base)
{
return false;
}
stack.top = 0;
return true;
}
bool StackEmpty(SqStack& stack)
{
if (stack.top == 0)
{
return true;
}
return false;
}
int GetStackSize(SqStack& stack)
{
return stack.top;
}
bool StackFull(SqStack& stack)
{
if (stack.top == MaxSize)
{
return true;
}
return false;
}
bool PushStack(SqStack& stack, SElemType value)
{
if (StackFull(stack))
{
cout << "栈满" << endl;
return false;
}
stack.base[stack.top] = value;
stack.top++;
return true;
}
bool PopStack(SqStack& stack, SElemType &value)
{
if (StackEmpty(stack))
{
cout << "栈为空" << endl;
return false;
}
stack.top--;
value = stack.base[stack.top];
return true;
}
bool GetStack(SqStack& stack, SElemType& value)
{
if (StackEmpty(stack))
{
cout << "栈为空" << endl;
return false;
}
value = stack.base[stack.top - 1];
return true;
}
void DeleteStack(SqStack& stack)
{
if (stack.base)
{
delete stack.base;
stack.top = 0;
stack.base = NULL;
cout << "栈已释放内存!" << endl;
}
}
int main()
{
SqStack stack;
InitStack(stack);
int number = 0;
int value = 0;
cout << "请输入需要插入的元素个数:";
cin >> number;
while (number > 0)
{
cin >> value;
PushStack(stack, value);
number--;
}
GetStack(stack, value);
cout << "当前栈顶的元素是:" << value << endl;
cout << "当前栈的元素个数是:" << GetStackSize(stack) << endl;
cout << "出栈顺序:" << endl;
while (!StackEmpty(stack))
{
PopStack(stack, value);
cout << value << " ";
}
DeleteStack(stack);
system("pause");
return 0;
}