Simple Queue Operator
#include <iostream.h>#include <stdio.h>#include <malloc.h>typedef struct student{ int data; struct student *next;}SNode;typedef struct linkqueue{ struct student *front,*rear;}LQueue;bool InitLQueue(LQueue &LQ){ LQ.front=(SNode *)malloc(sizeof(SNode)); if(!LQ.front) return false; LQ.rear=LQ.front; LQ.front->next=NULL; return true;}bool InsertQueue(LQueue &LQ,int x){ SNode *s; s=(SNode*)malloc(sizeof(SNode)); if(!s) return false; s->data=x; s->next=NULL; LQ.rear->next=s; LQ.rear=s; return true;}bool DeQueue(LQueue &LQ, int &e){ if(LQ.front == LQ.rear) return false; SNode * s=LQ.front->next; e=s->data; LQ.front->next=s->next; if(LQ.rear==s) LQ.rear=LQ.front; free(s); return true;}int GetLengthLQ(SNode *LN){ int n=0; SNode *p=LN; while(p) { p=p->next; n++; } return n;}void DisplayLQ(LQueue &LQ){ SNode *p=LQ.front->next; while(p){ printf("%d/n",p->data); p=p->next; }}
-------------------------------------------------------------------------------------------------------------------
Loop Queue Operator
#include <stdio.h>#include <malloc.h>#define MAXQSIZE 6typedef struct { int *base; int front; int rear;}LSQueue;
bool InitQueue(LSQueue &Q){ Q.base= (int *) malloc (MAXQSIZE*sizeof(int)); if(!Q.base)return false; Q.front=Q.rear=0; return true;}bool EnQueue(LSQueue &Q,int e){ if((Q.rear+1)%MAXQSIZE == Q.front)return false; Q.base[Q.rear]=e; Q.rear= (Q.rear+1)% MAXQSIZE; return true;}bool DeQueue(LSQueue &Q,int &e){ if(Q.front == Q.rear) return false; e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; return true;}void DisplayLSQ(LSQueue &Q){ LSQueue p =Q; while(p.front != p.rear){ printf("%d/n",p.base[p.front]); p.front = (p.front +1)%MAXQSIZE; }}