队列是一种特殊的顺序表,尾插头出,简称fifo.
game.h
#pragma once
#define LtData int
#include<stdbool.h>
typedef struct list
{
LtData o;
struct list* nod;
}node;
typedef struct queue
{
node* head;
node* list;
int size;
}q;
void* linit(q* e);
void push(q* list, LtData x);
void pop(q* list);
bool empty(q* list);
void destroy(q* list);
LtData backnum(q* q);
int queuesize(q* q);
game.c
#include"game.h"
#include<assert.h>
#include<stdio.h>
#include<stdbool.h>
#include<ctype.h>
void* linit(q* e)
{
e->head = e->list = NULL;
e->size = 0;
}
void push(q* q, LtData x)
{
node*tail= malloc(sizeof(node));
tail->o = x;
tail->nod = NULL;
assert(q);
if (q->head == NULL)
{
q->head=tail;
q->list=tail;
}
else
{
q->list->nod = tail;
q->list = q->list->nod;
}
q->size++;
}
void pop(q* q)
{
assert(q);
assert(empty(q));
node *p= q->head;
q->head = q->head->nod;
free(p);
q->size--;
}
bool empty(q* q)
{
if (q->size==0)
return false;
return true;
}
void destroy(q* q)
{
assert(q);
assert(q->head);
while (q->head)
{
node* e = q->head;
q->head = q->head->nod;
free(e);
}
q->list = 0;
q->size = 0;
}
int queuesize(q* q)
{
//
return q->size;
}
LtData backnum(q* q)
{
return q->list->o;
}