#define MAXSIZE 100
typedef int Datatype;
typedef struct {
Datatype a[MAXSIZE];
int front;
int rear;
}sequennce_queue;
void init(sequennce_queue *sq)
{
sq->front = 0;
sq->rear = 0;
}
int empty(sequennce_queue sq)
{
return (sq.front == sq.rear ? 1 : 0);
}
void display(sequennce_queue sq)
{
int i;
if (empty == 1)
printf("順序表爲空!\n");
else
{
for (i = sq.front; i < sq.rear; i++)
printf("%5d", sq.a[i]);
putchar('\n');
}
}
Datatype get(sequennce_queue sq)//取得隊首的值
{
if (empty == 1)
{
printf("順序表爲空!,無法獲取隊首的值。\n");
return 0;
}
return sq.a[sq.front];
}
void insert(sequennce_queue *sq, Datatype x)
{
int i;
if (sq->rear == MAXSIZE)
printf("隊列已滿,無法進行插入!\n");
else
{
sq->a[sq->rear] = x;
++sq->rear;
}
}
void dele(sequennce_queue *sq)
{
if (empty == 1)
{
printf("順序表爲空!,無法進行刪除。\n");
}
else
++sq->front;
}
void main()
{
int i;
sequennce_queue SQ;
init(&SQ);
printf("隊列空爲1,非空爲0:%d\n", empty(SQ));
for (i = 0; i < 5; i++)
insert(&SQ, i*2 );
display(SQ);
printf("隊列空爲1,非空爲0:%d\n", empty(SQ));
printf("首部的元素爲:%d\n", get(SQ));
dele(&SQ);
printf("首部的元素爲:%d\n", get(SQ));
}