#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Stack_Size 6
#define TRUE 1
#define FALSE 0
typedef struct //定义数据类型
{
char elem[Stack_Size];
int top;
}SeqStack;
//初始化顺序栈
void InitStack(SeqStack *S)
{
S->top=-1; //top指针拉置=-1,压入一个元素后,top指针位置=0; 栈顶位置=栈顶实际位置
}
//栈为空时返回真,否则返回假
int IsEmptyStack(SeqStack *S)
{
return S->top==-1?FALSE:TRUE;
}
//栈是满的返回真,否则返回假
int IsFull(SeqStack *S)
{
return S->top==Stack_Size-1?TRUE:FALSE;
}
//入栈
void Push(SeqStack *S) //进栈 ,将元素压入栈中
{
int i;
char ch;
if(S->top<=Stack_Size-1) //压入栈中的元素不能超过栈的最大存储
{
printf(“输入入栈字符元素:\n”);
for(i=0;i<Stack_Size;i++)
{
scanf(" %c",&ch);
S->top++; //移动栈顶指针
S->elem[S->top]=ch;
}
}
}

这篇博客介绍了顺序栈的数据结构,并提供了C语言实现顺序栈的基本操作,包括初始化栈、判断栈是否为空、判断栈是否已满、入栈、出栈、获取栈顶元素以及计算栈的长度。示例代码展示了如何使用这些操作。
1万+

被折叠的 条评论
为什么被折叠?



