數據結構之順序棧(C++版)
#include <iostream>
#include <stdlib.h>
#define MAXLISTSIZE 100 //預設的存儲空間最大容量
using namespace std;
typedef string ElemType;
typedef struct{
ElemType *base; //存儲空間基址
int top; //棧頂指針
int stacksize; //允許的最大存儲空間以元素為單位
}Stack;
void InitStack(Stack &S);
void DestroyStack(Stack &S);
void Push(Stack &S);
void Pop(Stack &S);
void GetTop(Stack S);
void StackLength(Stack S);
void StackEmpty(Stack S);
int main(void)
{
Stack S;
int z;
cout << "+---------------------------+" << ‘\n‘;
cout << "|----------順序棧-----------|" << ‘\n‘;
cout << "+---------------------------+" << ‘\n‘;
cout << "提示:為保證您的操作得到保存,請按正常順序退出系統^_^" << ‘\n‘;
do
{
cout << ‘\n‘ << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"--------------------------------" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ 主菜單 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+--------------------------------" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [1]----順序棧初始化操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [2]----順序棧的銷毀操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [3]----順序棧的入棧操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [4]----順序棧的出棧操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [5]----順序棧取棧頂元素 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [6]----順序棧求棧長操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [7]----順序棧的判空操作 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"+ [0]----退出系統 |" << ‘\n‘;
cout << ‘\t‘ << ‘\t‘ << ‘\t‘ <<"-----------------------------------" << ‘\n‘;
cout << "請輸入您的選擇:";
cin >> z;
switch(z)
{
case 0 : break;
case 1 : InitStack(S);break;
case 2 : DestroyStack(S);break;
case 3 : Push(S);break;
case 4 : Pop(S);break;
case 5 : GetTop(S);break;
case 6 : StackLength(S);break;
case 7 : StackEmpty(S);break;
default:cout << "無效選項!" << ‘\n‘;system("pause");
}
}
while(z!= 0);
}
void InitStack (Stack &S)
{
// 構造一個最大存儲容量為 maxsize 的空棧 S
int maxsize;
cout << "請輸入棧的最大儲存容量:";
cin >> maxsize;
if (maxsize == 0)
maxsize = MAXLISTSIZE;
S.base = new ElemType[maxsize];
if (!S.base) exit(1); //存儲分配失敗
S.stacksize = maxsize;
S.top = 0; //空棧中元素個數為0
system("pause");
}
void Push(Stack &S)
{
ElemType e;
int j, n;
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (S.top == S.stacksize)
{
cout << "棧已滿,無法進行插入" << endl;
system("pause");
return;
}
cout << "請輸入壓入棧的元素個數(元素個數必須小於或等於" << S.stacksize - S.top << "):";
cin >> n;
cout << "請輸入壓入棧的元素:";
for(j = 0; j < n; j++)
{
cin >> e;
*(S.base + S.top) = e; // 插入新的元素
++S.top; // 棧頂指針後移
}
system("pause");
}
void Pop(Stack &S)
{
ElemType e;
int j, n;
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (S.top == 0)
{
cout << "棧為空,無法出棧" << endl;
system("pause");
return;
}
cout << "請輸入出棧的元素個數(出棧元素個數應小於或等於" << S.top << "):";
cin >> n;
cout << "出棧的元素為:";
for(j = 0; j < n; j++)
{
e = *(S.base + S.top-1); // 返回非空棧中棧頂元素
cout << e << ‘ ‘;
--S.top; // 棧頂指針前移
}
cout << endl;
system("pause");
}
void GetTop(Stack S)
{
ElemType e;
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
if (S.top == 0)
{
cout << "棧為空,無法獲取棧頂元素" << endl;
system("pause");
return;
}
e = *(S.base + S.top-1); // 返回非空棧中棧頂元素
cout << "棧頂元素為:" << e << endl;
system("pause");
}
void StackEmpty(Stack S)
{
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
if(S.top == 0)
cout << "該順序棧為空棧" << endl;
else
cout << "該順序棧不為空棧" << endl;
system("pause");
}
void StackLength(Stack S)
{
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
// 返回S的元素個數,即棧的長度
cout << "棧長為:" << S.top << endl;
system("pause");
}
void DestroyStack(Stack &S)
{
// 銷毀棧S,S不再存在
if (S.stacksize == 0)
{
cout << "棧已銷毀或棧未初始化,請選擇1初始化棧" << endl;
system("pause");
return;
}
delete[] S.base;
S.top = 0;
S.stacksize = 0;
cout << "棧已銷毀" << endl;
system("pause");
}
數據結構之順序棧(C++版)