hamyhoang75
Youtube Master Race
- 3/4/09
- 1
- 0
Mình có bài này, nhờ mọi người giải thích hộ mình thuật toán để chạy là như thế nào. Cảm ơn mọi người nhiều.
Mã:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MaxLength 1000
struct Queue
{
int a[MaxLength+1];
int front,rear;
};
void CreateQ(Queue &Q)
{
Q.front=0;
Q.rear=0;
}
int EmptyQ(Queue Q)
{
if (Q.front==0 && Q.rear==0)
return 1;
else
return 0;
}
void AddQ(Queue &Q, int x)
{
if (EmptyQ(Q))
{
Q.front=Q.rear=1;
Q.a[Q.rear]=x;
}
else
{
Q.rear++;
Q.a[Q.rear]=x;
}
}
void RemoveQ(Queue &Q, int &x)
{
if (EmptyQ(Q))
cout<<"\n hang doi rong!:";
else
{
x=Q.a[Q.front];
if (Q.front==Q.rear)
Q.front=Q.rear=0;
else
Q.front++;
}
}
void indao(int n)
{
Queue q;
CreateQ(q);
int x;
while (n>0)
{
AddQ(q, n%10);
n=n/10;
}
while(!EmptyQ(q))
{
RemoveQ(q,x);
cout<<x<<" ";
}
}
void main()
{
int n;
clrscr();
cout<<"Nhap so nguyen duong n:\t"; cin>>n;
cout<<"\nSo n in nguoc la:\t";
indao(n);
getch();
}
