
本文共 4697 字,大约阅读时间需要 15 分钟。
### ���������������������```c#include "stdio.h"#include "stdlib.h"#define MAXSIZE 30typedef int ElemType;typedef struct { ElemType data[MAXSIZE]; int top;} SequenceStack;void InitStack(SequnceStack &S) { S.top = -1;}void DestroyStack(SequnceStack* S) { free(S);}bool IsEmpty(SequnceStack &S) { return (S.top == -1);}int getLength(SequnceStack S) { return S.top + 1;}bool Push(SequnceStack &S, ElemType e) { if (S.top == MAXSIZE - 1) { return false; } S.top++; S.data[S.top] = e; return true;}bool Pop(SequnceStack &S, ElemType &e) { if (S.top == -1) { return false; } e = S.data[S.top]; S.top--; return true;}bool GetTop(SequnceStack &S, ElemType &e) { if (S.top == -1) { return false; } e = S.data[S.top]; return true;}void PrintStack(SequnceStack S) { while (S.top != -1) { printf("%d\n", S.data[S.top]); S.top--; }}int main() { SequenceStack S; InitStack(S); Push(S, 1); Push(S, 2); Push(S, 3); int Length = getLength(S); printf("���������%d\n", Length); PrintStack(S); ElemType e; Pop(S, e); printf("���������������������%d\n", e); GetTop(S, e); printf("���������������������%d\n", e); if (IsEmpty(S)) { DestroyStack(&S); printf("������������"); } system("pause"); return 0;}
���������������������������
������������������������������������������������������C������������������������������������������LIFO���������������������������������������������������������������������������������������������������������������������
���������������
struct SequenceStack
**������������������������������ ElemType data[MAXSIZE]
���������������������������������������MAXSIZE
���int top
������������������������������-1������������������
���������������
void InitStack(SequnceStack &S)
���
- ������������������
top
���������-1��� - ���������������������������������������
void DestroyStack(SequnceStack* S)
���
- ������������������������
- ���������������������������������������������������������������������
bool IsEmpty(SequnceStack &S)
���
- ������������������������������
true
������������������������false
��� - ������������������������������������������
int getLength(SequnceStack S)
���
- ���������������������������������
top
���������top + 1
������ - ������������������������������������������
bool Push(SequnceStack &S, ElemType e)
���
- ���������
e
������������ - ������������������������
S.top
������MAXSIZE - 1
���- ���������������
false
������������- ������
top
���������e
���������data[S.top]
��� - ������
true
���
- ������
- ���������������
- ���������������������������������������������������������������������
bool Pop(SequnceStack &S, ElemType &e)
���
- ���������������������
- ���������������������������
S.top != -1
���- ���������������
false
��� - ���������
- ������������������������
e
��� - ������
top
���������e
���true
���
- ������������������������
- ���������������
- ������������������������������������������
bool GetTop(SequnceStack &S, ElemType &e)
���
- ���������������������
- ������������������������
- ������������������
false
��� - ���������������
e
���������true
��� - ������������������������������������������
void PrintStack(SequnceStack S)
���
- ������������������������������������������������
- ������������������������������������������
top
������������������ - ���������������������������������������
���������main
���������������
- ������������
SequnceStack
������S
��� - ������
InitStack(S)
���������������������-1���
������������������
- ���������������������������1���2���3���������������������������������
- ������������������������
getLength(S)
���������������������������4��� - ������������������������������
PrintStack(S)
���������������������������
���������������������
-
���������������������
- ������
Pop(S, e)
���������������������1��� - ���������������������������������������������
- ������
-
���������������������������
- ������
GetTop(S, e)
���������������������������2��� - ���������������������������������
- ������
������������������������
- ������������������������������������
- ������
DestroyStack(&S)
��������������� - ���������������������������
- ������
������������������
������������
������������������������������������������������������������������������������
- ������������������������������������
- ������������������������������������������������������
- ���������������������������������������������������
- ���������������������������������������������
- ���������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
