I'm creating a queue using struct in c to store customers. I'm able to delete a customer node but i would like to display the deleted customer's information i.e name and barcode number
typedef struct customer {
char name[50]; // the customer's name
double barcodeNumber; // the barcode number from the customer's ticket
struct customer *next; // a pointer to the next customer in the queue after this one
}
Customer;
typedef struct eventQueue {
Customer *head; // points to the customer at the front/head of the queue
Customer *tail; // points to the customer at the end/tail of the queue
} EventQueue;
here's my delete function
int removeCustomer(EventQueue *qPtr, Customer *c)
{
if(qPtr == NULL)
return INVALID_INPUT_PARAMETER;
if(c == NULL)
return INVALID_INPUT_PARAMETER;
if(qPtr->head == NULL)
return INVALID_QUEUE_OPERATION;
char *name;
double barcode;
strcpy(c->name,name);
barcodeCopy = c->barcode;
c=qPtr->head;
qPtr->head=qPtr->head->next;
free(c);
// Value to be returned if a function is completed successfully
return SUCCESS;
}
and here's my implementation of the code
void main()
{
printf("\nAttempting to remove customer from queue..");
// (attempt to) remove / pop a customer from the queue
Customer customerInfo;
// a variable to receive the customer data removed from the queue
result = removeCustomer(pQueue, &customerInfo); // this calls your implementation of removeCustomer()
// if customer wasn't removed successfully
if (result != SUCCESS)
{
printf("ERROR: Unable to remove customer from queue.\n");
}
else
{
printf("..customer removed successfully!\n");
printf("The customer removed was %s who was barcode number %.0lf.\n", customerInfo.name, customerInfo.barcodeNumber);}
}
CodePudding user response:
Just decompose the actions in the removeCustomer method
Getting the "first/front" customer from the queue
Customer* first = qPtr->head;
Copy its data to the Customer info struct you passed as parameter
// using first for clarity but qPtr->head->name also works
strcpy(c->name, first->name);
c->barcodeNumber = first->barcodeNumber;
Update the queue - remove front
qPtr->head = qPtr->head->next;
Delete the first customer (now extracted)
free(first);