forked from Zxlove720/c-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2024.5.9.exercise.c
150 lines (144 loc) · 2.41 KB
/
2024.5.9.exercise.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int datatype;
//typedef struct list
//{
// datatype data;
// struct list* next;
//}list;
//
//typedef struct queue
//{
// list* front;
// list* rear;
// int length;
//}queue;
//
//void initqueue(queue* q)
//{
// q->front = q->rear = (list*)malloc(sizeof(list));
// if (q->front == NULL)
// {
// printf("NULL\n");
// return;
// }
// q->front->next = NULL;
//}
//
//bool isempty(queue* q)
//{
// if (q->front == q->rear || q->front->next == NULL)
// {
// return true;
// }
// return false;
//}
//
//list* creatnode()
//{
// list* newnode = (list*)malloc(sizeof(list));
// if (newnode == NULL)
// {
// printf("NULL\n");
// return NULL;
// }
// printf("请输入元素的值\n");
// datatype element;
// scanf("%d", &element);
// newnode->data = element;
// newnode->next = NULL;
// return newnode;
//}
//
//void inqueue(queue* q)
//{
// list* newnode = creatnode();
// q->rear->next = newnode;
// q->rear = newnode;
//}
//
//datatype outqueue(queue* q)
//{
// datatype temp;
// list* pointer;
// pointer = q->front->next;
// temp = pointer->data;
// q->front = pointer;
// if (q->front->next == NULL)
// {
// q->rear = q->front;
// }
// return temp;
//}
//
//int main()
//{
// queue q;
// initqueue(&q);
// if (!isempty(&q))
// {
// printf("not empty\n");
// return -1;
// }
// printf("有多少元素要入队\n");
// int n;
// scanf("%d", &n);
// int i = 0;
// for (i = 0; i < n; i++)
// {
// inqueue(&q);
// }
// for (i = 0; i < n; i++)
// {
// printf("%d ", outqueue(&q));
// }
// return 0;
//}
typedef struct tree
{
datatype data;
struct tree* left;
struct tree* right;
}tree;
tree* creattree(tree* root)
{
datatype element;
scanf("%d", &element);
if (element != 0)
{
root = (tree*)malloc(sizeof(tree));
if (root == NULL)
{
printf("NULL\n");
return NULL;
}
root->data = element;
root->left = NULL;
root->right = NULL;
root->left = creattree(root->left);
root->right = creattree(root->right);
}
return root;
}
void visit(tree* root)
{
printf("%d ", root->data);
}
void preorder(tree* root)
{
if (root != NULL)
{
preorder(root->left);
visit(root);
preorder(root->right);
}
}
int main()
{
printf("请输入节点的值\n");
tree* root = NULL;
root = creattree(root);
preorder(root);
}