Home > OS >  How to give automatic number when the user inputs name? Using Queue in C
How to give automatic number when the user inputs name? Using Queue in C

Time:12-06

Student here.

My program needs to give a customer number starting from 1 when the user inputs a name. For example, if The user inputs "Althea" The program should give the customer number 1 to althea, when another name is inputted the program will give the customer number 2. I also need it if the other function is used, for example, if The user also did the dequeue function, the program should print, Customer number 1 "Althea" is deleted, same in peek operation "Customer number 1 'Althea' is first in line" and same in the display function, The program should display the customer number and the customer name. I do not know where to begin to do this. How to do this?

CodePudding user response:

You need to have your customers' numbers stored somewhere.

struct customer {
    char name[NAME_MAX];
    int  number;
};

Your queue can be implemented as a singly linked list:

struct list {
    struct customer customer;
    struct list *next;
};

with:

  • enqueue() being a push_back() on your list.
  • dequeue() being a pop_front() on your list.

And your queue data structure:

struct queue {
    struct list *front;
    struct list *rear;
    size_t size;
    size_t capacity;
};

From that, you can write your enqueuing and dequeuing logic:

int enqueue(struct queue *q, const char *name)
{
    if (!q) return 0;
    if (q->size == q->capacity) return -1;
    
    int number = q->rear ? q->rear->customer.number 1 : q->size;
    
    struct list *item = list_new(name, number);
    if (!item) return -2;
    
    
    if (q->rear == NULL) {
        q->front = item;
        q->rear = item;
    } else {
        q->rear->next = item;
        q->rear = q->rear->next;
    }
    
      q->size;
    return 1;
}
int dequeue(struct queue *q, struct list **first)
{
    if (!q) return 0;
    if (q->size == 0) return -1;
    
    struct list *item = q->front;
    
    if (q->front == q->rear) {
        q->front = NULL;
        q->rear = NULL;
    } else {
        q->front = q->front->next;
    }
    
    item->next = NULL;
    first ? *first = item : free(item);
    return 1;
}

list_new() is to create a new list node:

struct list *list_new(const char name[NAME_MAX], int number)
{
    struct list *item = malloc(sizeof *item);
    if (!item) return NULL;
    
    snprintf(item->customer.name, NAME_MAX, "%s", name);
    item->customer.number = number;
    item->next = NULL;
    return item;
}

If you need the peek:

int queue_peek(struct queue *q, struct customer *cust)
{
    if (!q) return 0;
    *cust = q->front->customer;
    return 1;
}

And to initialize your queue:

void queue_init(struct queue *q, const size_t capacity)
{
    q->front = NULL;
    q->rear  = NULL;
    q->size  = 0;
    q->capacity  = capacity;
}

Your code now should be straightforward.


Nice to read: A beginners' guide away from scanf().

CodePudding user response:

In this case your container:

char name[max][80];

is no longer appropriate and you need to switch to something like:

static struct
{
    int id;
    char name[80];
} list[max];

and main must keep a counter to maintain the member id.

What I don't quite understand is why you pass the container to the functions if it is declared globally.

You also need to take care of buffer overflows:

scanf("%s",data);

should be:

scanf("ys",data);

or better yet:

if (fgets(data, sizeof data, stdin))
{
    data[strcspn(data, "\n")] = '\0'; // Remove the trailing newline
}
  • Related