Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok so I'm trying to implement a queue in C but the whole use of pointers has f****d up my mind, seriously I cant seem to wrap my head around them, I just want to make sure I'm on the right track(which is suspect is not the case :( )

So if anyone could just make a quick check and point me in the right direction I'd be grateful.

here's my code so far:

C#
#include "queue.h"
#include "string.h"

#define QUEUE_MAX_SIZE 10

static Person queue[QUEUE_MAX_SIZE];
static int head = 0, tail = 0, nbr_elem = 0;

typedef struct{
char firstName[20];
char surName[20];
char persNbr[20];
}Person;


void enqueue(Person *person)
{
    strcpy(char *queue[tail], *person.firstName);
    strcpy(char *queue[tail], *person.surName);
    strcpy(char *queue[tail], *person.persNbr);
}

void dequeue(Person *person)
{
    int i;

        for(i=0;i<queue.size;i++){
            if(queue[i]=head)
                 strcpy(person, queue[head]);
                 queue[head]=null;
        }

}
Posted

It's not the concept of pointers that is apparently bothering you, but the concept of structures. So don't worry, pointers are relatively easy to understand.

Let's start with your enqueue function:
C++
void enqueue(Person *person)
{
    strcpy(char *queue[tail], *person.firstName);
    strcpy(char *queue[tail], *person.surName);
    strcpy(char *queue[tail], *person.persNbr);
}

As person is a pointer to a Person object, all the members of that object are being addressed by person->membername. Your queue array contains elements of type Person. And hence queue[n] is a Person object. Hence, you refer to its members by queue[n].membername. The first line of your function should therefore be:
C++
strcpy (queue[tail].firstname, person->firstName);

Instead of referring to queue[tail] several times, you could equally well define a pointer to that Person object by saying
C++
void enqueue(Person *person)
{
    Person* p = &queue[tail];
    strcpy (p->firstname, person->firstName);
    ...

Doesn't that look better? Now one step further. Why do you want to copy the members of your Person structure one-by-one. Let the C compiler do that for you. As your members are all plain data (and no pointers to other objects) you could do the whole copying in a single line:
C++
void enqueue (Person *person)
{
    queue[tail] = *person;

And finally, you forgot one important thing: You should have incremented the tail index. So the next time you are entering a person it goes into the next cell of your array. And I guess you have envisioned the nbr_elem variable to hold the number of current queue elements. So you should increment that as well.

Your dequeue function is totally off track, as you might have noticed. The idea should be to do the copying in reverse, just this time take the queue cell with index head as the source. I am not going to provide the source code for that as it is a good exercise for you.

Also, you should do some checking for queue over- and underflow in both functions enqueue and dequeue.
 
Share this answer
 
Comments
[no name] 12-Nov-13 8:08am    
Thank you for the very detailed answer, very helpful! about the dequeue function, so basically it's the same as the enqueue function only this time I have to use the head variable?
nv3 12-Nov-13 8:16am    
Exactly. If you like, axtend your question with the new version of the dequeue function with he green Improve Question button and I will take a look at it. I just want you to do it and realize how it cooperates with your enqueue function.

And there is just one more detail you should take a look at. The head and tail indexes might wrap around. Say, if entries 7 and 8 are filled. Head is pointing to 7, tail to 9. If you now add one more person, tail will increase to 10, which is no valid index. In that case you can wrap the index around to 0. You can keep adding persons until mbr_elem reaches 10. So you have to be carefull when incrementing the head and tail indexes.
Abhinav Gauniyal 12-Nov-13 12:54pm    
Infact , it helped me to clear my doubts too :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900