PDA

Просмотр полной версии : Пример циклической очереди на Turbo C.


IgrikX
28.03.2010, 19:20
Есть у кого пример циклической очереди на С. Срочно нужен!

greki_hoy
28.03.2010, 21:57
2IgrikX - вот схема однонаправленной (не деки) кольцевой очереди


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

typedef struct queue
{
struct node *head;
struct node *tail;
size_t count;
} queue;

typedef struct node
{
struct node *prev;
struct node *next;
void *data;
} node;

queue *new_queue(queue *q)
{
q = malloc(sizeof(*q));
q->count = 0;
q->head = NULL;
q->tail = NULL;
return q;
}

typedef void item;

node *add_item_queue(queue *q, item *it, size_t size)
{
node *n = malloc(sizeof(*n));
if (q->count == 0)
{
q->head = n;
n->next = NULL;
n->prev = NULL;
}
else
{
n->next = q->head;
n->prev = q->tail;
q->tail->next = n;
}
q->tail = n;
q->count++;
n->data = malloc(size);
memmove(n->data, it, size);
return n;
}

node *get_queue_head(queue *q)
{
return q->head;
}

int main(void)
{
queue *q = NULL;
node *n;
q = new_queue(q);
add_item_queue(q, "Phoenix", 8);
add_item_queue(q, "Zombie", 7);
add_item_queue(q, "Paradox", 8);
n = get_queue_head(q);
for (;;)
{
puts(n->data);
n = n->next;
}
}

несложно переделать в кольцевой дек