2

I am new to C. I have no idea about how to write a C function which creates an empty queue and return a void pointer.

void* queue_open(void)

I also want to know how to write a C function which puts an element at end of a queue.

void queue_put(void *p, void *elementp)

Thanks for your help!

6
  • 5
    Is this homework? What does your reference material say about implementing linked lists? Commented Sep 28, 2011 at 19:29
  • Yes, it is my homework. It's basically as me to create a shopping list to take entries from the keyboard. Each entry is composed of an item to purchase (string) and the number of items to purchase (int). After all items have been entered, print out the shopping list, remove all entries from the queue, close the queue. The home work as us to use modular programming, such as function creates an empty queue. I have main function done using functions like queue_open and queue_put. Now, I don't know how to write those functions. Commented Sep 28, 2011 at 19:42
  • What did you learn in class? Do you have reference material? Textbooks? Surely you can find linked list code on the web. Commented Sep 28, 2011 at 19:46
  • I am taking a lab based programming course, without textbooks. Every week, there is an assignment(coding subject). I have to learn by myself. Commented Sep 28, 2011 at 19:58
  • 1
    Not sure about TDD. Surely you need to learn the basics before that! Commented Sep 28, 2011 at 20:00

3 Answers 3

2

If you are coming from an object oriented background (as your method signatures seem to indicate).

Object oriented idea -> good way to do it in C

Object creation -> malloc a struct, then pass it into an initialization function

struct queue* q = (struct queue*)malloc(sizeof(struct queue));
queue_initialize(q);

if you want, you can wrap this in a function, like so

struct queue* queue_construct() {
  struct queue* q = (struct queue*)malloc(sizeof(struct queue));
  queue_initialize(q);
  return q;
}

Note that these pointer shouldn't point to void*, let C do at least some of the type checking for you.

Implement a method -> create a function that takes a struct pointer to the "almost this" struct.

struct user* user = ... whatever we do here ...;
queue_add(q, (void*)user);

As far as how to actually implement a queue, I suggest a good data structures or algorithms book, as there are many ways to go about it; and, the specific techniques you choose will have different impacts on performance and reliability. There's no one best way, it depends heavily on how the queue is to be used, and which aspects of performance are more important.

The book I recommend is Introduction to Algorithms. This book is overkill for most situations, with very detailed listings of nearly every major data structure you are likely to encounter in the first few years of programming. As such, it makes a great reference, despite its attempt at a language neutral approach, which now looks odd when compared to common programming languages.

Once you understand what is going on, you can do it in nearly any language.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to decide what a queue element should look like, what a queue is, and what it means for a queue to be empty. If you know those things, writing queue_open and queue_put should be pretty easy. I'd suggest that you start by defining a structure that represents your queue element.

3 Comments

here is my structure: typedef struct item { int num; char name[MAXSTR]; } Item;
That could be an element in your queue, but what makes it a queue? What's the relationship between two consecutive elements in a queue, and how do you plan to create that relationship? Here's a hint: two basic structures that can implement a queue are linked list and array.
Right. If you're using a linked list, then each item needs a next pointer, and a pointer to a queue is just a pointer to the first element. If you use an array, you don't need the next pointer, but you have to pre-allocate the array of items, and you need a way to remember which item is the first one and how many items are currently in the queue.
0

You can learn about queues here:

http://en.wikipedia.org/wiki/Queue_(data_structure)

While you could easily copy and paste the sample code from the link above and with little modification solve your homework problem, you are not going to learn a lot by doing that.

After understanding a queue conceptually, I recommend you try to implement it yourself, then use the sample code from the link above as a reference when you get stuck.

The best thing you could do is pair up with another student in your class who is smarter than you. Then pair program ( http://en.wikipedia.org/wiki/Pair_programming ) with him/her to solve the problem. You'll become a better programmer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.