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

全部博文(72)

文章存档

2010年(5)

2009年(14)

2008年(53)

分类: C/C++

2008-10-08 12:24:31

#include
#include
#include

typedef char DataType;
typedef struct stack
{
        DataType data;
        struct stack *next;
}Stack;

static Stack *S=NULL;

void StackCreate(void)
{
        S=(Stack *)malloc(sizeof(Stack));
}

int StackEmpty(void)
{
        return S == NULL;
}

int StackFull(void)
{
}

void push(DataType value)
{
        Stack *new_node=NULL;
        if((new_node=(Stack *)malloc(sizeof(Stack)))==NULL)
        {
                printf("memory alloc error!\n");
                exit(-1);
        }
        new_node->data = value;
        new_node->next = S;
        S = new_node;
}void pop(void)
{
        Stack *first_node;
        if(StackEmpty())
        {
                printf("Stack is empty!\n");
                exit(-1);
        }
        first_node=S;
        S = first_node->next;
        free(first_node);
}

DataType top(void)
{
        if(StackEmpty())
        {
                printf("Stack is empty!\n");
                exit(-1);
        }
        return S->data;
}

DataType StackPrint()
{
        Stack *stack=NULL;
        printf("The elements of stack are: \n");
        for(stack=S; stack!=NULL; stack=stack->next)
                printf("%4c",stack->data);
        printf("\n");
}

int main(void)
{
        StackCreate();
        push('a');
        push('b');
        push('c');
        push('d');
        push('e');
        push('f');
        push('g');

        StackPrint();
        printf("The top elements of the stack is :%c\n",top());

        printf("after two elements is pop,\n");
        pop();
        pop();

        StackPrint(S);
        printf("the top elements of the stack is :%c\n",top());

        return 0;
}




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