1

I'm creating a program that needs to be able to create/handle a number of queues. I used this code to work with 1 queue in programs. If I wanted to add more queues based on this code how exactly do I do it? And will there be a problem if each queue's size is varying depending on random number generation?

typedef int Item;
typedef struct node *link;
struct node{
    Item data;
    link next;
};

int QUEUEempty(link head){
    return head == NULL;
}

void QUEUEput(link *head, link *tail, Item data){
    if (*head == NULL){
        (*tail) = (link)malloc(sizeof(node));
        (*tail)->data = data;
        (*tail)->next = NULL;
        *head = *tail;
        return;
    }
    (*tail)->next = (link)malloc(sizeof(node));
    *tail = (*tail)->next;
    (*tail)->data = data;
    (*tail)->next = NULL;
    return;
}
Item QUEUEget(link *head){
    Item data = (*head)->data;
    link t = *head;
    *head = (*head)->next;
    free(t);
    return data;
}
  • 1
    You create another `link`? What else did you try? How did your attempt work, or not work? – Some programmer dude Sep 16 '16 at 21:14
  • I'm trying to understand how it works theoretically. What do you mean by another link? Can it work like functions being called with a parameter a link to a specific queue? – Dimitris Dimopoulos Sep 16 '16 at 21:21
  • Something like `link queue1 = malloc(sizeof *queue1), queue2 = malloc(sizeof *queue2);` Viola, now you have two queues. You need to initialize them before you use them though. – Some programmer dude Sep 16 '16 at 21:25
  • I see. By initiate you mean just the `head,tail = NULL`, right? And if you want to call QUEUEput for queue1 you do it by `queue1.QUEUEput()` if I understand correctly. Is there a way to use the line you posted in a for where the limit is entered by the user prompted at the start (like `queue[i]`) ? – Dimitris Dimopoulos Sep 16 '16 at 21:31
  • @DimitrisDimopoulos Since it's a pointer, it should be `queu1->QUEUEput()`. – Barmar Sep 16 '16 at 22:01
  • To handle a variable number of queues, you could certainly have an array of pointers. `link **queues = malloc(n * sizeof(*queues));` and then do `queues[i] = malloc(sizeof(*queues[i]));` – Barmar Sep 16 '16 at 22:03
  • This belongs on http://codereview.stackexchange.com/ – chux - Reinstate Monica Sep 17 '16 at 02:28
  • 4
    @chux this would be off-topic on Code Review as the OP is asking a specific how-to question, i.e.: "If I wanted to add more queues based on this code how exactly do I do it?" and not asking for a general code review. If the question is off-topic or too broad for Stack Overflow then please vote accordingly but do not recommend another site unless you are sure it is on-topic on that site. Thank you. – Phrancis Sep 17 '16 at 02:52
  • @Phrancis IMO, the post was on-topic worthy for Code Review - at least it is functional code and OP can consider that option. The issue is not one of what I should do on Stack Overflow - my experience with C on SO lead to a conclusion that you do not agree. Perhaps your experience with C exceeds mine. We simple disagree if this C post is Code Review worthy or not. Thank-you. – chux - Reinstate Monica Sep 17 '16 at 03:16
  • @Joe Wallis OP's post here is good C code, it hasn't a problem w/ "queue's size varying". It merits CR because it lacks such problems. Directing folks to consider CR is a _good thing_ to take their good code to the next step of quality. Various posts are on the edge between a coding problem (this code lacks) and many CR C code posts, 'tho targeted for review, contain far too many function/coding problems. Could have said "C code is good, no major issues, post on Code Review for additional insight." Sorry for my terse-ness of the original comment that did not well convey that thought. – chux - Reinstate Monica Sep 17 '16 at 04:13
  • See [Is it a good idea to `typedef` pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) Your `typedef struct node *link;` had me puzzled for a while, working out how you changed values in the calling code. – Jonathan Leffler Sep 17 '16 at 05:22
  • Understood. Will get my hands on coding and make a new question if needed. Thanks a lot. – Dimitris Dimopoulos Sep 18 '16 at 11:29

0 Answers0