用链表实现队列

/*************************************************************************
    > 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值