C
Chris M. Thomasson
arnuld said:[...]
struct my_list
{
char arrc[SIZE_ARR];
struct my_list* next;
struct my_list* prev;
};
struct my_queue
{
struct my_list* head;
struct my_list* tail;
};
[...]
FWIW, you can make your queue more generic/flexable by simply defining the
links as:
struct slink {
struct slink* next;
};
You don't need to hard code in a `void*' pointer, or extra space for a user
to store information in. Here is a very simple example code:
___________________________________________________________________
#include <stddef.h>
/* Sample Generic Queue
______________________________________________________________*/
#define CONTAINS(mp_self, mp_struct, mp_member) ((mp_struct*)( \
(unsigned char*)(mp_self) - offsetof(mp_struct, mp_member) \
))
struct slink {
struct slink* next;
};
struct queue {
struct slink* head;
struct slink* tail;
};
#define QUEUE_SINIT { NULL }
#define QUEUE_ITERATE_BEGIN(mp_self, mp_name, mp_struct, mp_member) { \
struct slink* QUEUE_slink__ = (mp_self)->head; \
while (QUEUE_slink__) { \
struct slink* QUEUE_next__ = QUEUE_slink__->next; \
mp_struct* mp_name = CONTAINS(QUEUE_slink__, mp_struct, mp_member)
#define QUEUE_ITERATE_END() \
QUEUE_slink__ = QUEUE_next__; \
} \
} ((void)0)
struct slink*
queue_push(
struct queue* const self,
struct slink* node
) {
if (node) {
node->next = NULL;
if (! self->tail) {
self->head = node;
} else {
self->tail->next = node;
}
self->tail = node;
}
return node;
}
struct slink*
queue_pop(
struct queue* const self
) {
struct slink* node = self->head;
if (node) {
self->head = node->next;
if (! self->head) {
self->tail = NULL;
}
}
return node;
}
/* Sample Application Code
______________________________________________________________*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFSIZE 16U
struct my_slink {
char buf[BUFSIZE];
struct slink slink;
};
struct my_slink*
my_slink_push(
struct queue* const self,
char const* buf
) {
struct my_slink* const my_slink = malloc(sizeof(*my_slink));
if (my_slink) {
size_t len = strlen(buf);
if (len > BUFSIZE - 1) {
len = BUFSIZE - 1;
}
memcpy(my_slink->buf, buf, len);
my_slink->buf[len] = '\0';
queue_push(self, &my_slink->slink);
printf("pushed(%p):%s\n", (void*)my_slink, my_slink->buf);
}
return my_slink;
}
void
my_slink_print(
struct queue* const self
) {
size_t i = 0;
QUEUE_ITERATE_BEGIN(self, iter, struct my_slink, slink);
printf("printing(%lu/%p):%s\n", (unsigned long)i,
(void*)iter, iter->buf);
++i;
QUEUE_ITERATE_END();
}
void
my_slink_flush(
struct queue* const self
) {
size_t i = 0;
struct slink* slink = queue_pop(self);
while (slink) {
struct my_slink* const my_slink = CONTAINS(slink, struct my_slink,
slink);
printf("destroying(%lu/%p):%s\n", (unsigned long)i,
(void*)my_slink, my_slink->buf);
free(my_slink);
++i;
slink = queue_pop(self);
}
}
int main(void) {
struct queue queue = QUEUE_SINIT;
my_slink_push(&queue, "Chris");
my_slink_push(&queue, "John");
my_slink_push(&queue, "Steve");
my_slink_push(&queue, "Lindsay");
my_slink_push(&queue, "Richard");
my_slink_push(&queue, "Tommy");
my_slink_print(&queue);
my_slink_flush(&queue);
my_slink_push(&queue, "Lisa");
my_slink_push(&queue, "Jane");
my_slink_push(&queue, "Mary");
my_slink_push(&queue, "This String Is Too Long!!");
my_slink_print(&queue);
my_slink_flush(&queue);
return 0;
}
___________________________________________________________________
Here is the code in pastebin just in case the post gets mangled by your
newsreader:
http://pastebin.com/m4b4bc009