链队列的输入与输出

来源:互联网 发布:个人域名企业备案 编辑:程序博客网 时间:2024/06/03 20:52
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1#define ERROR 0#define OVERFLOW -2typedef int status;typedef int elemtype;typedef struct QNode{elemtype data;struct QNode *next;}QNode,*QueuePtr;//这个是节点typedef struct{QueuePtr front;//队头指针QueuePtr rear;//队尾指针}LinkQueue;//这个是定义队列的首尾指针status creatqueue(LinkQueue &Q)//创建一个队列{Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));if(!Q.front)exit(OVERFLOW);Q.front->next=NULL;return OK;}status inqueue(LinkQueue &Q,elemtype e)//插入元素{QueuePtr p;p =(QueuePtr)malloc(sizeof(QNode));p->data=e;p->next=NULL;Q.rear->next=p;Q.rear=p;return OK;}int  outqueue(LinkQueue &Q,elemtype &e)//删除元素{ QueuePtr p;p=Q.front->next;e=p->data;Q.front->next=p->next;return e;}void output(LinkQueue &Q){QueuePtr p;p=Q.front->next;while(p!=NULL){printf("%d\n",p->data);p=p->next;}}int main(){int i;int e;int j;//int a;LinkQueue Q;creatqueue(Q);printf("输入队列中与几个元素:\n");scanf("%d",&e);printf("输入要插入队列中的元素:\n");for(i=e;i>0;i--){scanf("%d",&j);inqueue(Q,j);}//插入//a=outqueue(Q,e);//队头元素//printf("%d\n",a);output(Q);//输出所有元素return 0;}