C语言链式队列添加元素,创建

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
	int Data;
	struct node* Next;
}Node;
typedef struct HNode{
	 Node* Front;//分别为节点的指针类型 
	 Node* rear;
	int length;
}Quene;
Quene CreateQ (Quene Q)
{
	Q.Front=Q.rear=(Node*)malloc(sizeof(Node));
	Q.Front->Next=NULL;
	return Q;
}
int IsEmpty(Quene *Q);
void AddQ(Quene *Q,int data)
{
	Node* k=(Node*)malloc(sizeof(Node));
	k->Data=data;
	k->Next=NULL;
	Q->rear->Next=k;//完成前单元对后单元的连接 
	Q->rear=k;//进行尾指针的移动 
	Q->length++;
}
bool DeleteQ(Quene *Q)
{
	if(IsEmpty(Q))
	{
	 	return false;
	}
	else 
	{
		Q->Front->Next=Q->Front->Next->Next;//头节点的区域始终没有存数据,在头的Next是第一个 
		return true;
	}
}
int IsEmpty(Quene *Q)
{
	if(Q->Front==Q->rear)
	return 1;
	else
	return 0;
}
void PrintQ(Quene Q)
{
	
	while(Q.Front!=Q.rear)
	{
		printf("%d\n",Q.Front->Next->Data);
		Q.Front->Next = Q.Front->Next->Next;
	}
	return ;
}
int main()
{
	Quene Q;
	Q.Front=Q.rear=NULL;
	Q=CreateQ(Q);
	AddQ(&Q,100);
	AddQ(&Q,10);
	DeleteQ(&Q);
	PrintQ(Q);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值