GLib Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# Double-ended Queues

## Name

Double-ended Queues -- double-ended queue data structure.

## Synopsis

    #include <glib.h>

    struct      GQueue;
    GQueue*     g_queue_new                     (void);
    void        g_queue_free                    (GQueue *queue);
    void        g_queue_push_head               (GQueue *queue,
                                                 gpointer data);
    void        g_queue_push_tail               (GQueue *queue,
                                                 gpointer data);
    gpointer    g_queue_pop_head                (GQueue *queue);
    gpointer    g_queue_pop_tail                (GQueue *queue);
    gboolean    g_queue_is_empty                (GQueue *queue);
    gpointer    g_queue_peek_head               (GQueue *queue);
    gpointer    g_queue_peek_tail               (GQueue *queue);
    void        g_queue_push_head_link          (GQueue *queue,
                                                 GList *link);
    void        g_queue_push_tail_link          (GQueue *queue,
                                                 GList *link);
    GList*      g_queue_pop_head_link           (GQueue *queue);
    GList*      g_queue_pop_tail_link           (GQueue *queue);

## Description

The GQueue structure and its associated functions provide a standard queue data structure. Internally, GQueue uses the same data structure as GList to store elements.

The data contained in each element can be either integer values, by using one of the Type Conversion Macros, or simply pointers to any type of data.

To create a new GQueue, use g_queue_new().

To add elements, use g_queue_push_head(), g_queue_push_head_link(), g_queue_push_tail() and g_queue_push_tail_link().

To remove elements, use g_queue_pop_head() and g_queue_pop_tail().

To free the entire queue, use g_queue_free().

## Details

### struct GQueue

    struct GQueue
    {
      GList *head;
      GList *tail;
      guint  length;
    };

Contains the public fields of a Queue.

GList *`head` | a pointer to the first element of the queue.
GList *`tail` | a pointer to the last element of the queue.
guint `length` | the number of elements in the queue.

### g_queue_new ()

    GQueue*     g_queue_new                     (void);

Creates a new GQueue.

Returns : | a new GQueue.

### g_queue_free ()

    void        g_queue_free                    (GQueue *queue);

Frees the memory allocated for the GQueue.

`queue` : | a GQueue.

### g_queue_push_head ()

    void        g_queue_push_head               (GQueue *queue,
                                                 gpointer data);

Adds a new element at the head of the queue.

`queue` : | a GQueue.
`data` : | the data for the new element.

### g_queue_push_tail ()

    void        g_queue_push_tail               (GQueue *queue,
                                                 gpointer data);

Adds a new element at the tail of the queue.

`queue` : | a GQueue.
`data` : | the data for the new element.

### g_queue_pop_head ()

    gpointer    g_queue_pop_head                (GQueue *queue);

Removes the first element of the queue.

`queue` : | a GQueue.
Returns : | the data of the first element in the queue, or `NULL` if the queue is empty.

### g_queue_pop_tail ()

    gpointer    g_queue_pop_tail                (GQueue *queue);

Removes the last element of the queue.

`queue` : | a GQueue.
Returns : | the data of the last element in the queue, or `NULL` if the queue is empty.

### g_queue_is_empty ()

    gboolean    g_queue_is_empty                (GQueue *queue);

Returns `TRUE` if the queue is empty.

`queue` : | a GQueue.
Returns : | `TRUE` if the queue is empty.

### g_queue_peek_head ()

    gpointer    g_queue_peek_head               (GQueue *queue);

Returns the first element of the queue.

`queue` : | a GQueue.
Returns : | the data of the first element in the queue, or `NULL` if the queue is empty.

### g_queue_peek_tail ()

    gpointer    g_queue_peek_tail               (GQueue *queue);

Returns the last element of the queue.

`queue` : | a GQueue.
Returns : | the data of the last element in the queue, or `NULL` if the queue is empty.

### g_queue_push_head_link ()

    void        g_queue_push_head_link          (GQueue *queue,
                                                 GList *link);

Adds a new element at the head of the queue.

`queue` : | a GQueue.
`link` : | a single GList element, not a list with more than one element.

### g_queue_push_tail_link ()

    void        g_queue_push_tail_link          (GQueue *queue,
                                                 GList *link);

Adds a new element at the tail of the queue.

`queue` : | a GQueue.
`link` : | a single GList element, not a list with more than one element.

### g_queue_pop_head_link ()

    GList*      g_queue_pop_head_link           (GQueue *queue);

Removes the first element of the queue.

`queue` : | a GQueue.
Returns : | the GList element at the head of the queue, or `NULL` if the queue is empty.

### g_queue_pop_tail_link ()

    GList*      g_queue_pop_tail_link           (GQueue *queue);

Removes the last element of the queue.

`queue` : | a GQueue.
Returns : | the GList element at the tail of the queue, or `NULL` if the queue is empty.

<<< Previous Page | Home | Up | Next Page >>>
Singly-Linked Lists | Trash Stacks