Queue Operator

本文介绍两种队列数据结构的实现方式:链式队列和循环队列。通过C语言展示了如何初始化队列、插入元素、删除元素等基本操作,并提供了显示队列中所有元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 6
typedef 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;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值