Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

STACK(LIFO)

// STACK.cpp : Defines the entry point for the console application.

//STACK(LIFO)

#include "stdafx.h"

struct stack

{

    int a[100];

};

//struct nguoi

//{

//    char[30] hoten;

//    int tuoi;

//}

void Init(stack &st)

{

    st.n=0;

}

void push(stack &st,int x)

{

    if(st.n<100)

        st.a[st.n]=x;

        st.n++;

}

int pop(stack &st)

{

    int kq =st.a[st.n-1];

    st.n--;

    return kq;

}

void nhapstack(stack &st)

{

    FILE *f=fopen("input.txt","rt");

    while (!feof(f))

    {

        fscanf_s(f,"%d",&temp);

        push(st,temp);

    }

    fclose(f);

}

void xuatstack(stack &st)

{

    while (st.n > 0)

    {

        int kq=pop(st);

        printf("%d",kq);

    }

}

int _tmain(int argc, _TCHAR* argv[])

{

    stack st;

    Init(st);

    nhapstack(st);

    xuatstack(st);

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: