本文共 1243 字,大约阅读时间需要 4 分钟。
#include <stdio.h>
#include <malloc.h>typedef struct Qnode{ int data; struct Qnode *next;}Qnode,*QueuePtr;//创建链typedef struct { QueuePtr front;//队头指针 QueuePtr rear;//队尾指针}LinkQueue;//创建队列void EnQueue(LinkQueue &Q,int e)
{ QueuePtr p=(QueuePtr)malloc(sizeof(Qnode)); p->data=e;p->next=NULL; Q.rear->next=p; Q.rear=p;}//插入e为队尾元素void DeQueue(LinkQueue &Q, int &e)
{ if(Q.front==Q.rear) printf("error/n"); QueuePtr p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p);}//删除队头元素int QueueEmpty(LinkQueue Q)
{ if(Q.front==Q.rear) return 1; else return 0;}//判断链栈是否为空void InitQueue(LinkQueue &Q)
{ Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode)); Q.front->next=NULL; printf("输入6个队列元素:"); for(int i=0;i<6;i++) { int a; scanf("%d",&a); EnQueue(Q,a);//向队列中插元素 }}//初始化栈void Print(LinkQueue Q){ while(!QueueEmpty(Q)) { int e; DeQueue(Q,e); printf("%d ",e); }}//输出栈元素
void DestroyQueue(LinkQueue &Q)
{ while(Q.front) { Q.rear=Q.front->next; free(Q.front); Q.front=Q.rear; }}//销毁链队列int QueueLength(LinkQueue Q)
{ int j=0; while(Q.front->next) { Q.front=Q.front->next; j++; } return j;}//求链栈长度main()
{ int a;//存储链队列长度 LinkQueue Q; InitQueue(Q); a=QueueLength( Q); Print (Q); printf("链队列长%d",a); return 0;}
转载地址:http://pcmvi.baihongyu.com/