栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

7-2 队列操作 (300 分) PTA c语言

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7-2 队列操作 (300 分) PTA c语言

请实现一个MyQueue类,实现出队,入队,求队列长度.

实现入队函数 void push(int x); 实现出队函数 int pop(); 实现求队列长度函数 int size();

#include 
#include "malloc.h"
typedef struct QNode{
    int data;
    struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
    QueuePtr front, rear;
}linkQueue;
void push(int e, linkQueue *q){
    QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
//    if (!s)
//        exit(0);
    s->data = e;
    s->next = NULL;
    q->rear->next = s;
    q->rear = s;
}
int pop(linkQueue *q, int e){
    QueuePtr p = {0};
    if (q->front == q->rear) {
        printf("Invalidn");
        return 0;
    }
    p = q->front->next;
    e = p->data;
    q->front->next = p->next;
    if (q->rear == p)
        q->rear = q->front;
    free(p);
    printf("%dn", e);
}
int size(linkQueue q){
    int count = 0;
    if (q.front == q.rear)
        return count;
    QueuePtr p = q.rear->next;
    while(1){
        if (p == q.rear)
            break;
        p = q.front->next;
        count++;
    }
    return count;
}
int main() {
    int n, a1;
    int ope;
    int x;
    linkQueue q = {0}, *q1;
    q.front = q.rear = (QueuePtr)malloc(sizeof(QNode));
    q.rear->data=0;
    q.front->next = NULL;
    q1 = &q;
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        scanf("%d", &ope);
        switch(ope){
            case(1):
                scanf("%d", &x);
                push(x, q1);
                break;
            case 2:
                pop(q1, x);
                break;
            case 3:
                a1 = size(q);
                printf("%dn", a1);
                break;
        }
    }
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号