数据结构 - 链栈(C)

来源:互联网 发布:淘宝怎么买百度云资源 编辑:程序博客网 时间:2024/05/17 07:57
/* Stack.h - by Chimomo */#ifndef Stack_H#define Stack_Htypedef char Element;typedef struct node * PNode;/*Stack node*/typedef struct node{    int data;    PNode next;} Node;/*Stack*/typedef struct stack{    PNode top;    int size;} Stack;/*Construct an empty stack*/Stack *InitStack();/*Judge stack is empty or not*/int IsEmpty(Stack *pStack);/*Push element into stack*/PNode Push(Stack *pStack, Element element);/*Push element of integer into stack*/PNode PushInt(Stack *pStack, int element);/*Pop element from stack*/PNode Pop(Stack *pStack, Element *pElement);/*Return stack top element*/PNode GetTop(Stack *pStack, Element *pElement);/*Return stack top element of integer*/PNode GetTopInt(Stack *pStack, int *pElement);/*Return the size of stack*/int GetSize(Stack *pStack);/*Traverse stack and call visit funciton for each stack element*/void Traverse(Stack *pStack, void (*visit)(Element element));/*Clear stack*/void ClearStack(Stack *pStack);/*Destroy stack*/void DestroyStack(Stack *pStack);#endif
/* Stack.c - by Chimomo */#include<malloc h="">#include"Stack.h"/*Construct an empty stack*/Stack *InitStack(){    Stack *pStack = (Stack *)malloc(sizeof(Stack));    if(pStack != NULL)    {        pStack->top = NULL;        pStack->size = 0;    }    return pStack;}/*Judge stack is empty or not*/int IsEmpty(Stack *pStack){    if(pStack->top == NULL && pStack->size == 0)    {        return 1;    }    else    {        return 0;    }}/*Push element into stack*/PNode Push(Stack *pStack, Element element){    PNode pnode = (PNode)malloc(sizeof(Node));    if(pnode != NULL)    {        pnode->data = element;        pnode->next = GetTop(pStack, NULL);        pStack->size++;        pStack->top = pnode;    }    return pnode;}/*Push element of integer into stack*/PNode PushInt(Stack *pStack, int element){    PNode pnode = (PNode)malloc(sizeof(Node));    if(pnode != NULL)    {        pnode->data = element;        pnode->next = GetTop(pStack, NULL);        pStack->size++;        pStack->top = pnode;    }    return pnode;}/*Pop element from stack*/PNode Pop(Stack *pStack, Element *pElement){    PNode p = pStack->top;    if(!IsEmpty(pStack) && p != NULL)    {        if(pElement != NULL)        {            *pElement = p->data;        }        pStack->size--;        pStack->top = pStack->top->next;        free(p);    }    return pStack->top;}/*Return stack top element*/PNode GetTop(Stack *pStack, Element *pElement){    if(!IsEmpty(pStack) && pElement != NULL)    {        *pElement = pStack->top->data;    }    return pStack->top;}/*Return stack top element of integer*/PNode GetTopInt(Stack *pStack, int *pElement){    if(!IsEmpty(pStack) && pElement != NULL)    {        *pElement = pStack->top->data;    }    return pStack->top;}/*Return the size of stack*/int GetSize(Stack *pStack){    return pStack->size;}/*Traverse stack and call visit funciton for each stack element*/void Traverse(Stack *pStack, void (*visit)(Element element)){    PNode p = pStack->top;    int i = pStack->size;    while(i--)    {        visit(p->data);        p = p->next;    }}/*Clear stack*/void ClearStack(Stack *pStack){    while(!IsEmpty(pStack))    {        Pop(pStack, NULL);    }}/*Destroy stack*/void DestroyStack(Stack *pStack){    if(!IsEmpty(pStack))    {        ClearStack(pStack);    }    free(pStack);}</malloc>
/* Main.c - by Chimomo */#include<stdio.h>#include"Stack.h"void print(Element element){    printf("The node is %c\n", element);}main(){    Stack *pStack = InitStack();    char i, element;        printf("Push A-Z into stack:\n");    for(i = 'A'; i <= 'Z'; i++)    {        Push(pStack, i);        GetTop(pStack, &element);        printf("%c ", element);    }    printf("\nTraverse from stack top to bottom and call print function:\n");    Traverse(pStack, print);    printf("Pop each element of stack:\n");    for(i = 'A'; i <= 'Z'; i++)    {        Pop(pStack, &element);        printf("%c ", element);    }    ClearStack(pStack);    if(IsEmpty(pStack))    {        printf("\nStack was successfully cleared.\n");    }    DestroyStack(pStack);    printf("Stack was destroyed.\n");}/*Output:Push A-Z into stack:A B C D E F G H I J K L M N O P Q R S T U V W X Y ZTraverse from stack top to bottom and call print function:The node is ZThe node is YThe node is XThe node is WThe node is VThe node is UThe node is TThe node is SThe node is RThe node is QThe node is PThe node is OThe node is NThe node is MThe node is LThe node is KThe node is JThe node is IThe node is HThe node is GThe node is FThe node is EThe node is DThe node is CThe node is BThe node is APop each element of stack:Z Y X W V U T S R Q P O N M L K J I H G F E D C B AStack was successfully cleared.Stack was destroyed.Press any key to continue . . .*/
1 0
原创粉丝点击