数据结构——队列的基本操作

来源:互联网 发布:汽车电路软件下载 编辑:程序博客网 时间:2024/06/04 04:53

循环队列:

#include<bits/stdc++.h>#define N 5 //为了测试是否能循环,开的比较小using namespace std;typedef int QElemTyp;bool f;struct SqQueue{    QElemTyp *base;    int front;    int rear;};void init(SqQueue &q){    q.base = (QElemTyp*)malloc(sizeof(QElemTyp) * N);    if(!q.base) exit(0);    q.front = q.rear = 0;    f = false;}void menu(){    printf("                1.入队\n");    printf("                2.出队\n");    printf("                3.是否为空\n");    printf("                4.遍历\n");    printf("                5.退出\n");}void push(SqQueue &q){    if((q.front + N) % N == q.rear && f) {printf("队列满了, 入队失败\n"); return ;}    printf("请输入\n");    QElemTyp num;    scanf("%d", &num);    *(q.base + (q.rear + N) % N) = num;    q.rear = (q.rear + 1 + N) % N;    f = true;}void pop(SqQueue &q){    if(q.front == q.rear && !f) { printf("空队列\n"); return ;}    q.front = (q.front + 1 + N) % N;    if(q.front == q.rear) f = false;}void isEmpty(){    if(f) printf("NO\n");    else printf("YES\n");}void display(SqQueue q){    if(!f) {printf("空队列\n"); return ;}    while(true)    {        printf("%d ", *(q.base + q.front));        q.front = (q.front + 1 + N) % N;        if(q.front == q.rear) break;    }    cout << endl;}int main(){    SqQueue q;    init(q);    while(true)    {        menu();        int sel;        scanf("%d", &sel);        switch(sel)        {            case 1 : push(q); break;            case 2 : pop(q); break;            case 3 : isEmpty(); break;            case 4 : display(q); break;        }        if(sel == 5) break;    }    return 0;}


链队列:

#include<bits/stdc++.h>#define N 5using namespace std;typedef int QElemType;typedef struct QNode{    QElemType data;    struct QNode *next;}QNode, *QueuePtr;typedef struct{    QueuePtr  front;    QueuePtr  rear;}LinkQueue;void init(LinkQueue &q){    q.front = (QueuePtr)malloc(sizeof(QNode));    q.front->next = NULL;    q.rear = q.front;}void menu(){    printf("\t1.入队\n");    printf("\t2.出队\n");    printf("\t3.是否为空\n");    printf("\t4.遍历\n");    printf("\t5.退出\n");}void push(LinkQueue &q){    printf("请输入要入队的元素:\n");    QueuePtr now = (QueuePtr)malloc(sizeof(QNode));    scanf("%d", &now->data);    now->next = NULL;    q.rear->next = now;    q.rear = now;}void pop(LinkQueue &q){    if(q.front == q.rear)    {        printf("空队!\n");        return ;    }    QueuePtr s = q.front->next->next;    free(q.front->next);    q.front->next = s;    if(s == NULL) q.rear = q.front;}void isEmpty(LinkQueue q){    if(q.front == q.rear) puts("Yes");    else puts("No");}void display(LinkQueue q){    if(q.front == q.rear)    {        printf("空队!\n");        return ;    }    printf("从队头到队尾遍历:\n");    q.front = q.front->next;    while(q.front)    {        printf("%d ", q.front->data);        q.front = q.front->next;    }    cout << endl;}int main(){    LinkQueue q;    init(q);    while(true)    {        menu();        int sel;        scanf("%d", &sel);        switch(sel)        {            case 1 : push(q); break;            case 2 : pop(q); break;            case 3 : isEmpty(q); break;            case 4 : display(q); break;        }        if(sel == 5) break;    }    return 0;}


原创粉丝点击