![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 37
| Help with FIFO queue Code: /* file: myqueue.h -- definitions for queue manipulation routines. */
#ifndef QUEUE_DEFINED
#define QUEUE_DEFINED
typedef struct qitem {
struct qitem *next;
char *data;
} Qitem;
typedef struct queue {
Qitem *front, *back;
} MyQueue;
int queue_init(MyQueue *q); /* return -1 on error */
int queue_empty(MyQueue *q); /* return 1 if empty; else 0 */
int queue_put(MyQueue *q, Qitem *new_item); /* return -1 on error */
Qitem *queue_get(MyQueue *q); /* return (Qitem *)0 if the queue is empty */
#endif
/* end file: myqueue.h */
Tasks 1. Create a new file myqueue.c and #include the header file shown below. 2. Implement the functions listed in myqueue.h (see below) 3. Write a main() function that uses the functions from myqueue.h, and allows the user to: * Create new queues. * Add an element to a specified queue. * Remove an element and display its data. * Display the number of elements in a queue. * Display an entire queue, without removing elements. 4. Ensure your program is robust at dealing with errors such as attempting to remove an element where none exist. 5. Ensure that all memory which was malloc'd is free'd before your program exits Note that the queue_*() functions should not refer at all to the data field of Qitem. I understand it mostly but unsure how to start. Mainly where and how I initialise the "q" variable which is passed to several of the functions in the header file. Especially given that I don't know how many queues the user will want to create. I have a switch statement coded ready to select 1 to create a queue but am unsure of where to go from here. As I say I am looking for help, not for the work to be done for me. Any help really appreciated |
| Martin_T is offline | |
| | #2 |
| Registered User Join Date: Feb 2009
Posts: 35
| I think you should begin by first writing each of the functions in the header file, dont worry about the main for now. you might also like to write a test function so that you know your functions are working as desired. what does this function do? Code: int queue_init(MyQueue *q); |
| Brain_Child is offline | |
| | #3 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| Quote:
That said, you should probably just declare a MyQueue variable (not a pointer), and pass its address into the queue_init function, which will set both members to NULL. From there I would find a good tutorial on building and working with linked lists. See where that takes you and get back to us.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
| | #4 | |
| Registered User Join Date: Nov 2009
Posts: 37
| Quote:
I think it initialises the queue somehow. It is not clear,I've pretty much pasted in the information I have. | |
| Martin_T is offline | |
| | #5 | |
| Registered User Join Date: Nov 2009
Posts: 37
| Quote:
Anyway, as I say i'm just looking for guidance like this, not answers. I'll try and make a start and let you know how it goes. | |
| Martin_T is offline | |
| | #6 | |
| Registered User Join Date: Nov 2009
Posts: 37
| I think with me not even being able to do this (last weeks work) I might see if I can drop this module. I still can't get started. The queue structure requires a *front and *back element to initialise as you will see from the above code. You will also see the initialise function only takes in one parameter "q". So I have no idea how I will initialise a front and back node given that we are told Quote:
I really have no clue. | |
| Martin_T is offline | |
| | #7 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,931
| When you initialize the queue, there won't be any forward node or backward node to point to, so set them to null. (aka, initialize them).
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #8 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,931
| The queue_init function takes a pointer to an anchor that you will define in main. This is the variable you will use to anchor your queue all throughout your program (just like iMalc said). If you've worked with scanf(), you would pass the address of your anchor just like you would pass the address of a receiving variable in scanf().
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #9 |
| Registered User Join Date: Nov 2009
Posts: 37
| Firstly, thankyou for your time and for replying. It is REALLY appreciated Ok, this is beginning to make sense now..... I think So I could declare a queue item as simply as Code: MyQueue queue1; Code: queue_init(queue1) Code: *q = (MyQueue*)malloc(sizeof(MyQueue)); q->front=NULL; q->back=NULL; |
| Martin_T is offline | |
| | #10 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Your are close. Here's a few technical details to consider. This is your prototype: Code: int queue_init(MyQueue *q); Now, you have two choices. #1, if you want to declare and pass in a "MyQueue" like this: Code: MyQueue queue1; queue_init(&queue1); The other choice is to declare and submit a pointer: Code: MyQueue *queue1; queue_init(queue1); There is, I think, a significant issue to consider. Quote:
Main() should contain the "user interface", ie, a simple menu of choices in the form of a switch/case, one of which is going to be "create new queue". Furthermore, you will also need a way to "select active queue" unless you intend to offer a choice of available queues everytime the user wants to add an item to a queue, etc (getting to sound like a lot of work, isn't it?) IMO, your prof made a bad call not including some kind of char "label" in the MyQueue struct. Without that, you will have to keep track of the user's "name" for each queue seperately, perhaps a parallel array of these: Code: typedef struct _listing {
MyQueue *que;
char label[32];
} QueueTag;
Code: int i;
MyQueue **queues=NULL, **tmp=NULL;
for (i=0; i<6; i++) {
tmp = realloc(queues,(i+1)*sizeof(MyQueue*));
if (tmp) queues = tmp;
else return -1; /* handle error */
queues[i] = malloc(sizeof(MyQueue));
queues[i]->front = NULL;
queues[i]->back = NULL;
}
Some things to note:
Quote:
If you are studying programming, I presume you will have to redo this in time. Also, if you are studying programming, IMO you better deal with this now, it involves a number of essential and fundamental concepts in C.If you are not studying programming, you may want to consider that this looks like a labour intensive course and whether you can eat everything you put on your plate.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 11-08-2009 at 09:59 AM. | ||
| MK27 is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 37
| Thankyou so much for your time. You have made the issue alot clearer. I am going to sit down with this, read over it again with the code in front of me and see where I get to. It will be alot further now thanks to your time and effort. p.s. I am not studying programming soley. I have done two semesters of Java last year and this is the only C programming I am or will be doing for the remained of the course. The course lasts until December and the remainder of the C programming will be based on networks. We are not taught any C, just set a task per week along with a very small handout explaining a few basic concepts we will need to know. For the % of the course it is worth it is VERY time consuming for me. Thanks again |
| Martin_T is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory Fragmentation with Dynamic FIFO Queue | fguy817817 | Linux Programming | 17 | 10-31-2009 04:17 AM |
| Help with FIFO QUEUE | jackfraust | C++ Programming | 23 | 04-03-2009 08:17 AM |
| Fixing my program | Mcwaffle | C Programming | 5 | 11-05-2008 03:55 AM |
| help with queues | Unregistered | C Programming | 3 | 05-21-2002 09:09 PM |
| help with queues | Unregistered | C Programming | 3 | 05-21-2002 11:39 AM |