004 隊列的順序存儲

隊列

隊列先進先出,任意一本書上都有詳細的介紹,這裏不廢話了,直接上代碼。

循環隊列-順序存儲方式

#include<iostream>
using namespace std;
#define MaxSize 20
typedef int elemtype;

typedef struct 
{
    elemtype data[MaxSize];
    int front, rear;
}SqQueue;

void InitQueue(SqQueue &q)
{
    q.front = q.rear = 0;
}

bool EmptyQueue(SqQueue &q)
{
    if (q.front == q.rear) 
    {
        cout << "The Queue is empty" << endl;
        return true;
    }
    else
    {
        cout << "Not empty !!" << endl;
        return false;
    }
}

bool EnQueue(SqQueue &q, int x)
{
    if ((q.rear + 1) % MaxSize == q.front)
    {
        cout << "The queue is full !!" << endl;
        return false;
    }
    q.data[q.rear] = x;
    q.rear = (q.rear + 1) % MaxSize;
    cout << "Adding to the queue successful !" << endl;
    return true;
}


bool OutQueue(SqQueue &q)
{
    if (q.front == q.rear) return false;   //隊列是空的,返回錯誤
    elemtype x;
    x = q.data[q.front];
    q.front = (q.front + 1) % MaxSize;
    cout << "Outing from the queue successful !" << x << endl;
    return true;
}

int main()
{
    SqQueue q;
    InitQueue(q);
    EmptyQueue(q);
    EnQueue(q, 22);
    EmptyQueue(q);
    OutQueue(q);
    EmptyQueue(q);
    system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章