修改后代码如下,你看看应该有一些不一样的地方,队列很多判断你都写错了,这个队列好像是从数组的顶部往下存储的。。
#include
#include
#define maxsize 1024
typedef char datatype;
typedef struct
{
datatype data[maxsize];
int front;
int rear;
}sequeue;
void setnull(sequeue *sq) //置空
{
sq->front = maxsize - 1;
sq->rear = maxsize - 1;
}
int empty(sequeue *sq) //判空
{
if (sq->rear == sq->front)
return 1;
else
return 0;
}
datatype get_front(sequeue *sq) //取*sq的队头元素
{
if (empty(sq) == 1)
{
printf("queue is empty!\n");
return 0;
}
else
return sq->data[sq->front];
}
void enqueue(sequeue *sq) //入队
{
char x;
int i, j;
if (sq->front == (sq->rear - 1+maxsize) % maxsize)
{
printf("queue is full!\n");
}
else
{
printf("请输入数据数量i\n");
scanf("%d", &i);
getchar();
for (j = 0; j {
printf("请输入数据到队列中!\n");
scanf("%c", &x);
getchar();
sq->data[sq->rear] = x;
sq->rear--;
sq->rear = (sq->rear + maxsize) % maxsize;
}
}
}
datatype dequeue(sequeue *sq) //出队
{
datatype v=0;
if (empty(sq) == 1)
{
printf("队列为空!\n");
}
else
{
v = sq->data[sq->front];
sq->front = (sq->front - 1+maxsize) % maxsize;
}
return v;
}
void print(sequeue *sq)
{
int p = sq->front;
while (p != sq->rear)
{
printf("%5c\n", sq->data[p]);
p--;
p = (p + maxsize) % maxsize;
}
}
int main()
{
sequeue a;
sequeue *sq = &a;
setnull(sq);
enqueue(sq);
print(sq);
return 0;
}