Thread: Accessing next entry in a queue.

  1. #46
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in any case
    Code:
    tmp->isSpecific && tmp != NULL
    these conditions should be checked in the opposite order.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #47
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I still don't quite understand why the following code would not initialize my Driver struct. Any suggestions?
    Code:
    int createDriverAndAddByPriority(TaxiSystem* system, char* name, int priority){
     Driver *new_node = NULL, *cur = NULL, *prv = NULL;
     new_node = (Driver*) malloc(sizeof(Driver));
     if (new_node == NULL){
      printf("Fatal error: memory allocation failed!\n");
      return 0;
     }
     strcpy(new_node->name, name);
     new_node->priority = priority;
     cur = system->driversQueue;
     if (!cur)
      new_node->next = cur;
     else{
      while (cur && (cur->priority < new_node->priority)){
       prv = cur;
       cur = cur->next; 
      }
      new_node->next = cur;
      prv->next = new_node;
     }
     return 1;
    }

  3. #48
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    in the case the driverQueue is empty you should set system->driversQueue to new_node.
    But then you never answered the question how you initialize system.
    Kurt

  4. #49
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by peripatein View Post
    I still don't quite understand why the following code would not initialize my Driver struct. Any suggestions?
    Code:
    int createDriverAndAddByPriority(TaxiSystem* system, char* name, int priority){
     Driver *new_node = NULL, *cur = NULL, *prv = NULL;
     new_node = (Driver*) malloc(sizeof(Driver));
     if (new_node == NULL){
      printf("Fatal error: memory allocation failed!\n");
      return 0;
     }
     strcpy(new_node->name, name);
     new_node->priority = priority;
     cur = system->driversQueue;
     if (!cur)
      new_node->next = cur;
     else{
      while (cur && (cur->priority < new_node->priority)){
       prv = cur;
       cur = cur->next; 
      }
      new_node->next = cur;
      prv->next = new_node;
     }
     return 1;
    }
    You're definitely on the right track, but you're missing a few things.

    1. If driversQueue is empty (i.e. line 11 above), you need to set it to point to new_node, otherwise you can never get the first element into the list. I would not bother with cur at this point.
    2. Your else case does not handle inserting at the beginning of the list properly. If you are inserting at the beginning, you must update driversQueue accordingly


    Code:
    if (!system->driversQueue) {  // queue is empty
        update driversQueue and make sure the list is terminated
    }
    else {
        while (cur && (cur->priority < new_node->priority) {
            update cur and prv
        }
        if prv is NULL
            insert in beginning of list
        else  // inserting in middle or at end of list
            same code as lines 18 and 19
    }
    thats the rough structure, I'll let you have a go at filling in the details
    Last edited by anduril462; 06-14-2013 at 03:36 PM. Reason: fix missing list closing tag

  5. #50
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Were you inquiring about the initialization of system itself, and within this function? Or were you referring rather to the very initialization of the TaxiSystem struct?

  6. #51
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Quote Originally Posted by anduril462 View Post
    You're definitely on the right track, but you're missing a few things.

    1. If driversQueue is empty (i.e. line 11 above), you need to set it to point to new_node, otherwise you can never get the first element into the list. I would not bother with cur at this point.
    2. Your else case does not handle inserting at the beginning of the list properly. If you are inserting at the beginning, you must update driversQueue accordingly


    Code:
    if (!system->driversQueue) {  // queue is empty
        update driversQueue and make sure the list is terminated
    }
    else {
        while (cur && (cur->priority < new_node->priority) {
            update cur and prv
        }
        if prv is NULL
            insert in beginning of list
        else  // inserting in middle or at end of list
            same code as lines 18 and 19
    }
    thats the rough structure, I'll let you have a go at filling in the details
    This is what I wrote. It overrides the previous driver's name on the list:
    Code:
     cur = ptr->driversQueue;
     if (!ptr->driversQueue){
      ptr->driversQueue = new_node;
      new_node->next = NULL;
     }
     else{
         while (cur && (cur->priority < new_node->priority)){
             prv = cur;
       cur = cur->next; 
      }
         if (!prv){
             ptr->driversQueue = new_node;
       new_node->next = prv;
      }
         else{
             prv->next = new_node;
       new_node->next = cur;
      }
     }
     return 1;
    }
    Last edited by peripatein; 06-14-2013 at 03:45 PM.

  7. #52
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Code:
    if (!prv){
        ptr->driversQueue = new_node;
        new_node->next = prv;
    }
    This is not quite right. That section is supposed to add an element to the beginning of an existing list, but you throw away the rest of the list here. Remember, if we're in this section of code, prv is NULL. So what you did is
    1. Set driversQueue to the new_node
    2. Set new_node's next element to prv, which is NULL, and effectively terminates the list

    What you should do in that block is:
    1. Set the next element of new_node to the beginning of the existing list
    2. Update driversQueue to point to the new head of the list (new_node)

    EDIT: I find it's often very helpful to draw diagrams of your linked lists to ensure that you know what updates to what nodes, pointers, etc you are doing. From there, write out the steps in plain English. If you can't explain it in English, you have little hope of coding it correctly. Once you have the steps in English down, translating them to code is easy.

  8. #53
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Quote Originally Posted by anduril462 View Post
    Code:
    if (!prv){
        ptr->driversQueue = new_node;
        new_node->next = prv;
    }
    This is not quite right. That section is supposed to add an element to the beginning of an existing list, but you throw away the rest of the list here. Remember, if we're in this section of code, prv is NULL. So what you did is
    1. Set driversQueue to the new_node
    2. Set new_node's next element to prv, which is NULL, and effectively terminates the list

    What you should do in that block is:
    1. Set the next element of new_node to the beginning of the existing list
    2. Update driversQueue to point to the new head of the list (new_node)

    EDIT: I find it's often very helpful to draw diagrams of your linked lists to ensure that you know what updates to what nodes, pointers, etc you are doing. From there, write out the steps in plain English. If you can't explain it in English, you have little hope of coding it correctly. Once you have the steps in English down, translating them to code is easy.
    Alright, I have edited it accordingly. Now, whereas the drivers' queue seems to be updated correctly, the orders queue seems to not be updated as needed.
    Code:
     cur = ptr->driversQueue;
     if (!ptr->driversQueue){
      ptr->driversQueue = new_node;
      new_node->next = NULL;
     }
     else{
         while (cur && (cur->priority >= new_node->priority)){
             prv = cur;
       cur = cur->next; 
      }
         if (!prv){
             ptr->driversQueue = new_node;
       new_node->next = cur;
      }
         else{
             prv->next = new_node;
         new_node->next = cur;
      }
     }
     return 1;
    }

  9. #54
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Here's the code for the orders' queue:
    Code:
    int createOrderAndAddLast(TaxiSystem* system, int isSpecific, char* name){
     Order *new_node = NULL, *cur = NULL;
     new_node = (Order*) malloc(sizeof(Order));
     if (new_node == NULL){
      printf("Fatal error: memory allocation failed!\n");
      return 0;
     }
     new_node->isSpecific = isSpecific;
     if (name) strcpy(new_node->specificDriver, name);
     cur = system->ordersQueue;
     if (!cur){
      system->ordersQueue = new_node;
      new_node->next = NULL;
     }
     while (cur != NULL)
      cur = cur->next; 
     cur = new_node;
     new_node->next = NULL;
     /*new_node->next= NULL;*/
     return 1;
    }

  10. #55
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    This is how the output is expected to look like:
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michel 6
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    please enter name and priority
    David 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Jeff 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    John 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel Jeff John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Jeff
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Michael
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    0
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michael 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    4
    Press any key to continue . . .

  11. #56
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I don't really understand two things: 1) The queues are expected to be displayed based on the following format: Drivers: [Driver 1 Driver 2 Driver 3] Orders: [orderID1 orderID2 ]. And it is stated that the orderID, which is an integer in the TaxiSystem main struct, is assigned by the system. What I do not understand is how that ID is actually assigned and why are there only two IDs for three drivers? 2) Based on the format of the output in the previous post, why is the drivers' queue not updated near the end of that output, right after Michael 2? Why does the output still read [John David ]?

  12. #57
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by peripatein View Post
    Here's the code for the orders' queue:
    I came into this thread late, so I'm not so clear on what the orders queue is supposed to do. Should the elements have any particular ordering? Are the elements always inserted at the end?

    Once again, I would recommend figuring out abstractly how you want this queue to work. Then go about writing out the basic steps and logic in English. Translate that to pseudo code, then into real code. Those steps you wrote down in English would make nice comments for your pseudo code/real code.

  13. #58

  14. #59
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    The program now runs nearly perfectly. I thank you all wholeheartedly! There is one more thing I am concerned about, namely the instruction to verify that the input of the drivers' names does not exceed a 100 chars, and if it does then the chars should be passed to the next string. How do I go about that? Does it suffice to merely write something like %s100? I am really not sure. Any suggestions, please?

  15. #60
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would suggest this method FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    But you will need to take care of the rest of the string when it exceeds buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. DNS entry changes
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 09-02-2003, 08:57 PM
  3. Min, Max field entry?
    By JCCC in forum C Programming
    Replies: 1
    Last Post: 04-16-2002, 07:46 PM
  4. Registry entry from AIM help!
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 01-02-2002, 05:00 PM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM