11

Problem: to find the right data structure for the queue:

 #include <stdio.h>                                                                                                                                                             
 #include <stdlib.h>
 #include <stdarg.h>
 #include <time.h>

 int main(int argc, const char *argv[])
 {
     Queue q;
     ch ='A';
     for (int k = 0; int k < 4; int k++) {
         q.addQ(ch);
         ch++;
         q.addQ(ch);
         ch=q.front();
         q.removeQ();
     }
     return 0;
 }

I tried to compile it, but the Queue is undeclared:

$ gcc -o Qu_1 -g q_queue.c
q_queue.c: In function 'main':
q_queue.c:8: error: 'Queue' undeclared (first use in this function)

Question: What is the library for the basic data structures such as the queue in the example?

coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • Searching like this: http://stackoverflow.com/search?q=[c]+library+data+structures gets http://stackoverflow.com/questions/305611/container-class-library-for-c and http://stackoverflow.com/questions/890065/need-a-general-purpose-library-pure-c and http://stackoverflow.com/questions/668501/are-there-any-open-source-c-libraries-with-common-data-structures which might help. – dmckee --- ex-moderator kitten Jul 23 '09 at 18:00
  • 1
    And one of those points all the way back to http://stackoverflow.com/questions/2540/good-stl-like-library-for-c – dmckee --- ex-moderator kitten Jul 23 '09 at 18:13

4 Answers4

14

This looks like a good candidate for the TAILQ_* ?

#include <sys/queue.h>

"man queue" will give more details - there are simple lists, tail queues and circular queues there. Those are the macros that you'd need to bolt on your own structures, not classes of course.

The code for your scenario will look something like this (I should have made the add_to_queue return some code to check for error, and avoid global vars too, but hopefully I would be forgiven in this example):

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>

TAILQ_HEAD(tailhead, entry) head;

struct entry {
  char c;
  TAILQ_ENTRY(entry) entries;
};

void add_to_queue(char ch) {
  struct entry *elem;
  elem = malloc(sizeof(struct entry));
  if (elem) {
    elem->c = ch;
  }
  TAILQ_INSERT_HEAD(&head, elem, entries);
}

int main(int argc, char *argv[]) {
  char ch = 'A';
  int i;
  struct entry *elem;

  TAILQ_INIT(&head);
  for (i=0; i<4; i++) {
    add_to_queue(ch);
    ch++;
    add_to_queue(ch);

    elem = head.tqh_first;
    TAILQ_REMOVE(&head, head.tqh_first, entries);
    free(elem);
  }
  exit(0);
}
Andrew Y
  • 5,107
  • 2
  • 28
  • 29
5

A great option is to use the C Generic Library. This is a C library modeled (loosely) on the C++ STL. It provides a queue struct, lists, etc.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

There isn't one in the standard C library; tracing back through the related posts, it looks like the Queue data structure was meant to be written by the student.

Unlike C++, the standard C library does not provide a standard set of containers.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-12

First off, you need C++, not C. C is not an object-oriented language, and it doesn't have standard libraries for things like queues.

For C++, look for the std::queue.

#include <queue>

int main(int argc, const char *argv[])
{
     std::queue q;
     /// etc;
}

You can, of course, make queue-like structure in C, but you'll wind up doing a lot of the work yourself. See the answer below about the TAILQ_ macros.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169