Monday 15 March 2010

Multiple Queues in C -



Multiple Queues in C -

i have basic queue design, want have multiple queues. way looks right need queue.h file , replace head , tail different names, sure there improve 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 , initialize queue struct queue *thisqueue = malloc(sizeof *thisqueue); thisqueue->head = null; thisqueue->tail = null; homecoming 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 create multiple queues this?

main.c

int main() { struct node icecreamline; struct node bathroomline; icecreamline.push(13); bathroomline.push(2); //it looks have utilize syntax instead? struct queue *dronequeue; //(this line 5) push(&dronequeue,1666); push(&dronequeue,100); printf("--> %d",&dronequeue->head->data); printf("--> %d",&dronequeue->head->next->data); }

the first printf works, sec 1 gives me segmentation dump. here warnings

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

struct queue { struct node *head, *tail; };

add queueinit function allocate , initialize queue, returning pointer struct queue. pass pointer struct queue push , pop, , rid of global head , tail.

c queue

No comments:

Post a Comment