Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

CHUYỂN ĐỔI DỮ LIỆU TỪ STACK SANG QUEUE

12:43 28-03-2018 722 lượt xem 0 bình luận

Đề bài yêu cầu chuyển dữ liệu từ stack sang queue. Em viết như thế này mà code không chạy, em không biết sửa làm sao, anh/chị/bạn giúp em với ạ.

http://codepad.org/nzBoYKKs

#include <iostream>
#define Max 100

using namespace std;
typedef int item;

struct Stack
{
    int Top;
    item Data[Max];
};
struct Queue
{
    int Front, Rear;
    item Data[Max];
    int count;
};
void Init (Stack &S)
{
    S.Top = 0;
}

void Create (Queue &Q)
{
    Q.Front=0;
    Q.Rear=-1;
    Q.count=0;
}

int Isempty(Stack S)
{
    return (S.Top == 0);
}

int Isempty (Queue Q)
{
    if (Q.count == 0)
        return 1;
    return 0;
}

int Isfull(Stack S)
{
    return (S.Top == Max);
}

int Isfull (Queue Q)
{
    if (Q.count == Max)
        return 1;
    return 0;
}

void Push(Stack &S, item x)
{
    if (!Isfull(S))
    {
        S.Data[S.Top] = x;
        S.Top ++;
    }
}

void EnQueue(Queue &Q, item x)
{
    if (!Isfull(Q))
    {
        Q.Data[++Q.Rear] = x;
        Q.count++;
    }
    else cout<<"Queue day!";
}

int Pop(Stack &S)
{
    if (!Isempty(S))
    {
        S.Top--;
        return S.Data[S.Top];
    }
}

void Input (Stack &S)
{
    int n;
    item x;
    do
    {
        cout<<"Nhap so phan tu cua Stack :";
        cin>>n;
    } while (n>Max || n<1);
    for (int i=0; i<n; i++)
    {
        cout<<"Nhap phan tu thu "<<i+1<<":" ;
        cin>>x;
        Push(S,x);
    }
}

void stackToQueue (Stack &S,Queue &Q)
{
    item x;
    for (int i=0;i<=Max;i++)
    {
        S.Data[S.Top]=x;
        S.Top--;
        EnQueue(Q,x);
    }
}

void Output(Queue Q)
{
    if (Isempty(Q)) cout<<"Hang doi rong !";
    else
    {
        for (int i=Q.Front; i<=Q.Rear; i++)
            cout<<Q.Data[i];
    }
}

int main()
{
    Stack S;
    Init(S);
    Queue Q;
    Create(Q);
    Input(S);
    stackToQueue(S,Q);
    Output(Q);
    return 0;
}
 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

Câu hỏi mới nhất