这是《数据结构案例教程》上的,用了位运算,个人认为写的十分巧妙。
#include<stdio.h>
#include<stdlib.h>
void farmerProblem();
#define MAXQSIZE 100
typedef int ElemType;
typedef struct
{
ElemType data[MAXQSIZE];
int front,rear;
}SqQueue;
void Init_SqQueue(SqQueue *Q)
{
Q -> front = Q -> rear = 0;
}
int Empty_SqQueue(SqQueue *Q)
{
return(Q -> rear == Q ->front);
}
void In_SqQueue(SqQueue *Q,int e)
{
if(Q -> rear == MAXQSIZE)
{
return;
}
Q -> data[Q -> rear] = e;
Q -> rear += 1;
}
void Out_SqQueue(SqQueue *Q,int *e)
{
if(Q -> rear == Q -> front)
{
return;
}
*e = Q -> data[Q -> front];
Q -> front += 1;
}
int farmer(int location)
{
return(0 != (location & 0x08));
}
int wolf(int location)
{
return(0 != (location & 0x04));
}
int cabbage(int location)
{
return(0 != (location & 0x02));
}
int goat(int