用C实现的一个简单的队列
list.h
#include <stdio.h>
#include <stdlib.h>
struct LinkList
{
int score;
struct LinkList *next;
};
typedef struct LinkList List;
测试例子
#include "list.h"
typedef struct Queue{
int list_count;
List *head,*tail;
} Queue;
Queue t_queue;
void queue_init(Queue *q)
{
q->head = NULL;
q->tail = NULL;
printf("init Queue successful !!! \n");
}
int queue_put(Queue *q,int score)
{
List *tamp;
tamp = (List*) malloc(sizeof(List));
tamp->score = score;
tamp->next = NULL;
if(q->tail == NULL)
{
q->head = tamp;
}else
{
q->tail->next = tamp;
}
q->tail = tamp;
q->list_count++;
return 0;
}
int queue_get(Queue *q)
{
int score;
List *tamp;
tamp = (List*) malloc(sizeof(List));
if(q->head == NULL)
{// 队列为空
printf("Queue is NULL!!! \n");
}else
{
while(q->head != NULL)
{
tamp = q->head;// 先拿到对列头
q->head = tamp->next;// 然后重新给队列头赋值->相当于原先的队列头被删除了
q->list_count--;
score = tamp->score;
printf("now queue count = %d ,score = %d !!!\n",q->list_count,score);
if(q->head == NULL)
{// 如果新的队列头为空了,队列也就空了
q->tail = NULL;
break;
}
}
return score;
}
return 0;
}
int main(int argc, char **argv) {
// 初始化队列
queue_init(&t_queue);
// 往队列中存放数据
int j = 150;
while (j < 153)
{
queue_put(&t_queue,j);
j++;
}
// 从队列中取数据
queue_get(&t_queue);
return 0;
}