数据结构——链式栈

来源:互联网 发布:网络侵权案件的管辖 编辑:程序博客网 时间:2024/06/06 14:09

头文件:

#ifndef __LINKSTACK_H__#define __LINKSTACK_H__#include "error.h"#define FALSE 0#define TRUE  1typedef int StackData;typedef struct _node{    StackData data;    struct _node *next;}Node;typedef struct _linkStack{    Node *top;}LinkStack;// 创建栈LinkStack *Create_Stack();// 判栈空否int StackEmpty (LinkStack *s);  // 进栈int Push (LinkStack *S, StackData x);   // 出栈int Pop (LinkStack *S, StackData *x);// 获取栈顶元素int GetTop (LinkStack *S, StackData *x); // 销毁栈int Destroy(LinkStack *s);#endif //__LINKSTACK_H__

错误信息头文件:

#ifndef __ERROR_H__#define __ERROR_H__#include <stdio.h>#define ERROR         -1#define FULL_STACK    -2#define EMPTY_STACK   -3#define MALLOC_ERROR  -4int errno;     // 错误号void myError(char *str);char* myStrError(int num);#endif // __ERROR_H__

输出错误信息函数:

#include "error.h"void myError(char *str){    char *msg = myStrError(errno);    printf ("%s: %s\n", str, msg);}char* myStrError(int num){    switch (errno)    {        case ERROR:            return "输入参数错误";        case FULL_STACK:            return "满栈状态";        case EMPTY_STACK:            return "空栈状态";        case MALLOC_ERROR:            return "空间分配失败";    }}

功能函数:

#include "LinkStack.h"#include <stdlib.h>LinkStack *Create_Stack(){    LinkStack* s = (LinkStack*)malloc(sizeof(LinkStack)/sizeof(char));    if (s == NULL)    {        errno = MALLOC_ERROR;        return NULL;    }    // 置空栈    s->top = NULL;    return s;}int StackEmpty (LinkStack *s){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    return s->top == NULL;}int Push (LinkStack *s, StackData x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    // 新建结点    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));    if (node == NULL)    {        errno = MALLOC_ERROR;        return FALSE;    }    node->data = x;    node->next = s->top;    s->top = node;    return TRUE;}int Pop (LinkStack *s, StackData *x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    if (StackEmpty(s))    {        errno = EMPTY_STACK;        return FALSE;    }    Node *p = s->top;    *x = p->data;    s->top = p->next;    free(p);        return TRUE;}int GetTop (LinkStack *s, StackData *x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    if (StackEmpty(s))    {        errno = EMPTY_STACK;        return FALSE;    }    *x = s->top->data;      return TRUE;}// 销毁栈int Destroy(LinkStack *s){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    int x;    while(StackEmpty(s) != TRUE)    {        Pop (s, &x);    }    free(s);    return TRUE;}

main函数:

#include <stdio.h>#include "LinkStack.h"int main(){    LinkStack *s =  Create_Stack();    if (s == NULL)    {        myError ("Create_Stack");        return -1;    }    if (StackEmpty(s))    {        printf ("空栈\n");    }    int x;    if (Pop(s, &x) != TRUE)    {        myError ("Pop 错误");    }    int i;    for (i = 0; i < 10; i++)    {        Push(s, i);    }    char str[100];    for (i = 0; i < 12; i++)    {        if (Pop (s, &x) != TRUE)        {            sprintf (str, "Pop第%d个元素", i);            myError (str);        }        printf ("x : %d\n", x);    }    if (Destroy(s) == FALSE)    {        myError ("Destroy");    }    return 0;}