I am learning C and I have some trouble with pointers. I decided to create a queue to practice a bit. The program works as intended, however I want to know some good practices and suggestions.
Here is queue.c:
#include <stdlib.h>
#include "bank_sim.h"
#include "queue.h"
void initiliaze(Queue *queue) {
queue->head = NULL;
queue->tail = NULL;
}
void enqueue(Queue *queue, Customer *customer) {
if (queue->head == NULL) {
queue->head = customer;
queue->tail = customer;
queue->customer_cnt++;
} else {
queue->tail->next = customer;
queue->tail = customer;
queue->customer_cnt++;
}
}
Customer *dequeue(Queue *queue) {
if (queue->head == NULL) {
return NULL;
} else {
Customer *head_customer = queue->head;
queue->head = queue->head->next;
head_customer->next = NULL;
queue->customer_cnt--;
return head_customer;
}
}
queue.h:
#ifndef QUEUE_H
#define QUEUE_H
#include "bank_sim.h"
typedef struct {
Customer *head;
Customer *tail;
int customer_cnt;
} Queue;
void initiliaze(Queue *queue);
void enqueue(Queue *queue, Customer *customer);
Customer *dequeue(Queue *queue);
# endif
Finally bank_sim.h:
#ifndef BANK_SIM_H
#define BANK_SIM_H
typedef struct Customer Customer;
struct Customer {
int id;
int service_time;
struct Customer *next;
};
typedef struct {
Customer *customer;
int occupied;
} Bank_Teller;
#endif
Comments regarding anything is welcome including whether my structure for Customer
and Queue
can be improved.