I just learned about circular queues in class and I'm still confused, I know that without the empty cell we wouldn't be able to distinguish between an empty queue and a a queue with one element, but why ?
I used f.h for prototypes and f.c as implementation:
f.h:
#define n 50
struct queue
{
int key[n];
unsigned head;
unsigned tail;
};
void cree(struct queue *);
unsigned empty(struct queue);
int first(struct queue);
void enqueue(int, struct queue *);
void dequeue(struct queue *);
then f.c:
#include <assert.h>
#include "f.h"
void cree(struct queue *q)
{
q->head = 0;
q->tail = 0;
}
unsigned empty(struct queue q)
{
return (q.head == q.tail);
}
int first(struct queue q)
{
unsigned i;
assert(!empty(q));
i = q.head + 1;
if(i>n-1)
{
i = 0;
}
return(q.key[i]);
}
void enqueue(int x, struct queue *q)
{
q->tail++;
if(q->tail>n-1)
{
q->tail = 0;
}
assert(q->head != q->head);
q->key[q->tail] = x;
}
void dequeue(struct queue *q)
{
assert(!empty(*q));
q->head++;
if(q->head>n-1)
{
q->head =0 ;
}
}
You've got this a little bit wrong in 2 ways. The first way is that the confusion is between an empty queue and a full queue, not a queue with 1 element. Keeping one cell empty changes what it means to be "full".
So, given a circular queue, how do you determine how many elements it has in it?
You would like to write size = (end_position - start_position) % array_length. In fact, the % operator probably doesn't work like you want in your language, though, so you'll write size = (array_length + end_position - start_position) % array_length
If the queue is empty, you get size == 0, which is what you want. If the queue has array_length elements in it, though, you also get size == 0, which is wrong. You can fix that by ensuring that the number of elements is always less than the array length.
The other way you have this wrong is the "wouldn't be able to" part. It's almost always wrong to say that. If you store start_position and size, for example, instead of start_position and end_position, then it's easy to distinguish full from empty, and you can put array_length elements in your queue.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to enqueue a whole struct customer in the queue - I think. This is my homework. I am not asking for an answer. I need a mentor to explain what I am missing and what I have right per the assignment. I am very new. This is my second program. see below... thanks btw.
A store is having customers queue in several lines. After the cashier finishes helping a customer, he will survey all of the lines that are currently queued. Of all of the customers at the front of those lines, he’ll take the customer who has the fewest number of items. If there are two customers with the same number of items, he’ll take the customer who comes from the smaller line number. The lines are numbered 1 through 12. It’s possible that some of these lines will be empty, in which case these lines are ignored. The number of seconds the store clerk takes to check out a customer is 30 plus 5 times the number of items. Thus, if a customer has 8 times, the clerk would check her out in 30 + 8*5 = 70 seconds.
The Problem
You will write a program that reads in information about customers: which line they go to the back of (1 through 12), at what time (in seconds) they enter that line, and the number of items they have, and determines at what time each customer will check out.
Sample Input:
2
5
10 1 STEVEN 12
12 6 AHMAD 8
13 1 JENNY 40
22 6 JERMAINE 39
100000 12 AMALIA 53
6
100 1 A 100
200 2 B 99
300 3 C 98
400 4 D 97
500 5 E 96
600 6 F 95
Sample Output:
STEVEN from line 1 checks out at time 100.
AHMAD from line 6 checks out at time 170.
JERMAINE from line 6 checks out at time 395.
JENNY from line 1 checks out at time 625.
AMALIA from line 12 checks out at time 100295.
A from line 1 checks out at time 630.
F from line 6 checks out at time 1135.
E from line 5 checks out at time 1645.
D from line 4 checks out at time 2160.
C from line 3 checks out at time 2680.
B from line 2 checks out at time 3205.
Implementation Restrictions:
You must create a struct that stores information about a customer (name, number of items, line number, time entering line). Note that the storage of the line number is redundant, but is designed to ease implementation.
You must create a node struct for a linked list of customers. This struct should have a pointer to a customer struct, and a pointer to a node struct.
You must create a struct to store a queue of customers. This struct should have two pointers – one to the front of the queue and one to the back.
You must implement all of the lines that form as an array of size 12 (stored as a constant) of queues.
You must dynamically allocate memory as appropriate for linked lists.
Your queue must support the following operations:
a. Enqueue
b. Dequeue
c. Return the front of the queue WITHOUT dequeing d. Empty (returns 1 if the queue is empty, 0 if it is not)
You must free memory appropriately. Namely, when you dequeue, you’ll free memory for a node, but you will NOT free memory for the customer. You will free this memory a bit later right after you calculate when that customer will finish checking out.
Due to the nature of the problem, when you process the input, you can add everyone into their appropriate lines right at the beginning, before checking anyone out.
This wouldn’t work in all simulations (some of which you have to do in time order), but because there is ONLY one check out line, you can get away with it. The only thing you have to be cognizant about is that when you select a line, if the current time is, 100 for example, and three lines have customers who arrived before time 100 and the other lines have customers in the front who arrived AFTER time 100, you have to ignore the customers in those lines who arrived after time 100. In the case that all the lines have customers who arrived after time 100, you would take the line which has a customer who arrived first. You are guaranteed no ties for arrival time so this would be unique.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
typedef char customerName[9];
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct queue
{
int size;
int front;
int back;
int *array;
unsigned capacity;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
int isEmpty(queue *q);
void initialize(queue *q);
void initialize(queue *q)
{
q->size = 0;
q->front = -1;
q->back = -1;
}
int isEmpty(queue *q){
return (q->size == 0);//returns 1 if empty or 0 if false
}
int isFull(queue *q)
{ return (q->size == q->capacity); }
void enqueue(queue *q, int item)
{
if (isFull(q))
return;
q->back = (q->back + 1)%q->capacity;
q->array[q->back] = item;
q->size = q->size + 1;
}
int dequeue(queue *q)
{
if (isEmpty(q))
return 0;
int item = q->array[q->front];
q->front = (q->front + 1)%q->capacity;
q->size = q->size - 1;
return item;
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->array[q->front];
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[1];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
Here is my latest code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
#define NUMLINES 12
int currtime = 0;
typedef char customerName[9];
typedef struct node
{
int data;
struct customer* data;
struct node* next;
}node;
typedef struct queue
{
node* front;
node* back;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
typedef struct node *newNode;//define a node pointer
node createNode()
{
newNode temp;//declare node
temp = (newNode)malloc(sizeof(struct node));//allocate memory
temp->next = NULL;//next point to null
return *temp;// return the new node
}
struct queue* createQueue()
{
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->front = q->back = NULL;
return q;
}
int isEmpty(queue *q){
return (q->back == NULL);//returns 1 if empty or 0 if false
}
void enqueue(queue *q, customer* data)
{
// Create a new LL node
struct node* temp = createNode(data);
// If queue is empty, then new node is front and back both
if (q->back == NULL) {
q->front = q->back = temp;
return;
}
// Add the new node at the end of queue and change back
q->back->next = temp;
q->back = temp;
}
void dequeue(queue *q)
{
if (q->front == NULL)
return;
// Store previous front and move front one node ahead
struct node* temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->back = NULL;
free(temp);
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->front;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[11];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
qinit(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
queue* customerArray = (queue*) malloc(sizeof(queue) * 12);
for ( int i = 0; i < numCustomers; i++){
customer* newCustomer = (customer*) malloc(sizeof(customer));
scanf("%d", &(newCustomer->time));
scanf("%d", &(newCustomer->lineNumber));
scanf("%s", newCustomer->name);
scanf("%d", &(newCustomer->numberItems));
enqueue(&customerArray[newCustomer->lineNumber - 1], newCustomer);
}
int totalTime = INT_MAX;
for(int i=0;i<12;i++)
{
customer* frontCustomer = qfront(&customerArray[i]);
if(totalTime < frontCustomer->time)
{
totalTime = frontCustomer->time;
}
free(frontCustomer);
}
while(numCustomers--) {
int customerToCheckOutLine = 0; int minNumberOfItems = INT_MAX;
for( int j=11 ; j>=0; j--){
customer* frontCustomer = qfront(&customerArray[j]);
if(frontCustomer->time <= totalTime)
{
if(frontCustomer->numberItems < minNumberOfItems)
{
customerToCheckOutLine = frontCustomer->lineNumber;
minNumberOfItems = frontCustomer->numberItems;
}
free(frontCustomer);
}
}
customer* customerToCheckOut = qfront(&customerArray[customerToCheckOutLine -1 ]);
totalTime += 30;
totalTime += (customerToCheckOut->numberItems) * 5;
dequeue(&customerArray[customerToCheckOutLine - 1]);
}
free(customerArray);
}
}
return 0;
}
Your queue related functions look okay.
But, there are some bugs and the code needs refactoring to handle more things.
Your queue element array should be of type node * instead of int *.
You need the newCustomer array to be of length numCustomers (i.e. not 1).
At present, you have a single queue. But, you need an array of queues, one for each line, so an array length of 12.
When you queue a customer, you must select the queue [from the array of queues] based on the line number the customer is getting on.
Here's a biggie:
You can't queue two separate entries, one being time and the other being lineNumber. Well, you can I guess, but it's messy.
The way I'd do it is to change the data element of the node struct to be a pointer to the customer record. That way, every bit of data you need to determine the servicing order is [already] in the customer struct.
Anyway, here's some refactored code.
Per your request, it's not a solution, but some sample code as a basis for implementing the above fixes/suggestions.
You still have to change the queue struct and all queue related functions (if you decide to change the struct as I've suggested).
And, you still have to implement the selection/ordering process. And, the cleanup/deallocation.
This won't compile as is, because it's a "suggestion", but here it is:
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
typedef struct node {
#if 0
int data;
#else
customer *data;
#endif
struct node *next;
} node;
typedef struct queue {
int size;
int front;
int back;
#if 0
int *array;
#else
node *array;
#endif
unsigned capacity;
} queue;
#if 1
#define NUMLINES 12
queue queue_list[NUMLINES];
#endif
int isEmpty(queue *q);
void initialize(queue * q);
void
initialize(queue *q)
{
q->size = 0;
q->front = -1;
q->back = -1;
}
int
isEmpty(queue *q)
{
return (q->size == 0); // returns 1 if empty or 0 if false
}
int
isFull(queue *q)
{
return (q->size == q->capacity);
}
void
enqueue(queue *q, int item)
{
if (isFull(q))
return;
q->back = (q->back + 1) % q->capacity;
q->array[q->back] = item;
q->size = q->size + 1;
}
int
dequeue(queue *q)
{
if (isEmpty(q))
return 0;
int item = q->array[q->front];
q->front = (q->front + 1) % q->capacity;
q->size = q->size - 1;
return item;
}
int
front(queue *q)
{
if (isEmpty(q)) {
return 0;
}
return q->array[q->front];
}
int
main(int argc, const char *argv[])
{
queue *q;
int testCases = 0;
scanf(" %d", &testCases);
// NOTE/BUG: you need a separate queue for each line
for (int qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
initialize(q);
}
// shortcircuiting???
if (testCases > 0 && testCases <= 25) {
while (testCases--) {
#if 0
queue *q;
#endif
#if 0
q = malloc(sizeof(queue));
initialize(q); // starting new queue
#endif
int numCustomers;
scanf(" %d", &numCustomers);
if (numCustomers < 0 || numCustomers > 11) {
return 0;
}
// NOTE/BUG: you need a larger array of customers
#if 0
struct customer newCustomer[1];
#else
struct customer newCustomer[numCustomers];
#endif
for (int i = 0; i < numCustomers; i++) {
customer *cust = &newCustomer[i];
scanf(" %d", &cust->time);
scanf(" %d", &cust->lineNumber);
scanf(" %s", cust->name);
scanf(" %d", &cust->numberItems);
// NOTE/BUG: customer must be queued to the queue for the
// line number they're on
// NOTE/BUG: queue a single entry for each customer
#if 0
enqueue(q, cust->time);
enqueue(q, cust->lineNumber);
#else
q = &queue_list[cust->lineNumber - 1];
enqueue(q,cust);
#endif
}
for (int i = 0; i < numCustomers; i++) {
printf("%d %d %s %d\n",
newCustomer[i].time,
newCustomer[i].lineNumber,
newCustomer[i].name,
newCustomer[i].numberItems);
}
}
}
return 0;
}
UPDATE:
woah, this is amaze balls. thank you so much. I think the assignment is confusing because he says the assignment is queues with linked list not an array but like you said it needs to be an array of queues. that would suggest multiple entries into a single line. so in a sense this is both an queues with linked lists and array?
You need an array of queues, indexed by linenumber. This is not to be confused with the q->array element within the queue, which is different.
I just completed a working and tested version [for myself, because I'm crazy that way :-)]. I had the same confusion.
The queue is a ring queue [with array]. If the array pointer is a customer *, I couldn't find a good way to use node. The only way to do it would be to convert queue into a linked list header struct. That would require queue to be restructured [along will all the access functions for it ;-)].
But, before doing that, if we keep the array/ring method, then initialize needs to take a capacity arg (called with initialize(q,numCustomer);). And, it needs to do (e.g.) q->array = realloc(q->array,sizeof(customer *) * capacity);
And, enqueue needs a fix: the increment of q->back needs to be moved after the q->array[q->back] = item;. Currently, there is an off by one gap.
To convert to a linked list:
The structs change a bit:
typedef struct node {
customer *data;
struct node *next;
} node;
typedef struct queue {
node *front;
} queue;
You have an isFull function. That's not part of the requirements. If we switch queue into a linked list header for a list of node, it isn't even relevant/meaningful since, now, there is no maximum [as there was with q->capacity].
Now, when doing enqueue(q,cust);, the function would malloc a new node and append it to the end of the linked list. And, setup of (e.g.) newnode->next and newnode->data = cust;
When doing dequeue, it pops off the front element (a node), does cust = curnode->data;, then free(curnode), and returns cust.
Based on my rereading the requirements, I believe that this is what's required, and actually makes sense of all of them.
In other words, you can implement a queue with just a linked list (vs. what you did which was a ring queue with array--which I've also done in the past).
the ordering process is part of the reason I am so confused. I don't know where to do this. The list is already ordered by time the customer went to check out. I just need to calculate the total cashier time.there is only one cashier so all the people are served in order of entering the queue.
This is a bit tricky. You have to have a curtime global to keep track of the current time. Initially, this has to be set to the minimum of all cust->time values.
As you loop through all queues [i.e. waiting lines] to look at the first customer on the line, you have to do the following:
You skip the queue if it's empty.
You skip any customer if they have an arrival date in the future (i.e. cust->time > curtime).
Now you can "select" the customer. You need the find "best" customer [for the next one to be serviced]. That is, the one with the smallest order. The 2nd criteria of: with a tie, the customer with the lowest line number wins is handled automatically if you only update the "best" value if (cust->numberItems < best->numberItems) because we're traversing the queue_list array from low to high.
Then, service the "best" customer (i.e. print the message). Then, increment curtime by the elapsed time it takes to service the customer.
Repeat this until all queues are empty.
But, there is one gotcha. It happens with AMALIA.
The arrival time for this customer is so far in the future that the customer selection will fail to find any "best" customer because amalia->time is greater than curtime. Thus, it seems like AMALIA is not ready to be serviced.
So, if the selection fails (i.e. best is null), we have to loop through all front customers [again] and set curtime to the minimum of all their arrival times.
Then, we rescan/reselect for "best". Now, AMALIA will be selected
UPDATE #2:
in your example I don't understand what the #elseif etc. what are those? other than the obvious, I have never seen this
The lines starting with #if are preprocessor statements [just like #include, #ifdef, #else, #endif
The preprocessor is used to include/exclude code at compile time (vs using a real if language statement). You can think of the preprocessor as a separate pass before compilation that performs the conditional actions before passing the resultant file to the compile stage.
In my examples, I'm using them to show old code [yours] vs. new code [mine]:
#if 0
// old code ...
x = 1;
#else
// new code ...
x = 2;
#endif
When compiled, the old code is excluded, the new code is included as if we had done:
// new code ...
x = 2;
This is a useful technique in general. It allows you to "comment out" code that you suspect might be a bug. Or, for debug printf code. Or, you have working code (the "old" code), but you want to try something possibly cleaner or more efficient (the "new" code). You can flip back and forth during development just by changing #if 0 to #if 1 or vice versa
You can see the intermediate file by doing:
cc -E -P -o myfile.i myfile.c
Later on, when your code is [fully ;-)] debugged, you could hand edit out the #if cases you don't need/want.
can you do that same kind of example but for the linked list only implementation? or is that asking too much? :) why is there only a *front and no back of the list?
For a simple singly linked list, we only truly need a head/front pointer.
We don't need size because the empty case is: q->front == NULL. size is primarily useful if we had to sort the linked list (it saves a pass on the list to get the number of elements).
Using q->back adds a slight degree of complexity. It's a speedup when appending to the queue/list. If we don't have it, we find the last element in the list by traversing it. If we have back, that is the last element.
A singly linked list is sufficient for your use case. But, just for completeness, I've added doubly linked list support. We add a prev element to node [the backwards link].
Below, I've coded up the linked list structs and functions. To allow you to see the various modes, each optional element is wrapped in preprocessor #if/#endif pairs to include/exclude code, based on setting the given option on/off
DLINK [doubly linked list] is off by default
To enable it on a one time basis, you could do:
cc -DDLINK=1 -c myfile.c
Likewise for other options.
Anyway, here's the queue code based on a linked list:
// customer record
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;
// 1=use queue q->back
// need this for _doubly_ linked list
// speedup for append to end of queue for singly linked
#ifndef QBACK
#define QBACK 1
#endif
// 1=use queue q->size
#ifndef QSIZE
#define QSIZE 1
#endif
// 1=use doubly linked list
#ifndef DLINK
#define DLINK 0
#endif
// force QBACK on if doing doubly linked list
#if DLINK
#undef QBACK
#define QBACK 1
#endif
// [singly linked] queue element
typedef customer *qitem;
typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
#if DLINK
struct node *prev; // backward pointer to previous item
#endif
} node;
// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list
#if QBACK
node *back; // pointer to last node in list
#endif
#if QSIZE
int size; // current number of elements in list
#endif
} queue;
// list of queues (indexed by lineNumber - 1)
#define NUMLINES 12
queue queue_list[NUMLINES];
// qinit -- initialize/reset queue
void
qinit(queue *q)
{
q->front = NULL;
#if QSIZE
q->size = 0;
#endif
#if QBACK
q->back = NULL;
#endif
}
// qempty -- returns 1 if empty or 0 if false
int
qempty(queue *q)
{
// NOTE: size really isn't needed
#if 0
return (q->size == 0);
#else
return (q->front == NULL);
#endif
}
// enqueue -- append element to end of queue
void
enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;
newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;
// NOTE: this is the only place where back is a speedup
#if QBACK
prev = q->back;
#else
// what we have to do find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;
#endif
// append to tail of list
if (prev != NULL)
prev->next = newnode;
// add to end of empty list
else
q->front = newnode;
#if DLINK
newnode->prev = prev;
#endif
#if QBACK
q->back = newnode;
#endif
#if QSIZE
q->size += 1;
#endif
}
// dequeue -- dequeue from the front of the queue
qitem
dequeue(queue *q)
{
node *curnode;
qitem data;
do {
curnode = q->front;
// bug out if list is empty
if (curnode == NULL) {
data = NULL;
break;
}
// get node's data value (e.g. pointer to customer struct)
data = curnode->data;
#if QBACK
node *back = q->back;
#if DLINK
curnode->prev = back;
#endif
if (curnode == back)
q->back = curnode->next;
#endif
q->front = curnode->next;
#if QSIZE
q->size -= 1;
#endif
// release the node's storage back to the heap
free(curnode);
} while (0);
return data;
}
// qfront -- peek at front of queue
qitem
qfront(queue *q)
{
node *curnode;
qitem data;
curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;
return data;
}
UPDATE #3:
so my main function has a memory leak and I cannot find it. I am trying to learn Valgrind and gdb but there is a learning curve. My Valgrind outran is like 90,000 characters for some reason and doesn't say hey I have a memory leak. I don't even know what line. I appended my new main to the bottom of the above code.
A few issues:
You malloc the q pointer at the top of the outer while (testCases--) loop but do not free it.
But, as I mentioned, you need an array of queues [which can be a fixed array] and you've already done that.
But, q was your original/legacy code. The rest of your code no longer uses q. You just forgot to remove it.
You are now using customerArray instead. But, you are not calling qinit on each array element, so you could have a random data in each.
In the problem definition, the number of customer lines is fixed at 12, so you really don't need to malloc and free it. That's why I used a global array instead.
You have a bug in your totalTime calculation loop. You're doing a free of the customer record. So, the record is no longer valid in the loop below it.
And, in that second loop, you're doing a second free of the same struct. So, at present, this is a "double free" bug. The second loop is actually the correct place for the free [not the first loop]
This program is ambitious for a second ever assignment, so you're "being thrown out into the middle of the lake and being asked to swim to shore".
I would have expected a few more intermediate steps/programs before trying to code up this one.
When starting out, as you are, sometimes it's as important to study working code as it is to write it from scratch.
So, spoiler alert, here's my final, fully working code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TIME_PAST -1 // infinite time in the past
#define TIME_FUTURE INT_MAX // infinite time in the future
int curtime; // current time
// customer record
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;
// 1=use queue q->back
// need this for _doubly_ linked list
// speedup for append to end of queue for singly linked
#ifndef QBACK
#define QBACK 1
#endif
// 1=use queue q->size
#ifndef QSIZE
#define QSIZE 1
#endif
// 1=use doubly linked list
#ifndef DLINK
#define DLINK 0
#endif
// force QBACK on if doing doubly linked list
#if DLINK
#undef QBACK
#define QBACK 1
#endif
// [singly linked] queue element
typedef customer *qitem;
typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
#if DLINK
struct node *prev; // backward pointer to previous item
#endif
} node;
// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list
#if QBACK
node *back; // pointer to last node in list
#endif
#if QSIZE
int size; // current number of elements in list
#endif
} queue;
// list of queues (indexed by lineNumber - 1)
#define NUMLINES 12
queue queue_list[NUMLINES];
// qinit -- initialize/reset queue
void
qinit(queue *q)
{
q->front = NULL;
#if QSIZE
q->size = 0;
#endif
#if QBACK
q->back = NULL;
#endif
}
// qempty -- returns 1 if empty or 0 if false
int
qempty(queue *q)
{
// NOTE: size really isn't needed
#if 0
return (q->size == 0);
#else
return (q->front == NULL);
#endif
}
// enqueue -- append element to end of queue
void
enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;
newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;
// NOTE: this is the only place where back is a speedup
#if QBACK
prev = q->back;
#else
// what we have to do find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;
#endif
if (prev != NULL)
prev->next = newnode;
else
q->front = newnode;
#if DLINK
newnode->prev = prev;
#endif
#if QBACK
q->back = newnode;
#endif
#if QSIZE
q->size += 1;
#endif
}
// dequeue -- dequeue from the front of the queue
qitem
dequeue(queue *q)
{
node *curnode;
node *nextnode;
qitem data;
do {
// get the first node
curnode = q->front;
// if none, return null data
if (curnode == NULL) {
data = NULL;
break;
}
// get the data payload
data = curnode->data;
// point to the next node (i.e. second node)
nextnode = curnode->next;
#if QBACK
node *back = q->back;
#if DLINK
curnode->prev = back;
#endif
if (curnode == back)
q->back = nextnode;
#endif
q->front = nextnode;
#if QSIZE
q->size -= 1;
#endif
free(curnode);
} while (0);
return data;
}
// qfront -- peek at front of queue
qitem
qfront(queue *q)
{
node *curnode;
qitem data;
curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;
return data;
}
// custfind -- find next customer to serve
customer *
custfind(void)
{
int pass;
int qidx;
queue *q;
int mintime;
int moreflg;
customer *cust;
customer *best;
// pass 1:
// - remember the minimum time
// - find best using curtime
for (pass = 1; pass <= 2; ++pass) {
best = NULL;
mintime = TIME_FUTURE;
moreflg = 0;
// scan all queues -- examine _first_ customer on each line
for (qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
// get first customer on current line
cust = qfront(q);
if (cust == NULL)
continue;
// remember that there is at least _one_ customer waiting
moreflg = 1;
// remember the minimum time
if (cust->time < mintime)
mintime = cust->time;
// has the customer actually arrived yet? -- skip if not
if (cust->time > curtime)
continue;
// no previous "best" customer -- set from current
if (best == NULL) {
best = cust;
continue;
}
// get better customer (has fewer items to check out)
if (cust->numberItems < best->numberItems) {
best = cust;
continue;
}
}
// no more customers
if (! moreflg)
break;
// found a best match
// dequeue and free node
if (best != NULL) {
q = &queue_list[best->lineNumber - 1];
dequeue(q);
break;
}
// no best match found
// all customers are in the future [based on curtime]
// set new current time based on minimum time of all remaining customers
// the second pass _will_ find at least one after we do this
curtime = mintime;
}
return best;
}
// custdo -- check out customer
void
custdo(customer *cust)
{
int elap;
// current time has to be _at least_ the arrival time
if (curtime < cust->time)
curtime = cust->time;
// get amount of time it takes to service this customer
elap = 0;
elap += 30;
elap += cust->numberItems * 5;
// set current time after servicing the customer
curtime += elap;
printf("%s from line %d checks out at time %d.\n",
cust->name,cust->lineNumber,curtime);
// release the customer record storage
free(cust);
}
// testcase -- process a test case
void
testcase(FILE *fi)
{
queue *q;
customer *cust;
// reset all queues
// NOTE: probably not required
for (int qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
qinit(q);
}
// get number of customers for this case
int numCustomers;
fscanf(fi," %d", &numCustomers);
// read in all customer records for this test case
for (int icust = 0; icust < numCustomers; ++icust) {
cust = malloc(sizeof(*cust));
fscanf(fi," %d", &cust->time);
fscanf(fi," %d", &cust->lineNumber);
fscanf(fi," %s", cust->name);
fscanf(fi," %d", &cust->numberItems);
// add customer to appropriate line/queue
q = &queue_list[cust->lineNumber - 1];
enqueue(q,cust);
}
// set current time to [way in the] past
curtime = TIME_PAST;
while (1) {
// find the next customer
cust = custfind();
// no more customers
if (cust == NULL)
break;
// service customer
custdo(cust);
}
}
int
main(int argc, char **argv)
{
char *ifile;
FILE *fi;
--argc;
++argv;
// open the input file
if (argc > 0)
ifile = *argv;
else
ifile = "input.txt";
fi = fopen(ifile,"r");
if (fi == NULL) {
perror(ifile);
exit(1);
}
// get number of test cases
int testCases;
fscanf(fi," %d", &testCases);
// process all test cases
for (; testCases > 0; --testCases)
testcase(fi);
fclose(fi);
return 0;
}
I was solving a problem which allowed two types of operations: subtracting one from a number or multiplying it by two, with the source and the destination numbers provided. Input constraints are 1<=n<=10^4 for both numbers. I'm supposed to output the number of operations needed to produce the desired number from the given one. The following is my implementation, getting runtime error and, obviously, I do not know why. It'll be awesome if someone explains the bug. Thanks.
#include <stdio.h>
#include <stdlib.h>
int g[22222][3], v[2222], size;//g == graph, v == visited and size == the size of queue
typedef struct _queue
{
int val;
struct _queue *next;
struct _queue *prev;
} queue;
queue *head=NULL, *last=NULL;
void push(int val)
{
queue *ptr=(queue *) malloc(sizeof(queue));
ptr->next=NULL;
ptr->val=val;
if (head)
{
last->next=ptr;
ptr->prev=last;
}
else
{
head=ptr;
ptr->prev=NULL;
}
last=ptr;
}
void pop()
{
if (size)
{
queue *ptr=last;
last=last->prev;
if (head) last->next=NULL;
free(ptr);
}
}
int front() {return last->val;}
int bfs(int s, int d)//s == source and d == destination
{
int cnt=0;
push(s);
size++;
v[s]=1;
while (size)
{
int u=front();
pop();
size--;
for (int j=1; j<=2; j++)
{
if (d==g[u][j]) return (cnt+1);
if (!v[g[u][j]])
{
v[g[u][j]]=1;
size++;
push(g[u][j]);
}
}
cnt++;
}
}
int main()
{
int n, m, val;
scanf("%d%d", &n, &m);
if (n==m) {printf("0"); return 0;}
val=(n>m?n:m)*2;
v[0]=1;
for (int i=1; i<=val; i++)
{
g[i][1]=2*i;
g[i][2]=i-1;
}
printf("%d", bfs(n, m));
return 0;
}
You have implemented a stack i.e. LIFO (last in first out): you are adding to the end and retrieving from the end.
You should implement a queue i.e. FIFO (first in first out), so if you add to end, you should retrieve from front:
void pop()
{
if (size)
{
queue *ptr=head;
head=head->next;
if (head) head->prev=NULL;
free(ptr);
}
}
int front()
{
return head->val;
}
Also, I guess your aim is to count the smallest number of operations required to produce the desired number from a given one. Your cnt variable does not represent the smallest number of operations, it represents the number of times you retrieved an element from the queue. You need to increment it for each new level instead.
Finally, your bfs should return a value even if there is no path from s to d, so you should put return 0; after the while(size){} loop.
UPD. You need to skip g[u][j] if it is larger than 2 * (10^4) inside of bfs, otherwise those values be enqueued which is a waste of space. By the way your v array has only 2222 elements, it should have at least 20001 (v[20000] is the last one)
I coded a simple source. It contains a queue and some of the function a queue needs but for some reason malloc() only works once.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE sizeof(Queue)
Definition of the Node, which is an element of the list, and the queue.
typedef struct node {
char * value;
struct node * next;
} Node;
typedef struct queue {
Node * head;
Node * tail;
} Queue;
int initialization(void ** list, int type){
int code = -1;
//create an empty list.
//if queue dynamically allocate memory and assign NULL to both properties head and tail.
return code;
}
enqueue() add one element in the queue at a time. but for some reason it can only add one element then the program crashes.
int enqueue(Queue * q, char * instruction){
int code = -1;
if(q != NULL){
printf("Prepare to enqueue!\n");
Node * n = NULL;
n = (Node*)malloc(sizeof(Node));
if(n != NULL){
printf("Node created!\n");
strcpy(n->value, instruction);
n->next = NULL;
//if first value
if(q->head == NULL){
q->head = n;
q->tail = n;
printf("Enqueue first Node\n");
}
else {
q->tail->next = n;
q->tail = n;
printf("Enqueue another Node\n");
}
code = 0;
printf("Node \"%s\" Enqueued\n", instruction);
}
}
return code;
}
int dequeue(Queue * q){
int code = -1;
//dequeuing code here.
return code;
}
int isEmpty(void * list, int type){
int code = 0;
//check if the list is empty
return code;
}
the for loop in the main() function never reaches 3
int main(int argc, char * argv[]){
Queue * queue = NULL;
initialization((void*)&queue, QUEUE);
int i = 0;
for(i = 0; i < 3; i++){
if(enqueue(queue, "some value") != 0){
printf("couldn't add more Node\n");
break;
}
}
while(!isEmpty(queue, QUEUE)){
dequeue(queue);
}
return 0;
}
The initialization function is written this way because it should also be able to initialize stacks (I removed the stack code to reduce the source but even without it the bug persist). I also put printfs to debug the code. And I have more than enough memory to make this simple code run how it should.
Thanks in Advance!
Running this, I crash with a segmentation fault, as I'd expect:
n = (Node*)malloc(sizeof(Node));
n is allocated, it's contents uninitialized and effectively random
if(n != NULL){
n is not NULL, so...
strcpy(n->value, instruction);
And we crash.
See the problem? n->value is a pointer to nowhere. Or, to somewhere, but nowhere known. Nowhere good. And we're just dumping a string into that space.
Either change the Node struct so that value is a char [SOME_SIZE], or use strdup() instead of strcpy(), to actually allocate some memory for the poor thing.
n->value = strdup(instruction);
I have the structs and I would like to write a main function to do some tests, but:
1 - How can I print this queue?
2 - How can I enqueue more dinamically? Like, with a "for"?
I'm not trying to have a answer for a homework here (before someone say that). I have a test, so I'm only trying to learn how to do it, and if someone can help me I'll be very grateful.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct Queue{
int size;
int first;
int last;
int items[MAX_SIZE];
} Queue;
Queue* createQueue() {
Queue *queue = (Queue*)malloc(sizeof(Queue));
queue->size = 0;
queue->first = 0;
queue->last = MAX_SIZE - 1;
return queue;
}
Enqueue(Queue *queue, int item) {
if(queue->size >= MAX_SIZE) {
printf("Queue is full!");
}
else {
queue->last = (queue->last + 1) % MAX_SIZE;
queue->items[queue->last] = item;
queue->size++;
}
}
Dequeue(Queue *queue) {
if(queue->size <= 0) {
printf("Queue is empty!");
}
else {
queue->first = (queue->first + 1) % MAX_SIZE;
queue->size--;
}
}
int main {
int i;
Queue *queue = createQueue();
Enqueue(queue, 1);
Enqueue(queue, 5);
Enqueue(queue, 8);
Enqueue(queue, 9);
for(i = 0; i <= MAX_SIZE; i++) { // Is this "for" right?
printf("%d ", queue-> ????) // Don't know what to put here to print right
}
return 0;
}
Since you are using an array it could be done as follows:
for ( int i = 0; i < queue->size; i++ ) {
printf( "item at position %d is %d\n", i, queue->items[ i ] );
}
Important note though, you actually have a few errors in your implementation of a queue. You should be increasing the size of the queue in your Enqueue function (which you are not). To fix that add the following somewhere in the else condition:
queue->size += 1;
... also you should be decreasing the size in your Dequeue function, done as follows:
queue->size -= 1;
... also, depending on how you implement your queue, when you create a queue it should be empty and hence front and back are NULL pointers (if using a linked list) and thus should be -1 for an array, to represent an empty queue. Your createQueue function should be as follows:
Queue* createQueue() {
Queue *queue = (Queue*)malloc(sizeof(Queue));
queue->size = 0;
queue->first = -1;
queue->last = -1;
return queue;
}
... making that change will force you to change your Enqueue and Dequeue functions to respectively handle an empty queue properly. Also, you should create a destroyQueue function to clean up all of the memory you allocated (in this case only once). In summary, lot's needs to be changed for this queue to work properly. Since this is a homework assignment, I will leave it up to you.
Alternative Implements:
I recommend you explore how to implement a queue using a dynamic array or linked list. Using a fixed size array is practically useless... (but a fair introduction). Explore "dynamic" memory allocation and the memory pool, or more commonly known as heap.
Great job though, I hope you are learning!
/*Affichage*/
void affiche(struct file* f)
{
int j;
int taille=(f->ar-f->av)+1;
for(j=f->av; j < taille; j++)
printf("%d \n", f->tab[f->av]);
}