QUEUE(HANG DOI)

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

//QUEUE(HANG DOI) - FIFO(FIRST IN FIRST OUT)

#include "stdafx.h"

#include "conio.h"

struct Nguoi

{

    char Hoten[30];

    int Tuoi;

    char Nghe[100];

};

struct Queue

{

    Nguoi a[100];

};

void Init(Queue &qu)// ham khoi tao

{

    qu.n=0;

}

void EnQueue(Queue &qu, Nguoi x)// them ptu

{

    qu.a[qu.n]=x;

    qu.n++;

}

Nguoi DeQueue(Queue &qu)

{

    Nguoi x= qu.a[0];

    for(int i = 0;i<qu.n-1;i++)

    {

        qu.a[i] = qu.a[i+1];

    }

    qu.n--;

    return x;

}

void ReadQueue(Queue &qu)

{

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

    Nguoi temp;

    while (!feof(f))

    {

        fscanf(f,"%s",&temp.Hoten);

        fscanf(f,"%d",&temp.Tuoi);

        fscanf(f,"%s",&temp.Nghe);

        EnQueue(qu,temp);

    }

    fclose(f);

}

void PrintQueue(Queue &qu)

{

    while (qu.n >0)

    {

        Nguoi kq= DeQueue(qu);

        printf("%s ",kq.Hoten);

        printf("%d ",kq.Tuoi);

        printf("%s

",kq.Nghe);

    }

}

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

{

    Queue qu;

    Init(qu);

    ReadQueue(qu);

    PrintQueue(qu);

    getch();

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: