073.队列实例
【摘要】
#include <string.h>#include <stdlib.h>#include <stdio.h>#include <ctype.h> #define MAX 100 char *p[MAX], *qretrieve(void);int spos = 0;int rpos =...
-
#include <string.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <ctype.h>
-
-
#define MAX 100
-
-
char *p[MAX], *qretrieve(void);
-
int spos = 0;
-
int rpos = 0;
-
void enter(void), qstore(char *q), review(void), delete_ap(void);
-
-
int main(void)
-
{
-
char s[80];
-
register int t;
-
-
for(t=0; t < MAX; ++t) p[t] = NULL; /* init array to nulls */
-
-
for(;;) {
-
printf("Enter, List, Remove, Quit: ");
-
gets(s);
-
*s = toupper(*s);
-
-
switch(*s) {
-
case 'E':
-
enter();
-
break;
-
case 'L':
-
review();
-
break;
-
case 'R':
-
delete_ap();
-
break;
-
case 'Q':
-
exit(0);
-
}
-
}
-
return 0;
-
}
-
/* Enter appointments in queue. */
-
void enter(void)
-
{
-
char s[256], *p;
-
-
do {
-
printf("Enter appointment %d: ", spos+1);
-
gets(s);
-
if(*s==0) break; /* no entry */
-
p = (char *) malloc(strlen(s)+1);
-
if(!p) {
-
printf("Out of memory.\n");
-
return;
-
}
-
strcpy(p, s);
-
if(*s) qstore(p);
-
} while(*s);
-
}
-
-
/* See what's in the queue. */
-
void review(void)
-
{
-
register int t;
-
-
for(t=rpos; t < spos; ++t)
-
printf("%d. %s\n", t+1, p[t]);
-
}
-
-
/* Delete an appointment from the queue. */
-
void delete_ap(void)
-
{
-
char *p;
-
-
if((p=qretrieve())==NULL) return;
-
printf("%s\n", p);
-
}
-
-
/* Store an appointment. */
-
void qstore(char *q)
-
{
-
if(spos==MAX) {
-
printf("List Full\n");
-
return;
-
}
-
p[spos] = q;
-
spos++;
-
}
-
-
/* Retrieve an appointment. */
-
char *qretrieve(void)
-
{
-
if(rpos==spos) {
-
printf("No more appointments.\n");
-
return NULL;
-
}
-
rpos++;
-
return p[rpos-1];
-
}
文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_41055260/article/details/124518307
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)