1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define ElemType int
5
6 // 定义队列结点
7 typedef struct QNode
8 {
9 ElemType data;
10 struct QNode* next;
11 }QNode, *QNodePrt;
12
13 // 定义队首、队尾指针
14 typedef struct
15 {
16 QNodePrt front, rear;
17 }LinkQueue;
18 void InitQueue(LinkQueue &link)
19 {
20 // link.front=link.rear=(QNodePrt)malloc(sizeof(QNode));
21 link.front=link.rear=(QNode*)malloc(sizeof(QNode)) ;
22 // QNode*与QNodePrt具有等价效果 QNode*=QNodePrt
23 link.rear->next=NULL;
24
25 }
26 void InitQueue0(LinkQueue* link)//使用指针传递
27 {
28 link->front=link->rear=(QNode*)malloc(sizeof(QNode));
29 link->front->next=NULL;
30 }
31
32 void InitQueue1(LinkQueue * &link)//创建指针别名为函数形参
33 {
34 link->front=link->rear=(QNode*)malloc(sizeof(QNode));
35 link->front->next=NULL;
36
37 }
38 //重新创建了指针
39
40
41
42
43 int main(int argc, char const* argv[])
44 {
45
46 // LinkQueue* que;
47 // que->front->data=1;
48 // printf("%d\n", que->front->data);
49 //// 无法输出data数据
50 // QNodePrt node;
51 // node->data=2;
52 // printf("%d\n",node->data);
53 //// 无法输出data数据
54 //
55 ////原因:两次创建指针都(没有实例化对象) 没有将结构体成功创建出来知识创建了其类型的指针
56 //
57 // LinkQueue que0;
58 // que0.front->data=3;
59 // printf("%d\n",que0.front->data);
60 ////无法输出data数据
61 ////原因:虽然LinkQueue完成成创建,但是front rear指针并没有指向实例化对象
62 // InitQueue(que0);
63 // que0.front->data=9;
64 // printf("%d\n",que0.front->data);
65 // 输出data数据 9
66
67 LinkQueue que1;
68 LinkQueue* link=&que1;
69 InitQueue0(&que1);
70 // InitQueue0(link); 初始化使用指针进行地址传递
71 //que1地址传递
72 que1.front->data=10;
73 printf("%d\n",que1.front->data);
74 //输出data 10
75
76
77 LinkQueue que2;
78 LinkQueue* link1=&que2;
79 InitQueue1(link1);
80 link1->front->data=9;
81 // 实参传递必须为指针类型
82 // InitQueue1(&que1); 报错必须传递指针 不可直接直接传递地址
83 printf("%d\n",que2.front->data);
84 //输出data 9
85
86 return 0;
87 }