Q->Data=(int*)malloc(sizeof(int)*(MaxSize+1));这里的sizeof(int*)(MaxSize+1)怎么理解
时间: 2023-11-20 16:06:09 浏览: 41
这条代码的含义是在堆内存中分配一个大小为 sizeof(int)*(MaxSize 1) 字节的空间,并将其首地址赋值给指针变量 Data。
其中,sizeof(int) 表示 int 类型所占用的字节数,MaxSize 1 表示要分配的 int 类型元素的个数(MaxSize 个元素加上一个额外的位置,用于防止数组越界)。因此,sizeof(int)*(MaxSize 1) 就是要分配的空间的总大小。
相关问题
Q->Data=(int*)malloc(sizeof(int)*(MaxSize+1));
A: This line of code dynamically allocates memory for an integer array of size MaxSize + 1. The malloc function is used to allocate memory on the heap, and the sizeof operator is used to determine the size of each integer element in the array. The resulting pointer to the allocated memory is then assigned to the variable Data.
完善如下代码:#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 #define ERROR 0 #define OK 1 typedef int Status; typedef char ElementType; typedef struct TNode{ ElementType Data; struct TNode * Left; struct TNode * Right; }BiTNode,* BinTree; typedef struct QNode{ BinTree Data[MAXSIZE]; int front,rear; }* Queue; void LevelorderTraversal ( BinTree BT ); Queue CreatQueue(); Status IsFullQ(Queue Q); Status AddQ(Queue Q,BinTree X); Status IsEmptyQ(Queue Q); BinTree DeleteQ(Queue Q); BinTree CreatBinTree() { ElementType Data; BinTree BT, T; Queue Q = CreatQueue(); scanf("%c",&Data); if( Data != '@'){ BT = (BinTree)malloc(sizeof(struct TNode)); BT->Data = Data; BT->Left = BT->Right = NULL; AddQ(Q,BT); } else return NULL; while(!IsEmptyQ(Q)){ T = DeleteQ(Q); scanf("%c",&Data); if( Data == '@') T->Left = NULL; else{ T->Left = (BinTree)malloc(sizeof(struct TNode)); T->Left->Data = Data; T->Left->Left = T->Left->Right = NULL; AddQ(Q,T->Left); } scanf("%c",&Data); if(Data == '@') T->Right = NULL; else{ T->Right = (BinTree)malloc(sizeof(struct TNode)); T->Right->Data = Data; T->Right->Left = T->Right->Right = NULL; AddQ(Q,T->Right); } } return BT; } Queue CreatQueue() { Queue Q = (Queue)malloc(sizeof(struct QNode)); Q->front = Q->rear = 0; return Q; } Status IsFullQ(Queue Q) { if( (Q->rear+1)%MAXSIZE == Q->front ) return OK; else return ERROR; } Status AddQ(Queue Q,BinTree X) { if ( IsFullQ(Q) ) { printf("队列满"); return ERROR; } else { Q->rear = (Q->rear+1)%MAXSIZE; Q->Data[Q->rear] = X; return OK; } } Status IsEmptyQ(Queue Q) { if( Q->front == Q->rear ) return OK; else return ERROR; } BinTree DeleteQ(Queue Q) { if ( IsEmptyQ(Q) ) { printf("队列空"); return NULL; } else { Q->front = (Q->front+1)%MAXSIZE; return Q->Data[Q->front]; } } int main() { BinTree BT; BT = CreatBinTree(); if(BT == NULL){ printf("\n空树!\n"); }else{ printf("层序遍历的结果为:"); LevelorderTraversal ( BT ); } return 0; }
```
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef int Status;
typedef char ElementType;
typedef struct TNode {
ElementType Data;
struct TNode *Left;
struct TNode *Right;
} BiTNode, *BinTree;
typedef struct QNode {
BinTree Data[MAXSIZE];
int front, rear;
} *Queue;
void LevelorderTraversal(BinTree BT);
Queue CreatQueue();
Status IsFullQ(Queue Q);
Status AddQ(Queue Q, BinTree X);
Status IsEmptyQ(Queue Q);
BinTree DeleteQ(Queue Q);
BinTree CreatBinTree();
int main() {
BinTree BT;
BT = CreatBinTree();
if (BT == NULL) {
printf("\n空树!\n");
} else {
printf("层序遍历的结果为:");
LevelorderTraversal(BT);
}
return 0;
}
BinTree CreatBinTree() {
ElementType Data;
BinTree BT, T;
Queue Q = CreatQueue();
scanf("%c", &Data);
if (Data != '@') {
BT = (BinTree) malloc(sizeof(struct TNode));
BT->Data = Data;
BT->Left = BT->Right = NULL;
AddQ(Q, BT);
} else return NULL;
while (!IsEmptyQ(Q)) {
T = DeleteQ(Q);
scanf("%c", &Data);
if (Data == '@')
T->Left = NULL;
else {
T->Left = (BinTree) malloc(sizeof(struct TNode));
T->Left->Data = Data;
T->Left->Left = T->Left->Right = NULL;
AddQ(Q, T->Left);
}
scanf("%c", &Data);
if (Data == '@')
T->Right = NULL;
else {
T->Right = (BinTree) malloc(sizeof(struct TNode));
T->Right->Data = Data;
T->Right->Left = T->Right->Right = NULL;
AddQ(Q, T->Right);
}
}
return BT;
}
Queue CreatQueue() {
Queue Q = (Queue) malloc(sizeof(struct QNode));
Q->front = Q->rear = 0;
return Q;
}
Status IsFullQ(Queue Q) {
if ((Q->rear + 1) % MAXSIZE == Q->front)
return OK;
else
return ERROR;
}
Status AddQ(Queue Q, BinTree X) {
if (IsFullQ(Q)) {
printf("队列满");
return ERROR;
} else {
Q->rear = (Q->rear + 1) % MAXSIZE;
Q->Data[Q->rear] = X;
return OK;
}
}
Status IsEmptyQ(Queue Q) {
if (Q->front == Q->rear)
return OK;
else
return ERROR;
}
BinTree DeleteQ(Queue Q) {
if (IsEmptyQ(Q)) {
printf("队列空");
return NULL;
} else {
Q->front = (Q->front + 1) % MAXSIZE;
return Q->Data[Q->front];
}
}
void LevelorderTraversal(BinTree BT) {
Queue Q = CreatQueue();
BinTree T;
if (!BT) return;
AddQ(Q, BT);
while (!IsEmptyQ(Q)) {
T = DeleteQ(Q);
printf("%c", T->Data);
if (T->Left) AddQ(Q, T->Left);
if (T->Right) AddQ(Q, T->Right);
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)