队列的入队和出队的c语言程序,2019年C语言-初始化队列+入队+出队 列+销毁队列完整版完 整版.doc...

链队列题目:初始化队列+入队列+出队列+销毁队列 (1)初始化一个链队列;(2)在初始化好的链队列中放入数,入队列,完成后要求显示;(3)从队列中出队列,要求显示出来的元素和之后的队列;(4)销毁创建的队列,释放内存;#include#include#define NULL 0typedef int QElemType;typedef struct QNode{QElemType data;struct QNode *next;} QNode,*QueuePtr;typedef struct{QueuePtr front;QueuePtr rear;}LinkQueue;void CreateQueue(LinkQueue *Q){ int a;QueuePtr p;p=(QueuePtr)malloc(sizeof(QNode)); if(!p) printf("创建失败") ;else {p->next=NULL; Q->front=p; Q->rear=p; scanf("%d",&a); while(a!=-1) {p=(QueuePtr)malloc(sizeof(QNode));p->next=NULL; if(!p) printf("创建失败") ; else { p->data=a; Q->rear->next=p; Q->rear=p; scanf("%d",&a); } } }}void PrintfQueue(LinkQueue *Q){ QueuePtr p;for(p=Q->front->next;p!=NULL;p=p->next) { printf(" %d",p->data); }}void EnQueue(LinkQueue *Q,QElemType x) { QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode)); if(!p) printf("创建失败") ;else { p->data=x;p->next=NULL;Q->rear->next=p;Q->rear=p;}}void DeQueue(LinkQueue *Q,QElemType *f){ QueuePtr p;if(Q->front==Q->rear) printf("\nError");else { p=Q->front->next; *f=p->data; Q->front->next=p->next;if(Q->rear==p) Q->rear=Q->front;free(p);}}

void DeleteQueue(LinkQueue *Q){QueuePtr p; for(;Q->front!=NULL;free(p)) { p=Q->front; Q->front=Q->front->next;}

}void main(){ int *f,e; LinkQueue Queue,*Q; e=0;f=&e;Q=&Queue;printf("\n输入队列的元素依次为:");CreateQueue(Q);printf("\n初始队列为:");PrintfQueue(Q);printf("\n要入队的元素为:");scanf("%d",&e);EnQueue(Q,e);printf("\n入队后的队列为:");PrintfQueue(Q);DeQueue(Q,f);printf("\n出队的元素为:");printf("%d",e); printf("\n出队后的队列为:");PrintfQueue(Q);DeleteQueue(Q);}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值