Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335662
  • 博文数量: 72
  • 博客积分: 2130
  • 博客等级: 大尉
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-05 16:10
文章分类

全部博文(72)

文章存档

2010年(5)

2009年(14)

2008年(53)

分类: C/C++

2008-10-08 09:49:25

#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;
}




阅读(887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~