/*************************************************************************
> File Name: dui.c
> Author: charein
> Mail: chenyayun9@163.com
> Created Time: Wed 12 Feb 2014 08:56:12 AM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct nodet
{
int data;
struct nodet *next;
} node_t;
typedef struct queuet
{
node_t *head;
node_t *tail;
} queue_t;
void initQueue(queue_t* queue_eg)
{
queue_eg -> head = NULL;
queue_eg -> tail = NULL;
}
void enQueue(queue_t *hq, int x)
{
node_t *new_p;
new_p = (node_t *)malloc(sizeof(queue_t));
if (new_p == NULL) {
printf("malloc error\n");
exit(1);
}
new_p->data = x;
new_p->next = NULL;
if (hq->head == NULL) {
hq->head = new_p;
hq->tail = new_p;
} else {
hq->tail->next = new_p;
hq->tail = new_p;
}
return;
}
int is_emptyQueue (queue_t *hq)
{
if (hq->head == NULL)
return 1;
else
return 0;
}
//取出 队首元素,并 删除
int outQueue (queue_t *hq)
{
int temp;
if (hq->head == NULL) {
printf("nothing\n");
exit(1);
}
temp = hq->head->data;
hq->head = hq->head->next;
// if (hq->head == NULL)
// hq->tail == NULL;
return temp;
}
int main (int argc, char** argv)
{
queue_t q;
int a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int i;
initQueue(&q);
for (i=0; i<8; i++)
enQueue(&q, a[i]);
enQueue(&q, 68);
while (!is_emptyQueue(&q))
printf("%d.\n", outQueue(&q));
printf("\n");
return 0;
}
用链表实现队列
最新推荐文章于 2024-06-11 18:17:33 发布