#include
#include
#include
#define StackSize 100
typedef char DataType;
typedef struct stack
{
DataType data[StackSize];
int top;
}Stack;
void StackInit(Stack *S)
{
S->top = -1;
}
int StackEmpty(Stack *S)
{
return S->top==-1;
}
int StackFull(Stack *S)
{
return S->top==StackSize-1;
}
void push(Stack *S, DataType value)
{
if(StackFull(S))
{
printf("Stack is full!\n");
exit(-1);
}
S->data[++S->top] = value;
}
void pop(Stack *S)
{
if(StackEmpty(S))
{
printf("Stack is empty!\n");
exit(-1);
}
S->top--;
}
DataType top(Stack *S)
{
if(StackEmpty(S))
{
printf("Stack is empty!\n");
exit(-1);
}
return S->data[S->top];
}
DataType StackPrint(Stack *S)
{
int count = S->top;
printf("The elements of stack are: \n");
while(count != -1)
{
printf("%4c",S->data[count]);
count--;
}
printf("\n");
}int main(void)
{
Stack *S = (Stack *)malloc(sizeof(Stack));
StackInit(S);
push(S,'a');
push(S,'b');
push(S,'c');
push(S,'d');
push(S,'e');
push(S,'f');
push(S,'g');
printf("The number of stack elements are :%d\n",S->top+1);
StackPrint(S);
printf("The top elements of the stack is :%c\n",top(S));
printf("after two elements is pop,\n");
pop(S);
pop(S);
printf("the number of stack elements are :%d\n",S->top+1);
StackPrint(S);
printf("the top elements of the stack is :%c\n",top(S));
return 0;
}
阅读(916) | 评论(0) | 转发(0) |