Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么说我的队列';s的尺寸是0?_C_Struct_Queue_Malloc_Abstract Data Type - Fatal编程技术网

C 为什么说我的队列';s的尺寸是0?

C 为什么说我的队列';s的尺寸是0?,c,struct,queue,malloc,abstract-data-type,C,Struct,Queue,Malloc,Abstract Data Type,我想用C语言实现队列ADT 我有以下资料: // node structure: typedef int ElementType; typedef struct node { ElementType key; struct node* left_child; struct node* right_sib; }* Node; typedef Node Item; // queue structure: struct queue { Item* contents;

我想用C语言实现队列ADT

我有以下资料:

// node structure:

typedef int ElementType;
typedef struct node {
    ElementType key;
    struct node* left_child;
    struct node* right_sib;
}* Node;
typedef Node Item;

// queue structure:

struct queue {
  Item* contents;
  int head;
  int tail;
  int dim;
};
typedef struct queue* Queue;


Queue initqueue();                  // create a queue
int queueEmpty(Queue q);            // queue empty?
void enqueue(Queue q, Item elem);   // insert item
int size(Queue q);                  // size of the queue?


Queue initqueue() {
    Queue q = (Queue)malloc(sizeof(struct queue));
    q -> contents = (Item*)malloc(sizeof(Item));
    q -> head = 0;
    q -> tail = 0;
    q -> dim = 1;
    return q;
}

int queueEmpty(Queue q) {
    return (q -> head == q -> tail);
}

void enqueue(Queue q, Item elem) {
    if (q -> head == (q -> tail % q -> dim) + 1 || q -> dim == 1)
        q -> contents = (Item*)realloc(q -> contents, (q -> dim) * 2);
    q -> contents[q -> tail] = elem;
    q -> tail = (q -> tail + 1) % (q -> dim);
}

int size(Queue q) {
    if (q -> tail < q -> head)
        return ((q -> dim) - (q -> head) + (q -> tail) - 1);
    else
        return (q -> tail - q -> head);
}


int main() {
    Queue q;
    Item i = (Item)malloc(sizeof(struct node));
    i -> key = 4;
    q = initqueue();
    enqueue(q, i);
    printf("%d\n", queueEmpty(q));
    printf("%d\n", size(q));

    return 0;
}
//节点结构:
typedef int元素类型;
类型定义结构节点{
元素类型键;
结构节点*左_子节点;
结构节点*right\u sib;
}*节点;
typedef节点项;
//队列结构:
结构队列{
项目*内容;
int头;
内尾;
int-dim;
};
typedef结构队列*队列;
队列初始化队列();//创建队列
int queueEmpty(队列q);//队列是空的吗?
无效排队(队列q,项目元素);//插入项目
整数大小(队列q);//队列的大小?
队列初始化队列(){
队列q=(队列)malloc(sizeof(结构队列));
q->contents=(项目*)malloc(项目大小);
q->head=0;
q->tail=0;
q->dim=1;
返回q;
}
int queueEmpty(队列q){
返回(q->head==q->tail);
}
无效排队(队列q,项目元素){
如果(q->head==(q->tail%q->dim)+1 | q->dim==1)
q->contents=(Item*)realloc(q->contents,(q->dim)*2);
q->contents[q->tail]=元素;
q->tail=(q->tail+1)%(q->dim);
}
整数大小(队列q){
如果(q->tailhead)
返回((q->dim)-(q->head)+(q->tail)-1);
其他的
返回(q->tail-q->head);
}
int main(){
队列q;
项目i=(项目)malloc(sizeof(结构节点));
i->key=4;
q=initqueue();
排队(q,i);
printf(“%d\n”,queueEmpty(q));
printf(“%d\n”,大小(q));
返回0;
}

我不明白为什么输出是0和1,即队列是空的,其大小是0。

您忘记调整
q->dim
,因此
q->tail
排队后仍然是零(
1%1

.所以不是调试服务。使用符号编译,在调试器中运行代码,逐行跟踪程序,检查相关变量的值,了解实际情况。如果出现特定问题,请随时回到这里。信不信由你,但添加
typedef
s会使程序更难理解。名称越多,就越难了解其使用情况。请注意,点
和箭头
->
运算符绑定得非常紧密,不应被任何空格包围。另请参见-简短的回答是“通常不”。