棧結構 c

來源:互聯網
上載者:User

 

#include <stdlib.h>
#include <stdio.h>

#define MAXLEN 50

typedef struct
{
    char name[10];
    int age;
}DATA;

typedef struct stack
{
    DATA data[MAXLEN+1];
    int top;
}StackType;

StackType *STInit()
{
    StackType *p;
    if(p=(StackType *)malloc(sizeof(StackType)))
    {
        p->top=0;
        return p;
    }
    return NULL;
}

int STIsEmpty(StackType *s)
{
    int t;
    t=(s->top==0);
    return t;
}

int STIsFull(StackType *s)
{
    int t;
    t=(s->top==MAXLEN);
    return t;
}

void STClean(StackType *s)
{
    s->top=0;
}
void STFree(StackType *s)
{
    if(s)
    {
        free(s);
    }
}

int PushST(StackType *s,DATA data)
{
    if(s->top+1>MAXLEN)
    {
        printf("棧溢出!\n");
        return 0;
    }
    s->data[++s->top]=data;
    return 1;

}
DATA PopST(StackType *s)
{
    if(s->top==0)
    {
        printf("棧為空白!\n");
        exit(0);
    }
    return s->data[s->top--];
}
DATA PeekST(StackType *s)
{
    if(s->top==0)
    {
        printf("棧為空白!\n");
        exit(0);
    }
    return s->data[s->top];
}

int main()
{
    StackType *stack;
    DATA data,data1;

    stack=STInit();
    printf("入棧操作:\n");
    printf("請輸入 姓名 年齡入棧操作:");
    do
    {
        scanf("%s%d",data.name,&data.age);
        if(data.age==0)
        {break;}
        else{
            PushST(stack,data);
        }
    }while(1);

    //printf("(%s,%d)",PeekST(stack).name,PeekST(stack).age);
    do
    {
        printf("\n出棧操作:按任意鍵進行出戰操作:");
        getchar();
        data1=PopST(stack);
        printf("出棧的資料是:(%s,%d)\n",data1.name,data1.age);
    }while(1);

    STFree(stack);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.