Hey guys,
I am in the midsts of writing code that involves managing two queues of names, and having the ability to call one name from the queue, and then deleting it. For some reason, I can't get it to properly work. I was wondering if someone could look over these couple functions and see if you notice anything wrong with them.
Here's the two struct definitions:
Code:
#define MAX 50
#define EMPTY 0
struct elem {
char name[MAX];
struct elem *next;
};
struct queue {
int num;
struct elem *front;
struct elem *rear;
};
Function enqueue:
Code:
void enqueue(queue *qu, char pass[]) {
elem *p;
strcpy(p->name, pass);
p->next=NULL;
if (!empty(qu)) {
qu->rear->next = p;
qu->rear = p;
}
else qu->front = qu->rear = p;
printf("name = %s\n", qu->rear->name);
qu->num++;
}
Function dequeue:
Code:
char *dequeue(queue *qu) {
char *pass = malloc(sizeof(char)*50);
elem *p;
p = qu->front;
strcpy(pass, qu->front->name);
qu->front = qu->front->next;
qu->num--;
free(p);
return (pass);
}
I also have:
Code:
typedef struct queue queue;
typedef struct elem elem;
Now, what seems to happen is that when I call dequeue, the name it gives me is always the last name I add to either queue. I can't seem to get it to work.