[C语言]队列基本操作

#include<stdio.h>
typedef struct dataNode
{
	int data;
	struct dataNode *next;
}qNode;

typedef struct linkQueue
{
	qNode *head;
	qNode *tail;
}linkQueue;

//创建一个新队列
linkQueue* createLinkQueue()
{
	linkQueue *q = (linkQueue*)malloc(sizeof(linkQueue));
	q->head = NULL;
	q->tail = NULL;
	return q;
}

//向队列插入一个结点
linkQueue* insertNode(linkQueue *q,int i)
{
	qNode *node;
	node = (qNode*)malloc(sizeof(qNode));
	node ->data = i;
	node ->next = NULL;
	if(q->tail == NULL)
	{
		q->head = node;
		q->tail = node;
	}
	else
	{
		q->tail->next = node;
		q->tail = node;
	}
	return q;
}

//从队列删除一个结点
linkQueue* deleteNode(linkQueue *q)
{
	qNode *node;
	if(q->head == NULL) 
	{
		printf("the queue is empty \n");
		return q;
	}
	
	node = q->head;
	if(q->head == q->tail)
	{
		q->head = NULL;
		q->tail = NULL;
		free(node);
	}else
	{
		q->head = q->head->next;
		free(node);
	}
	return q;
}

//顺序打印队列
void printLinkQueue(linkQueue *q)
{
	qNode *node;
	node = q->head;
	while(node)
	{
		printf("%d  ",node->data);
		node=node->next;
	}
	printf("\n");
}

void main()
{
	int i ;
	linkQueue *q;
	q = createLinkQueue();
	
	for(i=0;i<=10;i++)
		q = insertNode(q,i);
	
	for(i=0;i<=4;i++)
		q = deleteNode(q);
	
    printLinkQueue(q);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值