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

时间:2021-08-05 10:23:39
#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;
}