#include<stdlib.h>
struct customer
{
int phoneNum;
char *name;
struct customer *nxt;
};
struct customer *initialNode(int i, char *n);
int isEmpty(struct customer *head, struct customer *tail);
void enqueue(int phNum1, char *cName1, struct customer *tail);
void dequeue(struct customer *head, struct customer *tail);
void display(struct customer *head, struct customer *tail);
int main()
{
int phNum;
char *cName;
int choice;
struct customer *head;
struct customer *tail;
head = initialNode(0, NULL);
tail = initialNode(0, NULL);
head->nxt = tail;
tail->nxt = head;
while (choice !=4)
{
printf("\nEnter your option:");
printf("\n1. Enqueue new customer. \n2. Dequeue customer. \n3. Display customer in queue. \n4. Exit. \nOption: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\nEnter customer phone number: ");
scanf("%d", &phNum);
printf("\nEnter customer name: ");
scanf("%s", cName);
enqueue(phNum, cName, tail);
break;
}
case 2:
{
dequeue(head, tail);
break;
}
case 3:
{
display(head, tail);
break;
}
default:
{
if (choice < 1 || choice > 4)
printf("\nInvalid option!\n");
break;
}
}
}
}
struct customer *initialNode(int i, char *n)
{
struct customer *newCustomer = malloc(sizeof(struct customer));
newCustomer->phoneNum = i;
newCustomer->name = n;
newCustomer->nxt = NULL;
return newCustomer;
}
int isEmpty ( struct customer *head, struct customer *tail)
{
if (head->nxt == tail)
return 1;
else
return 0; //cannot is full because it is not array, have no size limitation
}
void enqueue(int phNum1, char *cName1, struct customer *tail)
{
struct customer *nC = malloc(sizeof(struct customer));
nC = initialNode(phNum1, cName1);
nC->nxt = tail;
tail->nxt->nxt = nC;
tail->nxt = nC;
printf("\nSuccessfully enqueue a customer!\n");
}
void dequeue(struct customer *head, struct customer *tail)
{
struct customer *removedCustomer = malloc(sizeof(struct customer));
if (isEmpty(head, tail) == 1)
{
printf("\nThe queue is empty!\n");
}
else
{
removedCustomer = head->nxt;
printf("\nFirst customer is removed.\n");
head->nxt = head->nxt->nxt;
}
}
void display(struct customer *head, struct customer *tail)
{
struct customer *tempH = malloc(sizeof(struct customer));
tempH = head;
tempH = tempH->nxt;
if (isEmpty(head, tail) == 1)
{
printf("\nThe queue is empty!\n");
}
else
{
printf("\n===Current customer list===");
while (tempH != tail)
{
printf("\n\tCustomer phone number : %d", tempH->phoneNum);
printf("\n\tCustomer name: %s\n", tempH->name);
tempH = tempH->nxt;
}
}
}
Предполагается, что код составляет очередь из списка клиентов, в которых пользователь вводит свой номер телефона и имя, код работает хорошо, но когда я ввожу нового клиента, клиент перед именем все так или иначе становится именем последнего клиента.. Как мне решить это? Объявляю ли я массив строк 2D для сбора другого набора имени клиента?