简体   繁体   English

两个队列一个程序

[英]two queues one program

I want to create two queues of ordinary and vip clients in a bank, 1.How can i create two queues in one program. 我想在银行中创建两个普通客户和VIP客户的队列1.如何在一个程序中创建两个队列。 2.How can i pass the structure for queues as parameter of function enqueue and dequeue? 2.如何将队列结构作为函数入队和出队的参数? I am enquing according to customer type eg i enqueue to queue1 if client is vip i enqueue to queue2 if client is ordinary same for dequeue 我根据客户类型进行查询,例如,如果客户端是vip,则排队到queue1;如果客户端与出队普通相同,则排队到queue2

#include <stdio.h>
#include <malloc.h>
#include<string.h>

int position=0;
int length=1;enter code here

typedef struct Node
{
    int record;
    int CardNum;
    char CustomerType[20];
    struct Node* next;

}Node;

//VIP QUEUE
typedef struct queue
{
    Node* front1 = NULL;
    Node* rear1 = NULL;
}Queue1;

//Ordinary QUEUE
typedef struct queue
{
    Node* front2 = NULL;
    Node* rear2 = NULL;
}Queue2;

void Enqueue();
void Dequeue();

int main()
{
    char command[10];
    while(scanf("%s",command))
    {
        if(strcmp(command,"IN") == 0)
        {
            printf("IN:");
            Enqueue();
        }
        if(strcmp(command,"LIST") == 0)
        {
            printf("LIST:\n");
            List();
        }
        if(strcmp(command,"OUT") == 0)
        {
            Dequeue();
        }
        if(strcmp(command,"QUIT") ==0)
        {
            printf("GOOD BYE!\n");
            break;
        }
    }
    return 0;
}

Well the thing is you have created two different Queue types. 好了,您已经创建了两种不同的Queue类型。 But it shouldn't be the case. 但事实并非如此。 After all the queue we use to store VIP's will be same type as that of another. 在我们用于存储VIP的所有队列之后,将与另一个队列具有相同的类型。 But yes you need two different instance of that Queue type. 但是,是的,您需要该队列类型的两个不同的实例

Moreover you can't initialize members inside the struct declaration. 此外,您不能在struct声明中初始化成员。 This won't work. 这行不通。

For passing the Queue to another function there are several ways. 为了将Queue传递给另一个函数,有几种方法。 You can simply create Queue q1,q2 and then pass it's address to the function to allocate necessary nodes. 您可以简单地创建Queue q1,q2 ,然后将其地址传递给函数以分配必要的节点。

typedef struct queue
{
    Node* front;
    Node* rear;
}Queue;

Queue q1,q2;
init(&q1);
init(&q2);
...

void init(Queue *q1){
    q1->front = NULL;
    q2->rear  = NULL;
}

The signature of the init would be init的签名是

void init(Queue *q);

Whatever I have shown here in init you can do the same with the other queue functions. 无论我在init显示的内容如何,​​您都可以对其他队列功能执行相同的操作。

In fact moreover you can do pass not the address and then make changes to the local struct instance passed to the function and then return it. 此外,实际上,您可以不传递地址,然后对传递给函数的本地struct实例进行更改,然后返回它。

Other points that I can't ignore: 我不能忽略的其他几点:

You have used scanf and checked it's return value in one way - do it proper way. 您已经使用scanf并以一种方式检查了它的返回值-以正确的方式进行操作。

while(scanf("%9s",command) == 1){
   ...

Also another point is using multiple if with same condition. 另外还有一点就是使用多if用同样的条件。 You should merge the if statements - saves you from repetitive code and also you are not doing a redundant check. 您应该合并if语句-避免重复代码,而且您也没有进行多余的检查。


You could have initialized the structure like this also (But I have shown the other way so that you can write the other queue functions). 您也可以像这样初始化结构(但是我已经展示了另一种方式,以便您可以编写其他队列函数)。

Queue q1 = { .front = NULL, .rear = NULL };

Also another thing the signature for the main should be int main(void) . 另外, main的签名应该是int main(void)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM