以不同的方式改写#include <stdio.h> #include <stdlib.h> #define MAXLEN 100 typedef char DataType; typedef struct { DataType data[MAXLEN]; int length; } SeqBTree; void InitSeqBiTree(SeqBTree* T) { T->length = 0; } void CreateSeqBTree(SeqBTree* T) { int i; printf("请按顺序输入完全二叉树的节点:"); for (i = 1; i <= T->length; i++) { scanf_s(" %c", &T->data[i]); } } void ShowSeqBTree(SeqBTree* T) { int i = 1; int k; printf("输入完全二叉树(格式:双亲节点:左孩子,右孩子))\n"); while (i <= T->length) { printf("%c:", T->data[i]); if (2 * i <= T->length) printf("%c,", T->data[2 * i]); else printf(" ,"); if (2 * i + 1 <= T->length) printf("%c\n", T->data[2 * i + 1]); else printf(" \n"); i++; } } int main() { SeqBTree T; int n; InitSeqBiTree(&T); printf("请输入完成二叉树的节点个数:"); scanf_s("%d", &n); T.length = n; CreateSeqBTree(&T); ShowSeqBTree(&T); }这个代码
时间: 2023-06-24 10:08:35 浏览: 102
SeqList.h #include <stdio.h> //包含标准输入输出流的头文件 #include <assert.h
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100
typedef char DataType;
typedef struct {
DataType data[MAXLEN];
int length;
} SeqBTree;
void InitSeqBiTree(SeqBTree* T) {
T->length = 0;
}
void CreateSeqBTree(SeqBTree* T) {
int i;
printf("请按顺序输入完全二叉树的节点:");
for (i = 1; i <= T->length; i++) {
scanf_s(" %c", &T->data[i]);
}
}
void ShowSeqBTree(SeqBTree* T) {
int i = 1;
int k;
printf("输入完全二叉树(格式:双亲节点:左孩子,右孩子))\n");
while (i <= T->length) {
printf("%c:", T->data[i]);
if (2 * i <= T->length)
printf("%c,", T->data[2 * i]);
else
printf(" ,");
if (2 * i + 1 <= T->length)
printf("%c\n", T->data[2 * i + 1]);
else
printf(" \n");
i++;
}
}
int main() {
SeqBTree T;
int n;
InitSeqBiTree(&T);
printf("请输入完成二叉树的节点个数:");
scanf_s("%d", &n);
T.length = n;
CreateSeqBTree(&T);
ShowSeqBTree(&T);
return 0;
}
阅读全文