Thread: Queue trouble!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Queue trouble!

    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.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    No ideas?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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++;
    }
    What does p point at - shouldn't you malloc() a new elem each time?

    [I'm very surprised that this doesn't crash immediately, but I guess you may have some pointer value floating about on the stack that just happens to be valid].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Ahha, that did it. I could have sworn I had the code to alloc the memory in there before.. must have lost it in my many attempts to fix the code after the first run through. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM