1. 程式人生 > >C語言 棧的基本操作 棧的實現

C語言 棧的基本操作 棧的實現

#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 10
#define OK 1
#define ERROR -1
#define STACKINCREMENT 10/*儲存空間分配增量*/
typedef int ElemType;
typedef int Status;

棧的結構體

typedef struct SqStack
{
    int top;
    int bottom;
    ElemType array[STACK_SIZE];
}SqStack;

棧的初始化

Status Init_Stack(SqStack *
S) { S->top = S->bottom =0; return OK; }

入棧

Status Push(SqStack *S, ElemType e)//e為待壓入的元素
{
    int a = 0;

    if(S->top - S->bottom == STACK_SIZE - 1)
    {
        printf("This stack is full!\n\n");
        a = ERROR;
    }
    else
    {
        S->array[S->top] = e;
        S->top++;
        a = OK;
    }
    return a;
}

出棧

Status Pop(SqStack *S)
    int e;
    if(S->bottom == S->top)
        printf("This stack is empty!\n\n");
    e = S->array[(S->top) - 1];
    S->top--;
    return e;
}

獲取棧頂元素

Status GetTop(SqStack *S)
{
    int e;

    if(S->bottom == S->top)
    {
        printf("The stack is empty!\n\n"
); } else { S->top--; e = S->array[S->top]; S->top++; S->array[S->top] = e; } return e; }

遍歷棧

Status StackLength(SqStack *S)
{
    int i;
    int len = 0;

    if(S->top == S->bottom)
        printf("This stack is empty!\n\n");
    else
    {
        for(i = 0; i < S->top; i++)
            len++;
    }
    return len;
}

清空棧

Status ClearStack(SqStack *S)
{
    S->top = S->bottom;
    return OK;
}

輸出棧

void OutPut_Stack(SqStack *S)
{
    int i;
    int len = 0;
    int j = 0;

    if(S->top == 0)
        printf("This stack is empty!\n\n");
    for(i = 0; i < S->top; i++)
        len++;

    j = len;
    for(i = len - 1; i >= 0; i--)
    {
        printf("第%d個元素為:%d\n\n",j ,S->array[i]);
        j--;
    }
}

在主函式中呼叫以上函式

int main()
{
    int i;
    int a;
    int j = 0;
    SqStack S;
    int res1 = 0;//初始化棧的返回值
    int res2 = 0;//壓入棧的返回值
    int res3 = 0;//獲取棧頂元素的返回值
    int res4 = -1;//彈出棧頂元素的返回值
    int res6 = 0;//清除棧的返回值

    printf("1.初始化一個棧\n2.壓入一個元素\n3.彈出棧頂元素\n4.獲取棧的長度\n5.輸出站\n6.獲取棧頂元素\n7.清空棧\n");

    while(1)
    {
        printf("請輸入操作編號:");
        scanf("%d", &i);
        printf("\n");

        if(i == 1)
        {
            //初始化一個棧
            res1 = Init_Stack(&S);
            if(res1 == 1)
                printf("初始化成功\n\n");
            else
                printf("初始化失敗\n\n");
            res2 = 0;
            j = 1;
        }
        else if(i == 2)
        {
            //壓入元素
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("請插入資料:");
                scanf("%d", &a);
                printf("\n");
                res2 = Push(&S, a);
                if(res2 == 1)
                    printf("元素壓入成功\n\n");
                else
                    printf("元素壓入失敗\n\n");
                res2 = 0;
            }
        }
        else if(i == 3)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------彈出棧頂元素-----------\n\n");
                res4 = Pop(&S);
                if(res4 != -1)
                {
                    printf("棧頂元素彈出成功\n");
                    printf("棧頂元素為:%d\n", res4);
                }
            }

        }
        else if(i == 4)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------獲取棧的長度-----------\n\n");
                StackLength(&S);
            }
        }
        else if(i == 5)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------輸出棧-----------\n\n");
                OutPut_Stack(&S);
            }
        }
        else if(i == 6)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------獲取棧頂元素-----------\n\n");
                res3 = GetTop(&S);
                printf("棧頂元素為:%d\n", res3);
            }
        }
        else if(i == 7)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------清空棧-----------\n\n");
                //清空棧
                res6 = ClearStack(&S);
                if(res6 == OK)
                    printf("清除成功\n");
                else
                    printf("清除失敗\n");
            }

        }
        else
        {
            printf("操作編碼錯誤\n請重新輸入\n");
        }
    }

    return 0;
}

以上就是棧的操作的基本實現,希望能幫到大家