顺序栈
前提代码
#include <iostream>
/* 顺序栈结构 */
typedef struct
{
int data[20];
int top; /* 用于栈顶指针 */
}SqStack;
bool visit(int c)
{
cout<<c;
return true;
}
构造一个空栈S
bool InitStack(SqStack *S)
{
/* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
S->top = -1;
return true;
}
#include <iostream>
using namespace std;
#define MAX_SIZE 100
typedef struct
{
int data[MAX_SIZE];
int top;
} SqStack;
bool InitStack(SqStack *S)
{
S->top = -1;
return true;
}
int main()
{
SqStack stack;
InitStack(&stack);
cout << "Stack initialized successfully." << endl;
return 0;
}
把S置为空栈
bool ClearStack(SqStack *S)
{
S->top = -1;
return true;
}
若栈S为空栈,则返回TRUE,否则返回FALSE
bool StackEmpty(SqStack S)
{
if (S.top == -1)
return true;
else
return false;
}
返回S的元素个数,即栈的长度
int StackLength(SqStack S)
{
return S.top + 1;
}
若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
bool GetTop(SqStack S,int *e)
{
if (S.top == -1)
return false;
else
*e = S.data[S.top];
return true;
}
#include <iostream>
using namespace std;
typedef struct
{
SElemType data[100];
int top;
} SqStack;
bool InitStack(SqStack *S)
{
S->top = -1;
return true;
}
bool GetTop(SqStack S, int *e)
{
if (S.top == -1)
return false;
else
*e = S.data[S.top];
return true;
}
int main()
{
SqStack stack;
InitStack(&stack);
// 入栈
stack.data[++stack.top] = 10; //++stack.top = 0
stack.data[++stack.top] = 20; //++stack.top = 1
stack.data[++stack.top] = 30; //++stack.top = 2
// 获取栈顶元素
SElemType topElement;
if (GetTop(stack, &topElement) == OK)
{
cout << "Top element: " << topElement << endl;
}
else
{
cout << "Stack is empty." << endl;
}
return 0;
}
插入元素e为新的栈顶元素
bool Push(SqStack *S,int e)
{
if(S->top == MAXSIZE -1) /* 栈满 */
{
return false;
}
S->top++; /* 栈顶指针增加一 */
S->data[S->top] = e; /* 将新插入元素赋值给栈顶空间 */
return true;
}
int main()
{
SqStack stack;
stack.top = -1; // 初始化栈顶指针为-1,表示空栈
// 入栈操作示例
SElemType element;
cout << "请输入要入栈的元素:";
cin >> element;
Status pushStatus = Push(&stack, element);
if (pushStatus == OK)
{
cout << "入栈成功!" << endl;
}
else
{
cout << "入栈失败,栈已满!" << endl;
}
return 0;
}
若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
bool Pop(SqStack *S,int *e)
{
if(S->top == -1)
return false;
*e = S->data[S->top]; /* 将要删除的栈顶元素赋值给e */
S->top--; /* 栈顶指针减一 */
return true;
}
从栈底到栈顶依次对栈中每个元素显示
bool StackTraverse(SqStack S)
{
int i = 0;
while(i <= S.top)
{
visit(S.data[i++]);
}
cout<<endl;
return true;
}
int main()
{
int j;
SqStack s;
int e;
if (InitStack(&s))
{
for (j = 1; j <= 10; j++)
{
Push(&s, j);
}
}
cout << "栈中元素依次为:";
StackTraverse(s);
Pop(&s, &e);
cout << "弹出的栈顶元素 e=" << e << endl;
cout << "栈空否:" << StackEmpty(s) << "(1:空 0:否)" << endl;
GetTop(s, &e);
cout << "栈顶元素 e=" << e << " 栈的长度为" << StackLength(s) << endl;
ClearStack(&s);
cout << "清空栈后,栈空否:" << StackEmpty(s) << "(1:空 0:否)" << endl;
delete[] s.base;
return 0;
}
两栈共享空间
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */
两栈共享空间结构
typedef struct
{
SElemType data[MAXSIZE];
int top1; /* 栈1栈顶指针 */
int top2; /* 栈2栈顶指针 */
}SqDoubleStack;
bool visit(SElemType c)
{
cout<<c;
return true;
}
构造一个空栈S
bool InitStack(SqDoubleStack *S)
{
S->top1 = -1;
S->top2 = MAXSIZE;
return true;
}
把S置为空栈
bool ClearStack(SqDoubleStack *S)
{
S->top1 = -1;
S->top2 = MAXSIZE;
return true;
}
若栈S为空栈,则返回TRUE,否则返回FALSE
bool StackEmpty(SqDoubleStack S)
{
if (S.top1 == -1 && S.top2 == MAXSIZE)
return true;
else
return false;
}
返回S的元素个数,即栈的长度
int StackLength(SqDoubleStack S)
{
return (S.top1 + 1) + (MAXSIZE - S.top2);
}
插入元素e为新的栈顶元素
bool Push(SqDoubleStack *S,SElemType e,int stackNumber)
{
if (S->top1+1 == S->top2) /* 栈已满,不能再push新元素了 */
return false;
if (stackNumber == 1) /* 栈1有元素进栈 */
S->data[++S->top1] = e; /* 若是栈1则先top1+1后给数组元素赋值。 */
else if (stackNumber == 2) /* 栈2有元素进栈 */
S->data[--S->top2] = e; /* 若是栈2则先top2-1后给数组元素赋值。 */
return true;
}
若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
bool Pop(SqDoubleStack *S,SElemType *e,int stackNumber)
{
if (stackNumber==1)
{
if (S->top1==-1)
return false; /* 说明栈1已经是空栈,溢出 */
*e=S->data[S->top1--]; /* 将栈1的栈顶元素出栈 */
}
else if (stackNumber==2)
{
if (S->top2==MAXSIZE)
return false; /* 说明栈2已经是空栈,溢出 */
*e=S->data[S->top2++]; /* 将栈2的栈顶元素出栈 */
}
return true;
}
遍历并处理S
中的所有元素
bool StackTraverse(SqDoubleStack S)
{
int i;
i = 0;
while (i <= S.top1)
{
visit(S.data[i++]);
}
i = S.top2;
while (i < MAXSIZE)
{
visit(S.data[i++]);
}
cout << endl;
return true;
}
int main()
{
int j;
SqDoubleStack s;
int e;
if (InitStack(&s))
{
for (j = 1; j <= 5; j++)
{
Push(&s, j, 1);
}
for (j = MAXSIZE; j >= MAXSIZE - 2; j--)
{
Push(&s, j, 2);
}
}
cout << "栈中元素依次为:";
StackTraverse(s);
cout << "当前栈中元素有:" << StackLength(s) << endl;
Pop(&s, &e, 2);
cout << "弹出的栈顶元素 e=" << e << endl;
cout << "栈空否:" << (StackEmpty(s) ? 1 : 0) << "(1:空 0:否)" << endl;
for (j = 6; j <= MAXSIZE - 2; j++)
{
Push(&s, j, 1);
}
cout << "栈中元素依次为:";
StackTraverse(s);
cout << "栈满否:" << (Push(&s, 100, 1) ? 0 : 1) << "(1:否 0:满)" << endl;
ClearStack(&s);
cout << "清空栈后,栈空否:" << (StackEmpty(s) ? 1 : 0) << "(1:空 0:否)" << endl;
return 0;
}
链栈
前提代码
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */
/* 链栈结构 */
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct
{
LinkStackPtr top;
int count;
}LinkStack;
bool visit(SElemType c)
{
cout<<c;
return true;
}
构造一个空栈S
bool InitStack(LinkStack *S)
{
S->top = new StackNode;
if(!S->top)
return false;
S->top = NULL;
S->count = 0;
return true;
}
把S置为空栈
bool ClearStack(LinkStack *S)
{
LinkStackPtr p,q;
p = S->top;
while(p)
{
q = p;
p = p->next;
free(q);
}
S->count = 0;
return true;
}
若栈S为空栈,则返回TRUE,否则返回FALSE
bool StackEmpty(LinkStack S)
{
if (S.count == 0)
return true;
else
return false;
}
返回S的元素个数,即栈的长度
int StackLength(LinkStack S)
{
return S.count;
}
若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
bool GetTop(LinkStack S,SElemType *e)
{
if (S.top == NULL)
return false;
else
*e = S.top->data;
return true;
}
插入元素e为新的栈顶元素
bool Push(LinkStack *S,SElemType e)
{
//LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode));
LinkStackPtr s = new StackNode;
s->data = e;
s->next = S->top; /* 把当前的栈顶元素赋值给新结点的直接后继,见图中① */
S->top = s; /* 将新的结点s赋值给栈顶指针,见图中② */
S->count++;
return true;
}
若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
bool Pop(LinkStack *S,SElemType *e)
{
LinkStackPtr p;
if(StackEmpty(*S))
return false;
*e = S->top->data;
p = S->top; /* 将栈顶结点赋值给p,见图中③ */
S->top = S->top->next; /* 使得栈顶指针下移一位,指向后一结点,见图中④ */
free(p); /* 释放结点p */
S->count--;
return true;
}
bool StackTraverse(LinkStack S)
{
LinkStackPtr p;
p = S.top;
while(p)
{
visit(p->data);
p = p->next;
}
cout<<endl;
return true;
}
int main()
{
int j;
LinkStack s;
int e;
if (InitStack(&s) == OK)
for (j = 1; j <= 10; j++)
Push(&s, j);
cout << "栈中元素依次为:";
StackTraverse(s);
Pop(&s, &e);
cout << "弹出的栈顶元素 e=" << e << endl;
cout << "栈空否:" << StackEmpty(s) << "(1:空 0:否)" << endl;
GetTop(s, &e);
cout << "栈顶元素 e=" << e << " 栈的长度为" << StackLength(s) << endl;
ClearStack(&s);
cout << "清空栈后,栈空否:" << StackEmpty(s) << "(1:空 0:否)" << endl;
return 0;
}