in any case
these conditions should be checked in the opposite order.Code:tmp->isSpecific && tmp != NULL
in any case
these conditions should be checked in the opposite order.Code:tmp->isSpecific && tmp != NULL
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
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; }
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
You're definitely on the right track, but you're missing a few things.
- 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.
- 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
thats the rough structure, I'll let you have a go at filling in the detailsCode: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 }
Last edited by anduril462; 06-14-2013 at 03:36 PM. Reason: fix missing list closing tag
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?
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.
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 isCode:if (!prv){ ptr->driversQueue = new_node; new_node->next = prv; }
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; }
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; }
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 . . .
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 ]?
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.
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?
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