队列这种结构,其实就跟现实中排队买票类似,先排队的,先买票。队列的特性就是先进先出。
队列的存储有顺序存储和链式存储之分,本文主要介绍如何用链表来实现队列。
为了实现的方便,我们添加一个头指针指向队列的尾(rear),即头指针指向新入队的元素。
队列的入队操作,具体如下图
新的元素element入队时,只需要将element的next指针指向head->next,然后head->next再指向element就可以了
而队列的出队操作,需要先找到队列的front节点,然后再做出队操作。
具体看代码的实现
queue.h
#ifndef __queue_H__
#define __queue_H__
typedef int ElementType;
typedef struct QUEUE_T
{
ElementType value;
int length;
struct QUEUE_T *next;
}QUEUE;
typedef struct QUEUE_T Node;
#define QUEUE_MAX_LEN 10
extern void queue_main_test(void);
#endif
queue.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"
QUEUE * queue_create(void)
{
QUEUE * queue = (QUEUE *)malloc(sizeof(QUEUE));
if(NULL == queue)
{
printf("queue malloc fail\n");
return NULL;
}
memset(queue, 0, sizeof(QUEUE));
queue->length = 0;
queue->next = NULL;
return queue;
}
bool queue_is_empty(QUEUE *queue)
{
return queue->next == NULL;
}
bool queue_is_full(QUEUE *queue)
{
return queue->length == QUEUE_MAX_LEN;
}
ElementType queue_front(QUEUE *queue)
{
QUEUE * q = queue;
if(!q->next)
return -1;
while(q->next)
q = q->next;
return q->value;
}
ElementType queue_back(QUEUE *queue)
{
return queue->next->value;
}
int queue_in(QUEUE *queue, ElementType elem)
{
#if 0
if(true == queue_is_full(queue))
{
printf("Queue is full\n");
return -1;
}
#endif
Node *node = (Node *)malloc(sizeof(Node));
if(NULL == node)
{
printf("node malloc fail\n");
return -1;
}
memset(node, 0, sizeof(Node));
memcpy(&node->value, &elem, sizeof(ElementType));
node->next = queue->next;
queue->next = node;
//queue->length++;
return 0;
}
void queue_out(QUEUE *queue)
{
QUEUE *q,*temp;
q = temp = queue;
if(!queue || !queue->next)
return;
while(q->next)
{
temp = q;
q = q->next;
}
temp->next = NULL;
//queue->length--;
printf("out value=%d\n", q->value);
free(q);
}
/* traverse from back to front */
void queue_traverse(QUEUE *queue)
{
QUEUE * q = queue;
if(!queue || !queue->next)
return;
while(q->next)
{
printf("value=%d\n", q->next->value);
q = q->next;
}
}
void queue_main_test(void)
{
int i;
QUEUE * q = queue_create();
for(i=1; i<=10; i++)
queue_in(q, i);
printf("front:%d\n", queue_front(q));
printf("back:%d\n", queue_back(q));
queue_traverse(q);
}
329





