数据结构(C语言版)链队列的基本操作

#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define  OK   1
#define  ERROR 0
#define  OVERFLOW  -1
typedef   int  QElemType;
typedef   int  Status;
/***************存储结构**************/
typedef struct QNode{
	QElemType  data;
	struct QNode *next;
}QNode,*Queueptr;
typedef struct{
	Queueptr front;
	Queueptr rear;
}LinkQueue;
/**************初始化**************/
Status InitQueue_L(LinkQueue *Q){
	Q->front=Q->rear=(Queueptr)malloc(sizeof(QNode));
	if(!Q->front)exit(OVERFLOW);
	Q->front->next=NULL;
	return OK;
}
/**************排队**************/
Status EnQueue_L(LinkQueue *Q,QElemType e){
	Queueptr P;
	P=(Queueptr)malloc(sizeof(QNode));
	P->data=e;
	P->next=NULL;
	Q->rear->next=P;
	Q->rear=P;
	return OK;
}
/**************出队**************/
Status DeQueue_L(LinkQueue *Q,QElemType *e){
	Queueptr P;
	if(!Q->front->next)return ERROR;
	P=Q->front->next;
	*e=P->data;
	Q->front=P->next;
	if(P==Q->rear)Q->rear=Q->front;
	free(P);
	return OK;
}
/**************销毁**************/
Status DestroyQueue_L(LinkQueue *Q){
	while(Q->front){
		Q->rear=Q->front->next;
		free(Q->front);
		Q->rear=Q->front;
	}
	return OK;
}
/**************main**************/
main()
{
	LinkQueue Q1;
	int i,n,e;
	if(InitQueue_L(&Q1)==OK)printf("Init is ok!\n");
	printf("Please enter value for n:\n");
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d",&e);
		if(EnQueue_L(&Q1,e)!=OK)break;
	}
	if(DeQueue_L(&Q1,&e)==OK)printf("被删除对头元素为:%d\n",e);
	else printf("Delete operation is wrong.\n");
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值