我正在使用 c 中的 struct 創建一個佇列來存盤客戶。我可以洗掉客戶節點,但我想顯示已洗掉客戶的資訊,即姓名和條形碼編號
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;
這是我的洗掉功能
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;
}
這是我對代碼的實作
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);}
}
uj5u.com熱心網友回復:
只需分解 removeCustomer 方法中的操作
從佇列中獲取“第一個/前面”客戶
Customer* first = qPtr->head;將其資料復制到您作為引數傳遞的客戶資訊結構
// using first for clarity but qPtr->head->name also works
strcpy(c->name, first->name);
c->barcodeNumber = first->barcodeNumber;更新佇列 - 洗掉前端
qPtr->head = qPtr->head->next;洗掉第一個客戶(現已提取)
free(first);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338884.html
上一篇:預期為“int**”,但引數的型別為“int(*)[3]”
下一篇:將字串陣列傳遞給另一個函式
