queue——问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
 
typedef struct node{
    int data;
    int qnum;
    struct node * next;
}node;
 
typedef struct Linkqueue{
    node * front;
    node * rear;
}Linkqueue;
 
Linkqueue * Init(){
    Linkqueue * Q;
    node * p;
    p=(node *)malloc(sizeof(node));
    p->next=NULL;
    Q=(Linkqueue*)malloc(sizeof(Linkqueue));
    Q->front=Q->rear=p;
    return(Q);
}
 
int enQueue(Linkqueue * Q, int data,int qnum){
    node *p;
    p=(node *)malloc(sizeof(node));
    if(!p) return -1;
    p->data=data;
    p->qnum=qnum;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;
    return 1;
}
int inQueue(node * q, int data,int qnum){
    node * m;
    m->next=q->next;
    q->next=m;
    m->qnum=qnum;
    m->data=data;
    return 1;
}
 
int dequeue(Linkqueue * Q){
    node *p;
    int x;
    if(Q->front==Q->rear)
        return -1;
    p=Q->front->next;
    x=p->data;
    if(p==Q->rear)
        Q->rear=Q->front;
    free(p);
    return x;      
}
int map [5][10];
int main(){
    int times;
    int que;
    Linkqueue * Q;
    node * m;
    m->next=NULL;
    Q->front=Q->rear=m;
    scanf("%d%d",&times,&que);
    for(int i=1;i<=que;i++){
        int num;
        scanf("%d",&num);
        map[i][0]=num;
        for(int j=1;j<=num;j++){
            scanf("%d",map[i][j]);
        }
    }
    char s[100];
    int data;
    int count=0;
    while(scanf("%s",&s)){
        if(strcmp(s,"ENQUEUE")==0){
            scanf("%d",data);
            int flag=0;
            for(int i=1;i<=que;i++){
                for(int j=1;j<=map[i][0];j++)
                    if(map[i][j]==data){
                        for(int z=1;z<=count;z++){
                                node *q;
                                q=Q->front->next;
                                if(q->qnum==i&&q->next->qnum!=i){
                                    inQueue(q,data,i);
                                    count++;
                                    flag=1;
                                    break;
                                }
                                q=q->next;
                            }
                        if(flag==0){
                            enQueue(Q,data,i);
                            count++;
                            flag=1;
                            break;
                        }
                    }
                    if(flag==1)
                        break;
                }
            }
        else if(strcmp(s,"DEQUEUE")==0){
            int x;
            x=dequeue(Q);
            count--;
            printf("%d\n",x);
        }
        else if (strcmp(s,"STOP")==0)
            break;
    }
}

  

posted @   Mr.Struggle  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
阅读排行:
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现
点击右上角即可分享
微信分享提示