A general-purpose queue data structure
Let's develop a first-in-first-out (FIFO) data structure that queues
(almost) arbitrary data.
We're hoping to address the main problems that were exhibited by the
stack and list data structures:
- We should be able to manage the data without knowing what it is.
- We'd like operations, such as appending, to be independent of the
number of items already stored.
Such (highly desirable) operations are performed in a constant-time.
typedef struct _e {
void *data;
size_t datalen;
struct _e *next;
} ELEMENT;
typedef struct {
ELEMENT *head;
ELEMENT *tail;
} QUEUE;
|
Of note:
- We've introduced a new datatype, ELEMENT, to hold each
individual item of data.
- Because we don't require our functions to "understand" the data
they're queueing,
each element will just hold a void pointer to the
data it's holding, and remember its length.
- Our "traditional" datatype QUEUE now holds 2 pointers -
one to the head of the list of items, one to the tail.
CITS2002 Systems Programming, Lecture 19, p13, 3rd October 2023.
|