我正在从一本书中学习排队.作者解释了使用以下代码在队列中插入元素的操作.
#define MAX 100
char *p[MAX];
int spos = 0; // spos: holds the index of the **next free** storage location
int rpos = 0;// rpos: holds the index of the next item to retrieve
void qstore(char *q)
{
if(spos==MAX) {
printf("List Full\n");
return;
}
p[spos] = q;
spos++;
}
Run Code Online (Sandbox Code Playgroud)
因此,根据上面的代码,如果spos = 100,即队列中的最后一个元素,则队列已满.现在,由于spos保存下一个空闲存储位置的索引,因此当spos = 100时,数组中的最后一个位置为空.那么为什么它被解释为List full?不应修改此代码,以便它允许填充数组中的最后一个位置,或者我错过了一些非常明显的东西?
谢谢.
| 归档时间: |
|
| 查看次数: |
893 次 |
| 最近记录: |