顺序栈SqStack
输出
Push Success! 1
Push Success! 2
Push Success! 3
Push Success! 4
Push Success! 5
Push Success! 6
Push Success! 7
Push Success! 8
Push Success! 9
Push Success! 10
Current Top:10
(Current)顺序栈的元素为:base-->top:
1 2 3 4 5 6 7 8 9 10
Pop Success!
pop(10)
Pop Success!
pop(9)
Current Top:8
(Current)顺序栈的元素为:base-->top:
1 2 3 4 5 6 7 8
Clear Success!
是否为空?是
Destroy Success!
。h
#pragma once
/*
Push,Pop,GetTop,Traverse,ClearStack,DestroyStack, StackEmpty, StackFull
(*这个操作的实现参考Push中的代码)。请自行设计main函数以测试各个基本操作。
1. 在写代码之前,回想顺序表和顺序栈的关系,特别地,老师课上说的top可以是什么(指针或者int型数组下标)?
top指向的位置可以是什么?请约定,在代码中统一。
2. 在写代码前,思考各个操作的实现顺序。
3.这个栈实现了,课本P.75算法3.20 数制转换就很容易实现了。
*/
#include <iostream>
using namespace std;
typedef int ElemType;
#define MS 100
typedef struct Stack {//int型的顺序栈
int size;//顺序栈的容量
ElemType* top;//栈顶指针
ElemType* base;//
}Stack;
bool s_Init(Stack& L);//初始化
void s_Push(Stack& L,int value);//push
void s_Traverse(Stack L);//遍历
void s_Pop(Stack& L, int& e);//pop
void s_GetTop(Stack L, int& top);//gettop
bool isEmpty(Stack L);//是否为空
bool isFull(Stack L);//是否为满
void s_Clear(Stack& L);
void s_Destroy(Stack& L);
。cpp
#include "SqS.h"
/*
顺序栈---top在尾部
*/
bool isEmpty(Stack L) {
if (L.base == L.top) {
return true;
}
else {
return false;
}
}
bool isFull(Stack L) {
if (L.top - L.base == L.size) {
return true;
}
else {
return false;
}
}
bool s_Init(Stack& L) {//初始化
L.base = new ElemType[MS];//顺序栈的最大空间为100
if (!L.base) {
cout << "初始化、分配空间失败" << endl;
return false;
}
L.top = L.base;//初始为空
L.size = MS;//栈的最大容量
return true;
}
void s_Push(Stack& L,int value) {//push
if ( isFull(L)) {//栈顶在尾部
cout << "栈已经满啦!无法Push" << endl;
}
/*
int value;
cout << "请输入要push的值,用空格隔开" << endl;
cin >> value;
while (cin.get() != '\n') {
L.base[L.size] = value;
++L.top;//top始终往后移
++L.size;
}
*/
*(L.top) = value;
++L.top;//top始终往后移,指向栈顶的位置
cout << "Push Success! " << value << endl;
}
void s_Traverse(Stack L) {//遍历
if (isEmpty(L)) {
cout << "栈为空!" << endl;
}
else {
cout << "顺序栈的元素为:base-->top:" << endl;//从栈底开始遍历
int length = L.top - L.base;
for (int i = 0; i < length; i++) {
cout << L.base[i] << ' ';
}
cout << endl;
}
}
void s_Pop(Stack& L, int& e) {//pop
if (isEmpty(L)) {//栈为空,无法pop
cout << "栈空!无法pop" << endl;
}
else {
--L.top;
e = *(L.top);
--L.size;
cout << "Pop Success!" << endl;
}
}
void s_GetTop(Stack L, int& top) {
if (isEmpty(L)) {
cout << "栈为空!" << endl;
}
else {
top = *(--L.top);
}
}
void s_Clear(Stack& L) {
if (L.base) {
L.top = L.base;//top指向栈底了
cout << "Clear Success!" << endl;
}
else {
cout << "已Destroy,不必清了" << endl;
}
}
void s_Destroy(Stack& L) {
if (L.base) {
delete [] L.base;
L.size = 0;//栈的容量为0了
L.top = L.base = NULL;//回归内存
cout << "Destroy Success!" << endl;
}
else {
cout << "已Destroy,不必清了" << endl;
}
}
main
#include "SqS.h"
int main() {
Stack L;
s_Init(L);//初始化
s_Push(L, 1);//push
s_Push(L, 2);
s_Push(L, 3);
s_Push(L, 4);
s_Push(L, 5);
s_Push(L, 6);
s_Push(L, 7);
s_Push(L, 8);
s_Push(L, 9);
s_Push(L, 10);
int top1;
s_GetTop(L, top1);
cout << "Current Top:" << top1 << endl;
cout << "(Current)";
s_Traverse(L);
int e1,e2;
s_Pop(L, e1);
cout << "pop(" << e1 << ")" << endl;
s_Pop(L, e2);
cout << "pop(" << e2 << ")" << endl;
int top2;
s_GetTop(L, top2);
cout << "Current Top:" << top2 << endl;
cout << "(Current)";
s_Traverse(L);
s_Clear(L);
cout << "是否为空?";
cout << (isEmpty(L) ? "是" : "否" )<< endl;
s_Destroy(L);
return 0;
}
LinklistStack链栈---NoHead
输出
PUSH 10 elements~
PUSH 0 Success!
PUSH 1 Success!
PUSH 2 Success!
PUSH 3 Success!
PUSH 4 Success!
PUSH 5 Success!
PUSH 6 Success!
PUSH 7 Success!
PUSH 8 Success!
PUSH 9 Success!
current Stack:(top->base)9 8 7 6 5 4 3 2 1 0
top is 9
is empty? NO
POP 5 elements~
POP 9 Success!
POP 8 Success!
POP 7 Success!
POP 6 Success!
POP 5 Success!
current Stack:(top->base)4 3 2 1 0
top is 4
Clear Success!
Destroy Success!
。h
#pragma once
#include <iostream>
using namespace std;
typedef char ElemType;//!!char
typedef struct Stack {
ElemType data;
Stack* next;
}snode,*psnode;
void ini(psnode& S);//初始化
bool isEmpty(psnode S);//判空
void push(psnode& S,ElemType value);//PUSH
void pop(psnode& S,ElemType& e);//POP
void traverse(psnode S);//bianli
void getTop(psnode S);//get top
void clear(psnode& S);//clear
void destroy(psnode& S);//destroy
。cpp
#include "LLStack.h"
void ini(psnode& S) {
S = NULL;//无头结点版本
}
bool isEmpty(psnode S) {
if (S == NULL) {
return true;
}
else {
return false;//为空返回true 不为空返回false
}
}
void push(psnode& S,ElemType value) {//头茶法push
psnode p = new snode;
p->data = value;
p->next = NULL;
if (S == NULL) {//如果S为空,直接初始第一个结点
S = p;
}
else {//如果S不为空,p也永远是第一个结点
p->next = S;//头插
S = p;//更新当前第一个结点
}
cout << "PUSH " << value << " Success!" << endl;
}
void traverse(psnode S) {
if (isEmpty(S)) {
cout << "Stack is Empty! stop traverse!" << endl;
}
//top->base
cout << "current Stack:(top->base)";
psnode p;
p = S;
while (p) {//从栈顶开始遍历
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void pop(psnode& S,ElemType& e) {//头删法
if (isEmpty(S)) {
cout << "Stack is Empty! stop POP!" << endl;
}
else {
psnode p;
p = S;
e = p->data;
S = S->next;//更新栈顶pointer
delete p;
}
}
void getTop(psnode S) {//get top
if (isEmpty(S)) {
cout << "Stack is empty!Fail to get top!" << endl;
}
else {
psnode p = S;
cout << "top is " << p->data << endl;
}
}
void clear(psnode& S) {//clear
if (S == NULL) {
cout << "Stack has been destroy and doesn't need to be cleared." << endl;
}
else {
S = NULL;
cout << "Clear Success!" << endl;
}
}
void destroy(psnode& S) {//destroy
if (S = NULL){
cout << "Stack has been destroy and doesn't need to be destroyed." << endl;
}
else {
psnode p;
while (S)
{
p = S;
S = S->next;
delete p;//
}
S = NULL;
cout << "Destroy Success!" << endl;
}
}
main
#include "LLStack.h"
int main() {
psnode S;
ini(S);
//push
cout << "PUSH 10 elements~" << endl;
ElemType e = '0';
for (int i = 0; i < 10; i++) {
push(S, e);
e += 1;
}
//traverse
traverse(S);
//gettop
getTop(S);
//isempty?
cout << "is empty? " << (isEmpty(S) ? "YES" : "NO") << endl;
//pop
cout << "POP 5 elements~" << endl;
for (int i = 0; i < 5; i++) {
ElemType e;
pop(S, e);
cout << "POP " << e << " Success!" << endl;
}
//
traverse(S);
getTop(S);
//clear
clear(S);
destroy(S);
return 0;
}
SqStack应用-----数值转换
输出
。h
。cpp
main
LinkListStack----括号匹配
输出
。h
。cpp
main
后缀表达式求值
输出
。h
。cpp
main
中缀--->后缀
输出
。h
。cpp
main
直接求中缀
输出
。h
。cpp
main