顺序栈(C语言)
发布日期:2021-05-10 07:59:22 浏览次数:17 分类:精选文章

本文共 4697 字,大约阅读时间需要 15 分钟。

### ���������������������
```c
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 30
typedef 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)���������������
      • ���������������������������
  • ������������������

  • ���������4
  • ������������ 1 2 3
  • ���������������������1
  • ���������������������2
  • ������������

  • ������������

    ������������������������������������������������������������������������������

    • ������������������������������������
    • ������������������������������������������������������
    • ���������������������������������������������������
    • ���������������������������������������������
    • ���������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:链栈(C语言)
    下一篇:Mapper.xml中新增数据并返回主键ID(MYSQL)

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年04月19日 01时00分06秒