队列(Queue)
队列规则:先进先出(First In First Out)
使用单向链表来实现对列
typedef struct Node{
int value;
struct Node *next;
} Node;
typedef struct Queue{
Node *head;
Node *last;
}Queue;
1.队列初始化:QueueInit
QueueInit(Queue *q){
q->head = q->last = NULL;
}
2.队列销毁:QueueDestory
QueueDestory(Queue *q){
Node *cur,*next;
for(cur = q->head;cur!=NULL;cur = next){
next = cur->next;
free(cur);
}
q->head = q->last = NULL;
}
3.入队列:QueuePush
QueuePush(Queue *q,int v){
Node *node = (Node *)malloc(sizeof(Node));
node->value = v;
node->next = NULL;
if(q->head == NULL){
q->head = node;
q->last = node;
}
else
{
q->last->next = node;
q->last = node;
}
}
4.出队列:QueuePop
QueuePop(Queue *q){
//头删
Node *second = q->head->next;
free(q->head);
q->head = second;
if(q->head == NULL){
q->last = NULL;
}
}
5.队列第一个元素:QueueFront
QueueFront(){
return q->head->value;
}
6.队列大小:QueueSize
QueueSize(){
int size = 0;
for(Node*c = q->head;c!=NULL;c = c->next)
{
size++;
}
return size;
}
7.判断队列是否为空:QueueEmpty
QueueEmpty(){
if(q->head == NULL)
{
return 1;
}
else
return 0;
}