队列的C语言
接下来的写法有线性队列的,也有循环队列,代码没有调试,可能运行时有问题
include <stdbool.h>
include <stdio.h>
include <stdlib.h>
define SIZE 6
int top=0;
int rear=0;
int array[SIZE]={0};
bool is_empty()
{
return rear==0;
}
bool is_full()
{
return top==SIZE;
}
void enter(int n)
{
if(is_full()) printf("FULL!");
array[rear++]=n;
}
int leave()
{
if(is_empty()) return -1;
return array[top++];
}
接下来是循环队列,都是取模的方法。判断队列空或满的方式不同,一个是申明一个空间,将top与rear计数分开,一种是申明一个变量计队列,具体想法还在考虑。。。
include <stdio.h>
include <stdlib.h>
include <stdbool.h
define SIZE 6
int array[SIZE+1]={0};
int top=0;
int rear=0;
bool is_empty()
{
return top==rear;
}
bool is_full()
{
return ((rear+1)%SIZE)==top;
}
void enter(int n)
{
if(is_full()) printf("FULL!");
array[(rear++)%(SIZE+1)]=n;
}
int leave()
{
if(is_empty()) return -1;
else
return array[(top++)%(SIZE+1)];
}
方法二
include <stdio.h>
include <stdlib.h>
include <stdbool.h
define SIZE 6
int array[SIZE]={0};
int top=0;
int rear=0;
int count=0;
bool is_empty()
{
return count==0;
}
bool is_full()
{
return count==SIZE;
}
void enter(int n)
{
if(is_full()) printf("FULL!");
array[(rear++)%SIZE]=n;
count++;
}
int leave()
{
if(is_empty()) return -1;
else
return array[(top++)%6];
count--;
}
以上代码均未调试,可以帮我看看能不能实现目的

浙公网安备 33010602011771号