堆栈
@[.DataStructure]
1. 抽象数据结构
操作集合 | 解释 |
---|---|
Stack CreateStack(int MaxSize) | 初始化一个堆栈 |
bool IsFull(Stack S) | 判断堆栈S是否已满 已满返回 f a l s e false false |
bool Push(Stack S, ElementType X) | 将元素X压入堆栈。若堆栈已满,返回 f a l s e false false,否则将数据元素X插入到堆栈S栈顶处,并返回 t r u e true true |
bool IsEmpty(Stack S) | 判断堆栈S是否为空,若是返回 t r u e true true,否则返回 f a l s e false false |
ElementType Pop(Stack S) | 删除并返回栈顶元素。若堆栈为空,返回错误信息;否则将栈顶元素删除并返回 |
2. 出堆栈序列问题
如果将ABCD四个自符按顺序压入堆栈,是不是ABCD的所有排列都可能是出堆栈的序列?可以产生CABD这样的序列吗?
3. 动态数组堆栈实现
3.1 动态数组实现
以下这句话太强了
方式一:
#define MaxSize 100
int * Data;
Data = (int *)malloc(MaxSize * sizeof(int)); //暂时未彻底理解这句话。
方式二:
int Data[100];
相比于方式二,方式一的优势在于可以根据实际问题的需要在程序运行的时候动态创建 长度不一样的数组。这就是C语言的核心和灵魂所在 而方式二,一旦程序编译之后Data的长度就不能改变了。
3.2 全部代码
#define ElementType int
#define MAXSIZE 100
#include<iostream>
#include<cstdio>
using namespace std;
typedef int Position;
typedef struct SNode * PtrtoSNode;
struct SNode
{
ElementType * Data;
Position Top;
int MaxSize;
};
typedef PtrtoSNode Stack;
Stack CreatStack(int MaxSize)
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); // 说实话这句话不懂
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}
bool IsFull(Stack S)
{
return(S->Top == S->MaxSize - 1);
}
bool Push(Stack S, ElementType X)
{
if (IsFull(S)) {
cout << "The Stack is full" << endl;
return false;
}
else
{
S->Data[++(S->Top)] = X;
return true;
}
}
bool IsEmpty(Stack S)
{
return(S->Top == -1);
}
#define Error -100
ElementType Pop(Stack S)
{
if (IsEmpty(S))
{
cout << "the Stack is Empty" << endl;
return Error;
}
else
{
return(S->Data[(S->Top)--]);
}
}
int main()
{
Stack S = CreatStack(100);
for (size_t i = 0; i < 200; i++)
{
Push(S, i);
}
return 0;
}
4. 双堆栈实现
#define ElementType int
#define MAXSIZE 100
#include<iostream>
#include<cstdio>
using namespace std;
typedef int Position;
typedef struct SNode * PtrtoSNode;
struct SNode
{
ElementType * Data;
Position Top1;
Position Top2;
int MaxSize;
};
typedef PtrtoSNode Stack;
Stack CreatStack(int MaxSize)
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); // 说实话这句话不懂
S->Top1 = -1;
S->Top2 = MaxSize;
S->MaxSize = MaxSize;
return S;
}
bool Push(Stack S, ElementType X, int Tag)
{
if (S->Top2 - S->Top1 == 1)
{
cout << "The Stack is full" << endl;
return false;
}
else
{
if (Tag == 1)
{
S->Data[++(S->Top1)] = X;
}
else
{
S->Data[--(S->Top2)] = X;
}
return true;
}
}
#define Error -100
ElementType Pop(Stack S, int Tag)
{
if (Tag == 1) {
if (S->Top1 == -1)
{
cout << "The stack1 is Empty";
return Error;
}
else
{
return S->Data[(S->Top1)--];
}
}
else
{
if (S->Top2 = S->MaxSize)
{
cout << "The stack2 is empty";
return Error;
}
else
{
return S->Data[(S->Top2)++];
}
}
}
int main()
{
Stack S = CreatStack(100);
for (size_t i = 0; i < 200; i++)
{
Push(S, i, 1);
Push(S, i, 2);
}
return 0;
}
5. 链表堆栈实现
#define ElementType int
#define MAXSIZE 100
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct SNode * PtrtoSNode;
struct SNode
{
ElementType Data;
PtrtoSNode Next;
};
typedef PtrtoSNode Stack;
bool IsEmpty(Stack S)
{
return(S->Next == NULL);
}
Stack CreatStack()
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}
bool Push(Stack S, ElementType X)
{
PtrtoSNode TmpCell;
TmpCell = (PtrtoSNode)malloc(sizeof(struct SNode));
TmpCell->Data = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
return true;
}
#define Error -100
ElementType Pop(Stack S)
{
//删除并返回栈顶元素
PtrtoSNode FirstCell;
ElementType TopElem;
if (IsEmpty(S))
{
cout << "the stack is empty" << endl;
return Error;
}
else
{
FirstCell = S->Next;
TopElem = FirstCell->Data;
S->Next = FirstCell->Next;
free(FirstCell);
return TopElem;
}
}
int main()
{
Stack S = CreatStack();
for (size_t i = 0; i < 200; i++)
{
Push(S, i);
Push(S, i);
}
return 0;
}
数据结构动图资源
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html