链队基本操作

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

队列结点类型定义

typedef struct QNode 
{
	int data;
	struct QNode *next;
}QNode;

队列结构类型定义

typedef struct LQueue
{
	QNode *front;
	QNode *rear;
}LQueue;

链队初始化

void init(LQueue *&Q)
{
	Q=(LQueue*)malloc(sizeof(LQueue));
	Q->front=NULL;
	Q->rear=NULL;
}

结点入队

void enLQueue(LQueue *&Q,int x)
{
	QNode *s;
	s=(QNode*)malloc(sizeof(QNode));
	s->data=x;
	s->next=NULL;
	//rear指向最后一个元素
	if(Q->rear==NULL)//链队为空,则s为队首元素
	{		
		Q->front=Q->rear=s;				
	}
	else
	{
		Q->rear->next=s;//将结点s链接入队
		Q->rear=s;//尾指针后移
	}
	printf("%d入队\n",Q->rear->data);	
}

结点出队

void outQueue(LQueue *&Q)
{
	if(Q->rear==NULL)
		printf("队空,出队失败\n");
	else
	{
	//front指向第一个元素
		printf("%d出队",Q->front->data);
		Q->front=Q->front->next;
	}
}

取队首元素

void head(LQueue *Q)
{
	if(Q->front==NULL)
		printf("队空,无队首元素\n");
	else
	{
		printf("队首元素为%d",Q->front->data);
	}
}

创建链队

void createLQueue(LQueue *Q)
{
	int n;
	printf("请输入队列元素个数\t");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		int m;
		scanf("%d",&m);
		enLQueue(Q,m);	
	}
}

应用

int main()
{
	LQueue *Q;
	init(Q);
	createLQueue(Q);
	outQueue(Q);
	head(Q);
	return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值