'How do I use the header that has struct file in C?
I'm new to c and a little slow so bear with me. I was given this code as a header file and I was asked to create the Queue.c. I am confused on how to approach that.
What exactly do I do with the struct in order to initialize the queue?
I have looked online and I couldn't find what I needed in order to figure out how to implement it. I also don't know how to make the Qiterator().
#include <pthread.h> // Provides thread-safe mutex lock
#include <stdbool.h> // Provides boolean data type
#include "Car.h" // Provides the required Car type structure
/* =============================================================================
* Also use the extra field "list" to write function [Qiterator] that returns
* a list used to traverse all the elements of the queue starting at the head
* and ending at the tail without changing the state of the queue.
* =============================================================================
*/
typedef struct Queue_t {
Car **data; // Array to hold car queue
Car **list; // Array to hold car list
int capacity; // The array (queue) capacity
int count; // Number of cars currently in the queue
int tail; // The queue tail position
int head; // The queue head position
} Queue;
/*
* Initialize the fields of a Queue structure instance.
*/
void Qinit(int n);
void Qfree();
/* =============================================================================
* Clear the Queue.
* =============================================================================
*/
void Qclear();
void Qenqueue(Car *car);
Car* Qserve();
Car* Qpeek();
/* ===========================================================================
* Return a list of the queue contents and its size.
* ===========================================================================
*/
Car** Qiterator(int *sz);
int Qcapacity();
int Qsize();
bool QisFull();
bool QisEmpty();
and the header file for car
typedef struct Car_t {
int cid; // The car's unique ID
int vid; // The in-valet's ID
int sno; // The parking slot number
time_t atm; // The time of arrival (creation)
time_t ptm; // The time of parking (start time)
time_t ltm; // The expected time to leave (end time)
char pn[20]; // The car's image file name
char pnf[20]; // The car's flipped-image file name
} Car;
/*
* Initialize the newly created car by setting the following fields:
* - cid is set to a unique car ID.
* - sno is set to 0.
* - atm is set to the current time -- time of creation.
* - ltm is set to a random time limited to 180 seconds -- time to stay parked.
* - pn is set to the file name of a random car image limited to 13.
* - pnf is set to the file name of the horizontaly flipped same image as pn.
* =============================================================================
*/
void CarInit(Car *car);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
