數據結構——順序棧

頭文件:

#ifndef __SQSTACK_H__
#define __SQSTACK_H__

#include "error.h"

#define TRUE  1
#define FALSE 0

#define SIZE 10
typedef 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   -3



int 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章