seqqueue.h
#ifndef __SEQQUEUE_H__
#define __SEQQUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 10
typedef int datatype;
typedef struct{
datatype data[MAXSIZE];
int front,rear;
}queuenode,*seqqueue;
extern void create_seqqueue(seqqueue *L);
extern bool is_empty_seqqueue(seqqueue L);
extern bool is_full_seqqueue(seqqueue L);
extern int get_number_seqqueue(seqqueue L);
extern bool in_seqqueue(seqqueue L,datatype x);
extern bool out_seqqueue(seqqueue L,datatype *x);
extern void show_seqqueue(seqqueue L);
#endif
seqqueue.c
#include "seqqueue.h"
void create_seqqueue(seqqueue *L)
{
if((*L = (seqqueue)malloc(sizeof(queuenode))) == NULL)
{
printf("malloc no memory!\n");
return ;
}
(*L)->front = (*L)->rear = MAXSIZE-1;
}
bool is_empty_seqqueue(seqqueue L)
{
return (L->front == L->rear);
}
bool is_full_seqqueue(seqqueue L)
{
return ((L->rear + 1)%MAXSIZE == L->front);
}
int get_number_seqqueue(seqqueue L)
{
int n=0;
int r;
r= (L->front+1)%MAXSIZE;
while(r != (L->rear + 1)%MAXSIZE)
{
n++;
r= (r +1)%MAXSIZE;
}
return n;
}
bool in_seqqueue(seqqueue L,datatype x)
{
printf("in---->data:%d front:%d rear:%d\n",L->data[L->rear],L->front,L->rear);
if(is_full_seqqueue(L))
{
printf("queue is full!\n");
return 0;
}
L->rear = (L->rear +1)%MAXSIZE;
L->data[L->rear] = x;
printf("in---->data:%d front:%d rear:%d\n",L->data[L->rear],L->front,L->rear);
return 1;
}
bool out_seqqueue(seqqueue L,datatype *x)
{
printf("out---->data:%d front:%d rear:%d\n",L->data[L->front],L->front,L->rear);
if(is_empty_seqqueue(L))
{
printf("queue is empty!\n");
return 0;
}
L->front = (L->front + 1)%MAXSIZE;
*x = L->data[L->front];
printf("out---->data:%d front:%d rear:%d\n",L->data[L->front],L->front,L->rear);
return 1;
}
void show_seqqueue(seqqueue L)
{
int i=0;
seqqueue r;
r = L;
for(i = (r->front + 1)%MAXSIZE;i != (r->rear + 1)%MAXSIZE;i = (i + 1)%MAXSIZE)
{
printf("%d ",r->data[i]);
}
puts("");
}
main.c
#include "seqqueue.h"
int main(int argc, const char *argv[])
{
datatype x;
seqqueue H;
create_seqqueue(&H);
show_seqqueue(H);
while(1)
{
printf("Please input(number in <--->character out)[-1 exit:");
if(scanf("%d",&x))
{
if(x == -1)
{
break;
}
in_seqqueue(H,x);
show_seqqueue(H);
printf("number:%d\n",get_number_seqqueue(H));
}
else
{
getchar();
out_seqqueue(H,&x);
printf("out:%d\n",x);
show_seqqueue(H);
printf("number:%d\n",get_number_seqqueue(H));
}
}
show_seqqueue(H);
return 0;
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cc122f30ea6e6aea69b9220d5f9cc45f.png)