我用C语言编写了一个顺序队列的基本操作,但是出现了问题,麻烦大家帮忙看看,在线急等!!!!
#include<stdio.h>#include<stdlib.h>#definemaxsize1024typedefchardatatype;typedefstruc...
#include <stdio.h>
#include <stdlib.h>
#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->front+1)%maxsize;
}
void enqueue(sequeue *sq) //入队
{
char x;
int i,j;
if(sq->front==(sq->rear+1)%maxsize)
{
printf("queue is full!\n");
}
else
{
printf("请输入数据数量i\n");
scanf("%d",&i);
for(j=0;j<i;j++)
{
printf("请输入数据到队列中!\n");
scanf("%d",&x);
getchar();
sq->data[sq->rear]=x;
sq->rear--;
}
}
}
datatype dequeue(sequeue *sq) //出队
{
if(empty(sq)==1)
{
printf("队列为空!\n");
}
else
{
sq->front=(sq->front+1)%maxsize;
}
return (sq->data[sq->front]);
}
void print(sequeue *sq)
{
while(sq->front!=0)
{
printf("5%c\n",sq->data[sq->front]);
sq->front--;
}
}
int main()
{
sequeue a;
sequeue *sq=&a;
setnull(sq);
enqueue(sq);
print(sq);
return 0;
} 展开
#include <stdlib.h>
#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->front+1)%maxsize;
}
void enqueue(sequeue *sq) //入队
{
char x;
int i,j;
if(sq->front==(sq->rear+1)%maxsize)
{
printf("queue is full!\n");
}
else
{
printf("请输入数据数量i\n");
scanf("%d",&i);
for(j=0;j<i;j++)
{
printf("请输入数据到队列中!\n");
scanf("%d",&x);
getchar();
sq->data[sq->rear]=x;
sq->rear--;
}
}
}
datatype dequeue(sequeue *sq) //出队
{
if(empty(sq)==1)
{
printf("队列为空!\n");
}
else
{
sq->front=(sq->front+1)%maxsize;
}
return (sq->data[sq->front]);
}
void print(sequeue *sq)
{
while(sq->front!=0)
{
printf("5%c\n",sq->data[sq->front]);
sq->front--;
}
}
int main()
{
sequeue a;
sequeue *sq=&a;
setnull(sq);
enqueue(sq);
print(sq);
return 0;
} 展开
展开全部
修改后代码如下,你看看应该有一些不一样的地方,队列很多判断你都写错了,这个队列好像是从数组的顶部往下存储的。。
#include <stdio.h>
#include <stdlib.h>
#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<i; 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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询