对于'.'和'->'的使用,以及是否'*'有混淆
最近在学数据结构,但是对于代码中定义的结构体和结构体指针的使用总是
不清晰,不知道什么时候用
1.A.xx 例子中判空操作
2.A->xx 例子中二叉树创建
3.( \*A)->xx 例子中二叉树初始化处
4.(\*A).xx 例子中结点人队
希望大佬帮忙求解一下。下面是附上的例子。
例子:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
typedef char ElemType;
typedef int Status;
//二叉树的二叉链表存储表示
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct QNode{
BiTree Qdata;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//二叉树初始化 (对应(*A)->xx)
Status InitBiTree(BiTree *T){
(*T)=(BiTree)malloc(sizeof(BiTNode));
printf("初始化函数中:%d",sizeof(*T));
if(!(*T)) exit(OVERFLOW);
//(*T)->data=NULL;//是否需要初始化data中的值
(*T)->lchild=NULL;
(*T)->rchild=NULL;
return OK;
}
//二叉树创建 (对应A->xx)
Status CreateBiTree(BiTree &T){
char ch=getchar();
if(ch=='.')
T=NULL;
else{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
CreateBiTree(((T)->lchild));
CreateBiTree(((T)->rchild));
}
return OK;
}
//结点入队 (对应(*A).xx)
Status EnQueue(LinkQueue *Q,BiTree e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->Qdata=e;
p->next=NULL;
(*Q).rear->next=p;
(*Q).rear=p;
return OK;
}
//判断是否队列为空 (对应 A.xx)
Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear){
return TRUE;
}else{
return FALSE;
}
}
如果可以在说明下函数的参数何时选择&T,何时选择*T就更好了。感谢大佬