Multiple Queues in C

user3838436

I have a basic queue design, but I want to have multiple queues. The way it looks right now is that I would need another queue.h file and replace head and tail with different names, but I am sure there is a better way?

queue.h *Edited

#include<stdlib.h>  // malloc

struct Node {
    int data;
    struct Node* next;
};

struct Queue {
  struct Node *head, *tail;
};

struct Queue *QueueInit() {
    //allocate and initialize a queue
    struct Queue *thisQueue = malloc(sizeof *thisQueue);
    thisQueue->head = NULL;
    thisQueue->tail = NULL;
    return thisQueue;
}


void push(struct Queue *myQueue, int x) {
    struct Node *temp; 
    temp = malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = NULL;  
    if(myQueue->head == NULL && myQueue->tail == NULL) { //empty
        myQueue->head = myQueue->tail = temp;
        return;
    }
    myQueue->tail->next = temp;
    myQueue->tail = temp;
}

void pop(struct Queue *myQueue) {
    struct Node* temp = myQueue->head;
    if(myQueue->head == NULL) return;   //empty
    if(myQueue->head == myQueue->tail) {
        myQueue->head = myQueue->tail = NULL;
    }
    else {
        myQueue->head = myQueue->head->next;
    }
    free(temp);
}

How can I create multiple queues like this?

main.c

int main() {
    struct Node iceCreamLine;
    struct Node bathroomLine;

    iceCreamLine.push(13);
    bathroomLine.push(2);


    //It looks like I will have to use this syntax then instead?
    struct Queue *droneQueue;    //(THIS IS LINE 5)
    push(&droneQueue,1666);
    push(&droneQueue,100);

    printf("--> %d",&droneQueue->head->data);
    printf("--> %d",&droneQueue->head->next->data);

}

The first printf works, but the second one gives me a segmentation dump. Also here are the warnings

main.c: In function ‘main’: main.c:6:2: warning: passing argument 1 of ‘push’ from incompatible pointer type [enabled by default] In file included from queue.c:2:0: queue.h:21:6: note: expected ‘struct Queue *’ but argument is of type ‘struct Queue **’ main.c:7:2: warning: passing argument 1 of ‘push’ from incompatible pointer type [enabled by default] In file included from queue.c:2:0: queue.h:21:6: note: expected ‘struct Queue *’ but argument is of type ‘struct Queue **’ main.c:9:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat] main.c:10:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]

Politank-Z
struct Queue {
  struct Node *head, *tail;
};

Add a QueueInit function to allocate and initialize a queue, returning a pointer to a struct Queue. Pass a pointer to struct Queue to push and pop, and get rid of your global head and tail.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create celery multiple queues

From Dev

RabbitMQ configuration for multiple queues

From Dev

RabbitListener multiple queues behavior

From Dev

RabbitListener multiple queues behavior

From Dev

About multiple conversations and/or queues

From Dev

Multiple serial queues, UI not updating

From Dev

spring jmsListener to listen to multiple Queues

From Dev

Java: Executor Service with multiple queues

From Dev

Is cryptsetup benchmark for single or multiple queues

From Dev

one listener for multiple azure queues

From Dev

multiple workers and multiple queues on celery django daemon

From Dev

C language and Queues/linked lists

From Dev

Queues in iOS Objective-C

From Dev

c - kernel - spinlocks vs queues

From Dev

Debugging C++ queues in an IDE

From Dev

AWS multiple SQS queues and workers optimal design

From Dev

Multiple queues causing TF to lock up

From Dev

How to keep multiple independent celery queues?

From Dev

AMQP routing messages to multiple queues in order

From Dev

Consume multiple ActiveMQ queues via the same ActiveMQConnectionFactory

From Dev

Using multiple Queues instead of one PriorityQueue

From Dev

How to listen to multiple queues with autowired Spring Boot?

From Dev

NServiceBus Single Process, but Multiple Input queues

From Dev

Django/Celery multiple queues on localhost - routing not working

From Dev

Tensorflow Race conditions when chaining multiple queues

From Dev

Multiple input queues in one rebus process

From Dev

spring-amqp Multiple queues with different routingKey

From Dev

Consuming from multiple queues at once with aioamqp

From Dev

Publishing to multiple Rebus queues with different stores

Related Related

HotTag

Archive