[英]Implementation of simple queue data structure in C
我试图在C编程中实现一个简单的队列。 但我遇到了以下错误。 任何人都可以找到我的程序有什么问题
#include<stdio.h>
#include<stdlib.h>
#define QUENE_SIZE 10
struct queue_structure{
unsigned int queue_front, queue_rear;
int queue_array[10];
};
void enqueue(struct queue_structure *,int value);
int dequeue(struct queue_structure *);
int queue_full(struct queue_structure *);
int queue_empty(struct queue_structure *);
int main(){
struct queue_structure *queue_pointer = malloc(sizeof(struct queue_structure));
int option,number,i,dequeue_number;
queue_pointer->queue_front = queue_pointer->queue_rear = -1;
while(1){
printf("Enter the option:\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the number to be enqueued:\n");
scanf("%d",&number);
if(queue_full(queue_pointer) != 1){
enqueue(queue_pointer,number);
}
else{
printf("The queue is full");
}
break;
case 2:
if(queue_empty(queue_pointer) != 1){
dequeue_number = dequeue(queue_pointer);
printf("%d has been dequeued",dequeue_number);
}
else{
printf("Your queue is empty\n");
}
break;
case 3:
for(i = 0; i < 10 ; i++ ){
printf("%d",queue_pointer->queue_array[i]);
}
break;
case 4:
exit(0);
break;
}
}
}
void enqueue(struct queue_structure *qs, int number){
if(qs -> queue_front == qs -> queue_rear){
qs->queue_front = qs->queue_rear = -1;
}
else{
(qs -> queue_rear)++;
qs->queue_array[qs->queue_rear] = number;
}
}
int dequeue(struct queue_structure *qs){
int i;
if(qs->queue_front == qs->queue_rear){
qs->queue_front = qs->queue_rear = -1;
}
else{
for(i = qs->queue_front; i < qs->queue_rear ; i++){
qs->queue_array[i] = qs->queue_array[i + 1];
}
}
}
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
int queue_empty(struct queue_structure *qs){
if((qs->queue_rear && qs->queue_front) == -1){
return 1;
}
else{
return 0;
}
}
}
我收到以下错误
/tmp/ccLJHnMG.o:在函数main': queue1.c:(.text+0xda): undefined reference to
queue_empty的main': queue1.c:(.text+0xda): undefined reference to
'collect2:ld返回1退出状态**
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
} <<<<<<<<<<<<<<<<<<<<<<
int queue_empty(struct queue_structure *qs){
...
最后的花括号是错位的,它应该是这样的:
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
} /* added the }*/
int queue_empty(struct queue_structure *qs){
if((qs->queue_rear && qs->queue_front) == -1){
return 1;
}
else{
return 0;
}
}
/* there was a } here that I've removed */
否则queue_empty
在queue_full
定义。 这不是标准的C,但它似乎被gcc
作为扩展支持,因此在编译期间没有错误。
使用-pedantic
编译代码时,gcc会将其标记为:
aix@aix:~$ gcc -pedantic qq.c
qq.c: In function ‘queue_full’:
qq.c:78: warning: ISO C forbids nested functions
qq.c:78: warning: ISO C90 forbids mixed declarations and code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.