/* * (逆序)建立链栈与栈的基本操作. */ #include <stdio.h> #include <malloc.h> typedef char DataType; //结构体 typedef struct stack { DataType data; struct stack *next; }Stack; Stack *S=NULL; void StackCreate() { S=(Stack *)malloc(sizeof(Stack)); S->next = NULL; } int StackEmpty() { return S->next == NULL; } //入栈 void push(DataType value) { Stack *new_node = NULL; if((new_node=(Stack *)malloc(sizeof(Stack)))==NULL) { printf("memory alloc error!/n"); } //逆序建创 new_node->data = value; new_node->next = S->next; S->next = new_node; } //出栈 void pop() { Stack *first_node; if(StackEmpty()) { printf("Stack is empty!/n"); return; } first_node=S; S = first_node->next; free(first_node); } char GetTop() { } void StackPrint() { Stack *stack=NULL; printf("The elements of stack are: /n"); for(stack=S->next; stack!=NULL; stack=stack->next) printf("%c/n",stack->data); } int main() { StackCreate(); printf("push elements: /n"); push('a'); push('b'); push('c'); push('d'); push('e'); push('f'); push('g'); StackPrint(); printf("after two elements is pop,/n"); pop(); pop(); StackPrint(); return 0; }