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

循环队列:

#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 5

using 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;
}



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值