数据结构——顺序栈

来源:互联网 发布:淘宝童装拍照技巧 编辑:程序博客网 时间:2024/05/17 03:26

头文件:

#ifndef __SQSTACK_H__#define __SQSTACK_H__#include "error.h"#define TRUE  1#define FALSE 0#define SIZE 10typedef int StackData; typedef struct _stack{    StackData data[SIZE];    // 栈数组    int  top;                // 栈顶元素下标}Stack;// 置空栈int InitStack (Stack *S);       // 判栈是否空栈int StackEmpty (Stack *S);// 判栈是否栈满   int StackFull (Stack *S);       // 进栈int Push (Stack *S, StackData x);   // 出栈int Pop (Stack *S, StackData *x);   // 取栈顶int GetTop (Stack *S, StackData *x); #endif // __SQSTACK_H__

错误信息头文件:

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

错误信息函数:

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

功能函数:

#include "SqStack.h"int InitStack (Stack *S)    {    if (S == NULL)    {        errno = ERROR;        return FALSE;    }    S->top = -1;   }// 空返回真,否则返回假int StackEmpty (Stack *S){    if (S == NULL)    {        errno = ERROR;        return FALSE;    }    return S->top == -1;}// 满返回真,否则返回假int StackFull (Stack *s){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    return s->top == (SIZE-1);}int Push (Stack *s, StackData x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    // 判断是否满栈    if (StackFull(s))    {        errno = FULL_STACK;        return FALSE;    }/*    s->data[top+1] = x;    s->top++;*/    s->data[++s->top] = x;    return TRUE;}int Pop (Stack *s, StackData *x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    // 判断是否空栈    if (StackEmpty(s))    {        errno = EMPTY_STACK;        return FALSE;    }/*      *x = s->data[s->top];    s->top--;*/    *x = s->data[s->top--];    return TRUE;}int GetTop (Stack *s, StackData *x){    if (s == NULL)    {        errno = ERROR;        return FALSE;    }    // 判断是否空栈    if (StackEmpty(s))    {        errno = EMPTY_STACK;        return FALSE;    }    *x = s->data[s->top];    return TRUE;}

main函数:

#include <stdio.h>#include "SqStack.h"int main(){    Stack s;    if (InitStack(&s) == FALSE)    {        printf ("初始化失败\n");        printf ("错误号:%d\n", errno);        myError("InitStack");        char * str = myStrError(errno);        printf ("str: %s\n", str);    }    if (StackEmpty(&s))    {        printf ("空栈\n");    }    if (StackFull(&s))    {        printf ("满栈\n");    }    int x;    if (Pop(&s, &x) != TRUE)    {        myError ("Pop 错误");    }    int i;    for (i = 0; i < 10; i++)    {        Push(&s, i);    }    if (Push(&s, 100) != TRUE)    {        myError("压入第11个元素");    }    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);    }    return 0;}
原创粉丝点击