#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode * next;
} QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q){
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QueuePtr));
if(!Q.front)exit(ERROR);
Q.front->next=NULL;
return OK;
}
//销毁队列
Status DestroyQueue(LinkQueue &Q){
while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(ERROR);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e){
//若队列不空,则删除Q的队头元素,用e返回元素其值,并返回OK
if(Q.front==Q.rear)return ERROR;
QueuePtr p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
int main(){
LinkQueue Q;
InitQueue(Q);
return 0;
}
队列的练习
最新推荐文章于 2024-07-02 13:22:41 发布