#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;
}
阅读(1340) | 评论(0) | 转发(0) |