C++实现简单链式队列

#include 

using namespace std;

typedef int elemType;
typedef struct qNode {
    elemType data;
    struct qNode *next;
}qNode, *Qptr;

typedef struct {
    qNode *front;
    qNode *rear;
}linkQueue;

bool initQueue(linkQueue &q) {
    q.front = q.rear = new qNode;
    if (!q.front) return false;
    q.front->next = NULL;
    return true;
}

bool enQueue(linkQueue &q, int e) {
    Qptr s = new qNode;
    if (!s) return false;
    s->data = e;
    s->next = NULL;
    q.rear->next = s;
    q.rear = s;
    return true;
}

bool deQueue(linkQueue &q, int &e) {
    if (q.rear == q.front) {
        return false;
    }
    Qptr s;
    s = q.front->next;
    e = s->data;
    q.front->next = s->next;
    if (q.rear == s) q.rear = q.front;
    delete s;
    return true;
}

bool getHead(linkQueue q, int &e) {
    if (q.rear != q.front) {
        e = q.front->next->data;
        return true;
    }
    return false;
}

int main() {
    linkQueue q;
    initQueue(q);
    cout << "Put 1,2,3,4,5 in queue." <

Output:

Put 1,2,3,4,5 in queue.
Get front element of the queue:1
Dequeue all element: 1 2 3 4 5

你可能感兴趣的:(C++实现简单链式队列)